douxian7534 2017-10-25 09:35
浏览 31
已采纳

MySQL一次更新多行

I have a table which includes a row for each day of the week.

Each row contains 2 input fields.

enter image description here

I am wanting to click one save button which will update all rows from the table into seperate MySQL rows.

I have the below code to insert new rows (which works fine) but wondering how this can be changed to an UPDATE statement?

 $insertArr = array();
    for ($i=0; $i<$cnt; $i++) {
        $insertArr[] = "('" 
            . mysql_real_escape_string($_GET['Actual'][$i]) .
            "', '" 
            . mysql_real_escape_string($_GET['Period'][$i]) .
            "', '" 
            . mysql_real_escape_string($_GET['AddedBy'][$i]) .
                "', '" 
            . mysql_real_escape_string($_GET['Date'][$i]) .
                "', '" 
            . mysql_real_escape_string($_GET['Employee'][$i]) .
                "', '" 
            . mysql_real_escape_string($_GET['Rotered'][$i]) . "')";
}



 $query = "INSERT INTO hr_employee_rostered_hours (Actual, PeriodID, AddedBy, DateOfHours, EmployeeUniqueID, Rotered) VALUES " . implode(", ", $insertArr);
 mysql_query($query) or trigger_error("Insert failed: " . mysql_error());
}
  • 写回答

2条回答 默认 最新

  • doushizhou4477 2017-10-25 09:50
    关注

    The mysql extension has been deprecated in PHP, and I strongly advice against using it.

    Assuming that you're still getting the values that you want to update using the array,

    Here is a link about PDO (not official docummentation) that helped me out when I first started with PHP and PDO

    Here's an example using PDO

    $updateq = "UPDATE hr_employee_rostered_hours SET (Actual = :actualvalue, PeriodID = :periodid, AddedBy = :addedby,DateOfHours = :dateofhrs, Rotered = :rotered ) WHERE EmployeeUniqueID = :employeeid";
    $updatex = $dbh->prepare($updateq);
    $updatex->bindValue(":actualvalue",$insertArr[0]);
    $updatex->bindValue(":periodid",$insertArr[1]);
    $updatex->bindValue(":addedby",$insertArr[2]);
    $updatex->bindValue(":dateofhrs",$insertArr[3]);
    $updatex->bindValue(":periodid",$insertArr[5]);
    $updatex->bindValue(":employeeid",$insertArr[4]);
    $updatex->execute();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果