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;
        }
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 php的curl不支持http2
  • ¥15 为什么我按照电路图做出的仿真和实物都不能使用
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多