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条)

报告相同问题?

悬赏问题

  • ¥15 关于IMageEnView 图标定位问题
  • ¥20 求解答(matlab)
  • ¥30 ffmpeg库使用过程中遇到的问题
  • ¥15 pyqt5 中python如何通过Qtwebchannel主动发消息给web前端
  • ¥15 关于HTML中title获取xml内容的问题
  • ¥15 fanuc机器人PRIO083数字信号未复原错误,如何解决?
  • ¥20 如何为现有电路板增加远程控制功能
  • ¥15 UE5打包失败,求解决
  • ¥15 请问STM32G431的CANOPEN协议函数怎么写
  • ¥15 graphpad prism 三因素重复测定报错