-1

关于为什么我的数据没有更新的任何想法?他们对我编写准备好的陈述的方式有根本性的错误吗?

表格:

while($log_dates = mysqli_fetch_assoc($q_log_dates_result)) {   
 echo "<tr>";
 echo "<input type='hidden' name='data[][log_dates_ID]' value='" . $log_dates['log_dates_ID'] . "'/>";
 echo "<td><input type='text' name='data[][week_date]' value='" . $log_dates['week_date'] . "' /></td>";
 echo "<td><input type='text' name='data[][crew_chief]' value='" . $log_dates['crew_chief'] . "' readonly /></td>";
 echo "<td><input type='text' name='data[][monday_crew]' value='". $log_dates["monday_crew"] ."'/></td>";
 echo "</tr>";
} // end while loop

PHP:

if (isset($_POST['submit'])) {

$stmt = $connection->stmt_init();
if($stmt->prepare("UPDATE log_dates SET (week_date, crew_chief, monday_crew) VALUES (?, ?, ?) WHERE log_dates_ID = ?")) {

// Bind your variables to replace the ?s
$stmt->bind_param('sssi', $week_date, $crew_chief, $monday_crew, $log_dates_ID);


$returnedData = $_POST['data'];



for($i=0;$i<count($returnedData);$i+=4){
    $log_dates_ID = $returnedData[$i]['log_dates_ID'];
    $week_date = $returnedData[$i+1]['week_date'];
    $crew_chief = $returnedData[$i+2]['crew_chief'];
    $monday_crew = $returnedData[$i+3]['monday_crew'];
    $stmt->execute();
}


    // Close statement object
    $stmt->close();
}


}
4

2 回答 2

2

您的 UPDATE 语法不正确。它应该是:

UPDATE log_dates
SET week_date = ?, crew_chief = ?, monday_crew = ?
WHERE log_dates_ID = ?

您正在尝试INSERT在语句中使用语法UPDATE。他们一点也不相似。

文档

于 2013-07-10T17:25:00.647 回答
1

我会这样做...

在函数内部...

function updatedata($week_date, $crew_chief, $monday_crew, $log_dates_ID){
 $stmt = $connection->stmt_init();
 $stmt->prepare("UPDATE log_dates SET (week_date, crew_chief, monday_crew) VALUES (?, ?, ?) WHERE log_dates_ID = ?")) 
 $stmt->bind_param($week_date, $crew_chief, $monday_crew, $log_dates_ID);
 $stmt->execute();
}

然后在你的循环中......

for($i=0;$i<count($returnedData);$i+=4){
$log_dates_ID = $returnedData['log_dates_ID'][$i];
$week_date = $returnedData['week_date'][$i];
$crew_chief = $returnedData['crew_chief'][$i];
$monday_crew = $returnedData['monday_crew'][$i];
updatedata($week_date, $crew_chief,$monday_crew, $log_dates_ID);
}
于 2013-07-10T16:16:42.067 回答