dsshsta97935 2018-06-28 05:13
浏览 42
已采纳

如何使用where条件更新多个数据

I want update multiple data's from array with where condition (2 post value)

$markid=50008; (Post Item)
$ass='Att1';(Post Item)

$data=array( 'regno'=>$regno,'assmark'=>$assmark);

$query="update pre_marks set $ass=$assmark where regno=$regno and markid=$markid" ;
$this->db->query($query);

My output should be

Array
(
    [regno] => 210417103001
    [Att1] => 58
)
Array
(
    [regno] => 210417103002
    [Att1] => 23
)
Array
(
    [regno] => 210417103003
    [Att1] => 20
)
Array
(
    [regno] => 210417103004
    [Att1] => 45
)
Array
(
    [regno] => 210417103005
    [Att1] => 25
)
Array
(
    [regno] => 210417103006
    [Att1] => 0
)

This worked as well. But error comes

A Database Error Occurred Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'where regno= and markid=50009' at line 1

update pre_marks set Att1= where regno= and markid=50009

Filename: C:/xampp/htdocs/Admin-Panel/system/database/DB_driver.php

Line Number: 691

How to solve this?

$markid = $this->input->post('markid');
$ass = $this->input->post('sel_ass1');                 
for($i=12;$i<=$i+6;$i++) {       
    $regno= $objWorksheet->getCellByColumnAndRow(2,$i)->getValue();
    $assmark= $objWorksheet->getCellByColumnAndRow(5,$i)->getValue();

    $data=array('regno'=>$regno,$ass=>$assmark);
    echo '<pre>';
    print_r($data);
    echo '</pre>';

    $this->db->where(['regno' => $regno,'markid' => $markid]);
    $this->db->update('pre_marks',[$ass => $assmark]);
    if ($this->db->affected_rows() > 0 ) {
        return TRUE;
    }
    else {
        return FALSE;
    }
}
  • 写回答

1条回答 默认 最新

  • dongmu2517 2018-06-28 06:04
    关注

    You should check variables before using them in query!

    if(!empty($markid) && !empty($ass)) {
      if(!empty($assmark) && !empty($regno)) {
        $this->db->set($ass, $assmark)
          ->where("regno", $regno)
          ->where("markid", $markid)
          ->update("pre_marks");
        if ($this->db->affected_rows() > 0 ) {
          echo TRUE;
        }
        else {
          echo FALSE;
        }
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能