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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀