Обновление нескольких столбцов и строк с помощью PDO

Я взял небольшой фрагмент из форума, который позволяет мне обновлять несколько строк с помощью PDO. В примере используется только один столбец, но я хочу, чтобы он мог включить обновление по столбцам.

Я немного изменил фрагмент, проблема в том, что одно изменение в строке (url) изменит всю запись в строке (url)

Если у кого-то есть глаза, чтобы посмотреть, в чем проблема:

if (isset($_POST['submit'])) { $stmt = $db->prepare("UPDATE `$tbl_name` SET `url`=:url, `country`=:country WHERE id=:id"); $stmt->bindParam(':id', $id, PDO::PARAM_INT); $stmt->bindParam(':url', $url, PDO::PARAM_STR); $stmt->bindParam(':country', $country, PDO::PARAM_STR); foreach ($_POST['url'] as $id => $url) { $stmt->execute(); } foreach ($_POST['country'] as $id => $country) { $stmt->execute(); } echo '<h1>Updated the records.</h1>'; } // Print the form. echo '<form action="' . htmlspecialchars($_SERVER['PHP_SELF']) . '" method="post">'; foreach ($db->query("SELECT * FROM `$tbl_name` ORDER BY `id`") as $row) { echo '<input type="text" name="url[' . (int)$row['id'] . ']" value="' . htmlspecialchars($row['url']) . '" /><input type="text" name="country[' . (int)$row['id'] . ']" value="' . htmlspecialchars($row['country']) . '" /><br />'; } echo '<input type="submit" name="submit" value="Update" /></form>'; 

Related of "Обновление нескольких столбцов и строк с помощью PDO"

Вы только захотите сделать один цикл, привязав как $url и $country . Что-то вроде этого

 if (!isset($_POST['url'], $_POST['country']) || count($_POST['url']) != count($_POST['country'])) { throw new Exception('URL / country mismatch'); } foreach ($_POST['url'] as $id => $url) { if (!array_key_exists($id, $_POST['country'])) { throw new Exception("No matching country for ID $id"); } $country = $_POST['country'][$id]; $stmt->execute(); }