PDA

View Full Version : Update multiple field in PHP/MYSQL



devil_vin
09-10-2007, 07:37 AM
Hi,guys!I have a form need to update several fields in database,may I know what is the correct sql statement? One of the field is not editable,I just need to update other editable fields value to database. The below update statement seems went wrong.Thanks for your help...




if ($numrow != 0)
{// fetch each record in result set
for ($counter = 0; $row = mysql_fetch_row($result); $counter++)
{
foreach ($row as $key => $value)
{
if ($key == 0 || $key == 2)
{
?>
<td><input type = "text" name = "edited1" size = "30" value = "<?php
echo $value; ?>"></td>
<?php
}
else
if ($key == 1)
{

?>
<td><input type ="text" size = "30" value = "<?php
echo $value; ?>" readonly ="readonly"></td>
<?php
}
else
{
?>
<td><textarea name = "edited2" rows = "3"><?php
echo $value; ?></textarea></td>
<?php
}
}
}
}
}

?>

</tr>
</table>
<table>
<tr>
<td>&nbsp;</td>
<td width='4%'>&nbsp;</td>
<td></tr>
<tr>
<td>&nbsp;</td>
<td width='4%'>&nbsp;</td>
<td><input type='submit' name='submit' value='Upgrade'></td>
</table>
</form>
</body>
</head>
</html>

<?php

$tbl_name = "member";
if (isset($_POST['submit']))
{
$insert = mysql_query("UPDATE $tbl_name SET name = '" . $_POST['edited1'] . "'
WHERE name = '" . $_REQUEST['name'] . "');

devil_vin
09-10-2007, 08:47 AM
I have tried the following script to update multiple field but data not save into database



<?php

$tbl_name = "member";
if (isset($_POST['submit']))
{
$update = mysql_query("
UPDATE
$tbl_name
SET
name = '" . mysql_real_escape_string($_POST['edited1']) . "',
telephone = '" . mysql_real_escape_string($_POST['edited1']) . "',
address = '" . mysql_real_escape_string($_POST['edited2']) . "'
WHERE
name = '" . mysql_real_escape_string($_REQUEST['name']) . "',
telephone = '" . mysql_real_escape_string($_REQUEST['telephone']) . "',
address = '" . mysql_real_escape_string($_REQUEST['address']) . "'
or die(mysql_error())
");




if ($update)
{
?> <script type="text/javascript">
alert("Your profile already updated.");
history.back();

</script>
<?php

//THIS CODE IS UNCOMMENTED
//So that people without JS can still see the message.
die("Your profile already updated");
//END
}

}
?>


I am using action = "<? echo $HTTP_SERVER_VARS['PHP_SELF'] .
"?frame=profile"; ?>", which reflect the page(profile.php) itself.