novice mySQL UPDATE of multiple records from form
Posted: Sun Oct 19, 2008 8:09 am
To simplify, I have stripped my code example down to the essential issue. I have a form that is populated with a list of records from a database. An example would be a list of products and their assiciated prices. There is a auto_increment primary key. With all the product items on one screen, I want to change prices of some, then reupload the whole lot back to the database. I can populate my form just fine, but I can't get the edited data back into the database. I've tried to set up a foreach loop, but no changes were reflected. Any help appreciated.
Code: Select all
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<?php
include("php_includes/localhost.php");
$result = mysql_query ("SELECT test1, id from test") or die ('Error: '.mysql_error ());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<p>
TEST1: <input name="test1" type="text" value="<?php echo ($row["test1"])?>"/>
<input name="id" type="text" value="<?php echo ($row["id"])?>"/>
<?php
;}
?>
<input type="hidden" name="submitted" value="1" />
<input type="submit" value="Submit" />
</form>
<?php if ($_POST['submitted']) {
$test1 = $_POST['test1'];
$id = $_POST['id'];
$sql = "UPDATE test SET
test1='$test1',
WHERE id='$id'";
$ok = @mysql_query($sql);
}
?>