PDA

View Full Version : Problem with multiple record updates


?gkj
07-10-2003, 04:34 PM
I have looked at a lot of threads for this problem and no one actuallly tells anyone what exactly to code, they just throw in a idea.

I dynamically make a form and upon submitting I want it to update those fields.
It works fine except that it only updates the first field and stops updating. I have printed $tmp & $val so I know that those are correct.
What should I change in this code for mysql to run all the update queries.


$i=1;
while (list ($key,$val) = each ($_POST)){
$db_connect = mysql_connect("localhost", "$db_user", "$db_pass");
mysql_select_db("$db_name",$db_connect);

if ($i < $total_fields){
$tmp = mysql_field_name($result, $i);
$sql= "UPDATE $table_name SET $tmp='$val' WHERE $index_field = '$old_info'";
mysql_query($sql, $db_connect);
mysql_close();
$i++;
}
}

?gkj
07-10-2003, 05:18 PM
I finally figured it out:

User form sends two values and three hidden values.
(hidden type)$total_fields = 2.
(hidden type)$name
(hidden type)$name_value


$result = mysql_query("SELECT * FROM $table_name WHERE $name= '$name_value",$db_connect);
$i=1;
while (list ($key,$val) = each ($_POST)){

if ($i < $total_fields and ($i !=$total_fields-1)){
$tmp = mysql_field_name($result, $i);
$setvalues.= $tmp."='".$val."',";}

if ($i == $total_fields-1){
$tmp = mysql_field_name($result, $i);
$setvalues.= $tmp."='".$val."' ";}

$i++;
}
$sql= "UPDATE $table_name SET $setvalues WHERE $name= '$name_value";
mysql_query($sql, $db_connect);
print 'Thank you! Entry has been updated.';
}