Problem with UPDATE in php
What is wrong with my code? I can display text from my database but the update is not working..
index.php
$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,"FirstName");
$last=mysql_result($result,$i,"LastName");
$age=mysql_result($result,$i,"Age");
echo "<b>$first $last</b><br>Age: $age<br>";
$i++;
}
$query="SELECT * FROM persons WHERE personID='$id'";
$resulta=mysql_query($query);
$list=mysql_numrows($resulta);
$i=0;
while ($i < $list) {
$first=mysql_result($resulta,$i,"FirstName");
$last=mysql_re开发者_C百科sult($resulta,$i,"LastName");
$age=mysql_result($resulta,$i,"Age");
++$i;
}
?>
<form action="update.php">
<input type="hidden" name="ud_id" value="<?php echo "$id"; ?>">
First Name: <input type="text" name="ud_first" value="<?php echo "$first"?>"><br>
Last Name: <input type="text" name="ud_last" value="<?php echo "$last"?>"><br>
Age: <input type="text" name="ud_age" value="<?php echo "$age"?>"><br>
<input type="Submit" value="Update">
</form>
update.php
<?php
include_once('config.php');
$query="UPDATE persons SET FirstName='$ud_first', LastName='$ud_last', Age='$ud_age' WHERE personID='$ud_id'";
mysql_query($query);
?>
I thik the problem is http://lt.php.net/manual/en/security.globals.php
so you cant use $ud_id
you have to do it as it shoud be with $_POST['ud_id']
HTML:
<form action="update.php" method="post">
PHP:
$ud_id = mysql_real_escape_string($_POST['ud_id']);
...
I think the only things you forgot is the fetching of id in your while function, and the getting of that id in which you need to use $_GET in the update.php I hope this gives you a little head start.
for index.php
$i=0;
while ($i < $list)
{
$id =mysql_result($resulta,$i,"id");
$first=mysql_result($resulta,$i,"FirstName");
$last=mysql_result($resulta,$i,"LastName");
$age=mysql_result($resulta,$i,"Age");
++$i;
}
for update.php
include_once('config.php');
$ud_id = $_GET['id'];
$query="UPDATE persons SET FirstName='$ud_first', LastName='$ud_last', Age='$ud_age' WHERE personID='$ud_id'";
if(!$query)
{
die('Could not delete data: ' . mysql_error());
}
mysql_close($link);
精彩评论