duanchijie2323 2013-02-04 15:45
浏览 238
已采纳

尽管一切正确,MySql查询语法错误

All the server details are put correctly, and the connection does happen properly, I cannot seem to find the syntax error that shows up in the output.

<?php
    ob_start();
    $phn=$_GET['phn'];
    $con = mysql_connect("<server>","<user>","<pass>") or die('error' . mysql_error());
    $db_selected = mysql_select_db('<db name>', $con);
    if (!$db_selected) {
        die ('Can\'t use user : ' . mysql_error());
    }
    $characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
     $key= '';
     for ($i = 0; $i < 9; $i++) {
          $key.= $characters[rand(0, strlen($characters) - 1)];
     }
     echo $key;
    $sql="INSERT INTO buser (phone, key) VALUES (".$phn.",'".$key."')";
    if($result = mysql_query($sql ,$con) or die ('Error: '.mysql_error ()))
    {
    $q="SELECT * FROM buser WHERE phone=$phn";
    $idd=mysql_query($q,$con) or die ('Error: '.mysql_error ());
    while($row = mysql_fetch_assoc($idd))
      {
      $id=$row['bid'];
      }
    }
     ?>

Output:

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key) VALUES (9999,'ywfjj2dtc')' at line 1

  • 写回答

1条回答 默认 最新

  • douna5529 2013-02-04 15:46
    关注

    KEY is a MySQL reserved keyword. You must enclose it in backticks to use it as a column or table identifier.

    $sql="INSERT INTO buser (phone, `key`) VALUES (".$phn.",'".$key."')";
    

    Your script is vulnerable to SQL injection in its current form. At a minimum, you must call mysql_real_escape_string() on the value of $phn.

    $phn = mysql_real_escape_string($_GET['phn']);  
    

    In the long term, consider switching to an API which supports prepared statements, like MySQLi or PDO.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊