2013-07-10 140 views
-1

關於爲什麼我的數據未被更新的任何想法?他們的寫作準備陳述的方式有什麼根本性錯誤?從數據循環插入PHP準備的語句插入

形式:

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(); 
} 


} 

回答

2

UPDATE語法是不正確的。它應該是:

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

你試圖用戶INSERT語法在UPDATE聲明。他們根本不相似。

Documentation

+0

感謝您的回覆,但我不認爲這是正確的,因爲我使用相同的方法插入數據到我的數據庫,它的工作原理。只是不更新​​(代碼上面) – user1040259

+1

看到我的新答案。 – Barmar

+0

謝謝!關閉..現在「空」正在更新到我的領域從POST。現在需要弄清楚:( – user1040259

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); 
} 
+0

沒有區別。我喜歡你的想法,謝謝。 – user1040259

+0

嗯,我注意到你的更新中有'sssi',這是一個額外的價值綁定。你只有4個?雖然 – KyleK

+0

這個bind_param('sssi',$ week_date,$ crew_chief,$ monday_crew,$ log_dates_ID);應該是bind_param($ week_date,$ crew_chief,$ monday_crew,$ log_dates_ID); – KyleK