I have a MySQL database with the table "chapters". This table has three columns: id, name, password. I am automatically outputting the contents of this table via an HTML form (it's an update page where the user can edit all of the values, hence outputting the values in input fields). Here's the code for the output:
<table>
<?php
while ($row=mysql_fetch_array($result)) {
echo "<tr id='chapter-{$row['id']}' chapter-id='{$row['id']}'>";
echo "<td>";
echo "<input type='text' name='chapter[][id]' value='{$row['id']}'>";
echo "</td>";
echo "<td>";
echo "<input type='text' name='chapter[][name]' value='{$row['name']}'>";
echo "</td>";
echo "<td>";
echo "<input type='text' name='chapter[][password]' value='New Password'>";
echo "</td>";
echo "<td>";
echo "<img class='delete-chapter' src='reject-button.png' alt='Delete Button'>";
echo "</td>";
echo "</tr>";
}
?>
</table>
I am passing all of that information into ChaptersUpdate.php via a submit button that's on the bottom of that form. The idea is that ChaptersUpdate.php should look at all of the information within $_POST and update the "chapters" table so that it reflects the new values that the user put in. Here's the PHP code:
<?php
include 'database_login.php';
foreach ($_POST['chapter'] as $chapter_id => $chapter_info)
{
mysql_query("UPDATE chapters SET name={$chapter_info['name']}, password={$chapter_info['password']} WHERE id={$chapter_info['id']}");
}
?>
Only problem is that, for some reason, the SQL query that actually ends up running looks horrible and SUPER-confusing:
UPDATE chapters SET name=, password= WHERE id=132UPDATE chapters SET name=MIT, password= WHERE id=UPDATE chapters SET name=, password=New Password WHERE id=UPDATE chapters SET name=, password= WHERE id=133UPDATE chapters SET name=Harvard, password= WHERE id=UPDATE chapters SET name=, password=New Password WHERE id=
I am not really sure where to start here...is the problem in my forms and how I am setting up the array or in my PHP file?
Thanks in advance!