dongyong6428 2014-10-30 17:03
浏览 23
已采纳

简单更新php中的两个字段会出错

I am trying to update some fields in a mysql database from php. This always gives me error, and I don't know why. Could you have a quick look?

         $sqlupdate_ = "UPDATE $db SET city=$city, country=$country WHERE  userIP=$ip;";
              $sqlupdate = mysql_query($sqlupdate_);
              echo $sqlupdate_;

 if($sqlupdate){

      //The query returned true - now do whatever you like here.
      echo 'success';

 }else{

      //The query returned false - you might want to put some sort of error reporting here. Even logging the error to a text file is fine.
      echo "error: ";

 }
  • 写回答

2条回答 默认 最新

  • dongtuan1980 2014-10-30 17:07
    关注

    First of all:, use mysqli or PDO since mysql functions are depracated.

    Second: avoid sql injections with escape your strings.

    I think, your problem is that, your city, country and ip are varchars not integers, so you need to add quotes around them, and you want to update your table not your database.

    Try to echoes your query and run directly in mysql.

    $sqlupdate_ = "UPDATE " . mysqli_real_escape_string($link, $db). " SET city= '" .mysqli_real_escape_string($link, $city) ."', country='".mysqli_real_escape_string($link, $country)."' WHERE  userIP='".mysqli_real_escape_string($link, $ip)."'";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题