dstd2129 2014-03-29 21:50
浏览 41
已采纳

尝试使用PHP将数据从表单发送到MySql时出错

I have some problems while trying to send data from form to mysql database using php.I know how to fix this when i set form action to anothen page (<form action="example.php>, but i want that all procces stay on one page. WHen i run my php script and enter name in both of fields and go send, only url page changes, nothing else.Hope u can help me.Thanks

<?php
$con=mysqli_connect("localhost","root","","test");
if (mysqli_connect_errno())
{
echo"Error connecting to database". mysqli_connect_error();
}
if (isset($_POST['input_send']))
{
$name=($_POST['input_name']);
$lastname=($_POST['input_lastname']);
$insert="INSERT INTO test_mysql (name, lastname) VALUES ('$name', $lastname)";
echo"record added";
}
?>
<form action="" action="post">
First name: <input type="text" name="input_name"/>
Last name: <input type="text" name="input_lastname"/>
<input type="submit" value="send" name="input_send"/>
</form>
  • 写回答

3条回答 默认 最新

  • doubu0897 2014-03-29 21:58
    关注

    change this

      $insert="INSERT INTO test_mysql (name, lastname) VALUES ('$name', $lastname)";
    

    to

     mysqli_query("INSERT INTO test_mysql (name, lastname) VALUES ('$name', '$lastname')");
    

    and this

    action="post"
    

    to

    method="post"
    

    and escape your variables like that:

    $name=mysqli_real_escape_string($_POST['input_name']);
    $lastname=mysqli_real_escape_string($_POST['input_lastname']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?