0
我在foreach循環內創建了一個表,在表中顯示了所有記錄。然後我包含一個SQL更新與選擇和更新一個數組值。當我選擇不同的值時,循環會更新表中的所有記錄,而不是隻有一個被更新。我整天都在苦苦掙扎,請幫助我。PDO在foreach循環中更新行值
如果foreach是錯誤的方式,我將非常感謝您對如何顯示可顯示記錄並允許更新每個陣列的表的建議。
<?php
foreach ($stmt as $key => $row)
{
$newcustomerid = htmlentities($row['customer_id']);
echo '<tr><td>' . htmlentities($row['customer_company']) . '</td>';
echo '<td> id:' . $newcustomerid . 'key: ' . $key .'</td>';
echo '<td>' . htmlentities($row['customer_email']) . '</td>';
echo '<td>' . htmlentities($row['customer_phone']) . '</td>';
echo '<td>' . htmlentities($row['customer_country']) . '</td>';
echo '<td>' . htmlentities($row['customer_city']) . '</td>';
echo '<td>' . htmlentities($row['customer_segment']) . '</td>';
echo '<td>' . htmlentities($row['customer_updated']) . '</td>';
$customer_status = htmlentities($row['customer_status']);
if(isset($_POST['Submitbb'])){ //check if form was submitted
$input = $_POST['Submitbb']; //get input text
$stmtUpdateStatus = $conn->prepare("UPDATE user_customers SET `customer_status` = :customer_status WHERE `customer_id` = :customer_id");
$stmtUpdateStatus->execute(array(':customer_status' => $input, ':customer_id' => $row['customer_id']));
}
echo '<td>
<form action="" method="post">
<select name="Submitbb" onchange="this.form.submit();">
<option> - ' . $customer_status . ' - </option>
<option>Susisiekti</option>
<option>Priminimas 1</option>
<option>Priminimas 2</option>
<option>Paskambinti</option>
<option>Netinkamas klientas</option>
</select>
</form>
</td>';
echo '<td>' . 'Išsaugoti' . '</td></tr>';
}
?>
謝謝。
謝謝!但是..它說:「未定義的索引:customer_id」在您張貼的第二個代碼塊的第三行。 – Lukas
嘿,採取兩個塊的新副本,我做了幾個編輯,其中一個修復該錯誤 – andrew
謝謝,我現在就試試。 – Lukas