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 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c