douyan2821 2013-10-30 22:52
浏览 18
已采纳

插入数据库返回空? [关闭]

I'm having problems with inserting into a database. the query completes successfully but when I look in the database the fields are empty.

Here's my HTML form:

<form action = "add_at.php" method = "post">
    <input type = "text" class = "userip" name="userip">
    <input type = "text" class = "coordinates" name="coordinates">
    <input type = submit>
</form>

Here's my php script:

<?php

$mysql_host ='host';
$mysql_user ='user';
$mysql_pass ='pass';
$mysql_database = 'database';
$user_ip = $_POST['userip'];
$user_cords = $_POST['coordinates'];

mysql_connect($mysql_host, $mysql_user, $mysql_pass) or die('unable to connect.');
mysql_select_db($mysql_database);
$query = "INSERT INTO `userips` (ip,cord) VALUES ('$user_ip','$user_cords')";

mysql_query($query) or die('nope, sorz.');
mysql_close();

echo('woot wooooooooooooot')

?>

Update:

I found the problem. I needed to add a \ to one of my input fields in my HTML form. Simple mistake. Thank you all for your help, though.

  • 写回答

2条回答 默认 最新

  • doushan5245 2013-10-30 23:02
    关注

    You should check to see if your post variables are set before calling your SQL command.

    Eg...

    if (isset($_POST['userip']))
    

    Also, you are open to SQL injection.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!