dongya767979565 2013-05-06 21:21
浏览 24
已采纳

MYSQL错误而不是null仍然向mysql添加数据

Hi I have a piece of code I am trying to use to populate a new database table. The problem I am having is that i am getting the following 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 'N TENSION')' at line 2

The second problem is that the code inserts all entries and not just the ones where the field search_term has a value

my code is here

 $query = "SELECT * FROM vistordetails1 WHERE search_term IS NOT NULL";      
 $result = mysql_query($query) or die(mysql_error());
 while($row = mysql_fetch_array($result)){

$search_term =$row['search_term'];
    $client_id =$row['client_id'];


     mysql_query("INSERT INTO google1 
     (client_id, term) VALUES('$client_id', '$search_term') ") 
                   or die(mysql_error());  

          }
  • 写回答

1条回答 默认 最新

  • douzhiji2020 2013-05-06 21:26
    关注

    You can do everything with a single query:

    INSERT INTO google1 (client_id, term)
    SELECT client_id, search_term
    FROM vistordetails1
    WHERE search_term IS NOT NULL
          AND search_term <> ''
    

    (anyway, the problem is that probably you have a term that contains a ', and it is not correctly escaped)

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

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路