doue9730 2011-07-15 05:38 采纳率: 100%
浏览 39
已采纳

使用foreach循环插入查询的问题

I am using foreach loop for insertion query. At the same time I am matching value if those are exist in database or not. If value exist than I want to skip that particular key and than move to next key value to insert. Again it check if value exist or not than save record if values are not exist in database.

I have used following approach to do this.

foreach( $inputs as $key => $value) {
if ($key == 'empty') continue;
    $check=mysql_query("select * from from t_atc_list where s_title='$key' and r_name='$value'");

    if(mysql_num_rows($check)!=0){ continue;        }
    $result = mysql_query("INSERT into t_atc_list(a_name, s_title, r_name) values('$album','$key', '$value')")or die(mysql_error());
    echo "<strong>$key</strong> Singer <strong>$value</strong></br>";

}

NOTE : (1). Values are inserting if value are already present that is duplicate entries. (2). I am getting warning " mysql_num_rows(): supplied argument is not a valid MySQL result resource in..."

  • 写回答

1条回答 默认 最新

  • doufei3561 2011-07-15 05:40
    关注

    You have the word "from" twice in your first query, which causes your query to have invalid syntax and return (bool)false instead of a MySQL resource. So mysql_num_rows($check) throws an error and the if statement always evaluates to false.

    Change:

    select * from from t_atc_list
    

    To:

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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料