【问题标题】:Getting Error with qoutes in MySQL query在 MySQL 查询中出现引号错误
【发布时间】:2015-07-21 07:48:02
【问题描述】:

这是我的代码:

$result = mysql_query("INSERT INTO clients (client_id, name, surname, tel1,tel2,id_num,address)
   VALUES ('" .$updating_id . "','" .$updatedName1 . "','" .$updatedName1 . "',  '" .$updatedSurname1
   . "', '" . $updatedTel1  . "', '" .$updatedTel2 . ", '" .$updatedId_num1. "', '" .$updatedAddress1.
   ")  ON DUPLICATE KEY UPDATE    name='" . $updatedName1 . "', surname='" . $updatedSurname1 . "',
   tel1='" . $updatedTel1 . "', tel2='" . $updatedTel2 . "', id_num='" . $updatedId_num1 . "',
   address='" .$updatedAddress1 . "'");
   if(mysql_query($result))
   {  echo $updatedName1,"  ", $updatedSurname1, " updated successfully "; 
   }                        
   else {
   echo  mysql_error();} 
   }

我注意到mysql_query("INSERT INTO 上的第一句话... 以VALUES ('" .$updating_id . "'... 声明的第一个引号结束,但我引用的方式是我的示例中的方式,我已经评估过。

【问题讨论】:

  • 错误说明了什么?
  • 不要再使用已弃用的mysql_* API。将 mysqli_*PDO 与准备好的语句一起使用。
  • @kristian 错误是'查询为空'
  • 你打电话给mysql_query 两次并在$result 上打电话毫无意义。
  • 使用if($result) 而不是if(mysql_query($result))

标签: php html mysql apache


【解决方案1】:

使用if($result) 代替if(mysql_query($result))。谢谢@Vinie

您在VALUES 声明中遗漏了两个简单的引号:

$updatedTel2 . "'
$updatedAddress1."'

您需要查看mysql_real_escape_string(); 或至少使用 PDO :)

【讨论】:

  • 非常感谢这个 mysql_real_escape_string();功能省去很多麻烦
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多