Hi. I am trying to create a form that can update mutiple fields in a mysql database. I need it to be able to also update mutiple rows of the same field. The code below doesn't update, it actaually delets all data from the 4 rows. Any ideas? Thanks.
<html>
<head>
<title>Update rows in database table</title>
</head>
<body>
<?
$host = "myhost"; //database host
$user = "myusername"; //database user
$password = "mypw"; //database password
$database = "mydb"; //name of database
$table = "catalog"; //name of table
$columns = "catalog,Name,StreetAddress,City,Country";
$unique_column = "catalog";
$link_id = mysql_connect($host,$user,$password) or die("Cannot connect to database");
$db = mysql_select_db($database) or die("Cannot select database");
if(isset($HTTP_POST_VARS[update]))
{
if(count($update_id) > 0)
{
$update_rows = implode(",",$update_id);
$query = "UPDATE $table SET Name='$Name' , StreetAddress='$StreetAddress' , City='$City' , Country='$Country' WHERE $unique_column=($update_rows)";
$result = mysql_query($query) or die("Error in Query: >>>$query<<<. Error: " . mysql_error());
$affected = mysql_affected_rows();
$count = count($update_id);
$msg = "$affected / $count rows updated";
}
else
{
$msg = "No rows selected for updating";
}
}
$i = explode(",",$columns);
$num_columns = count($i);
echo "<form method='post' action='$PHP_SELF'>\n";
echo "<table border='1' cellpadding='1' cellspacing='1'>\n";
if(isset($msg))
{
echo "<tr><td align='center' colspan='" . ($num_columns+1) . "'><i>$msg</i></td></tr>\n";
}
echo "<tr>\n";
echo "<td align='center'><b>Update?</b></td>\n";
for($x=0;$x<$num_columns;$x++)
{
echo "<td align='center'><b>" . $i[$x] . "</b></td>\n";
}
echo "</tr>\n";
$query = "SELECT $columns FROM $table";
$result = mysql_query($query) or die("Error in Query >>>$query<<<. Error: " . mysql_error());
if($row = mysql_fetch_array($result))
{
do{
echo "<tr>\n";
echo "<td><input type=checkbox name=update_id[] value='" . $row[$unique_column] . "'></td>\n";
echo "<td>$row[$unique_column]</td>\n";
for($x=1;$x<$num_columns;$x++)
{
echo "<td><input type=text name=$fname size=20 value=$row[$x]></td>\n";
}
echo "</tr>\n";
}while($row = mysql_fetch_array($result));
echo "<tr><td align='center' colspan='" . ($num_columns + 1) . "'><input type='submit' name='update' value='Update Selected Products'></td></tr>\n";
}
echo "</table>\n";
echo "</form>\n";
?>
</body>
</html>