Jump to content


Photo

why these php codes doen't work?


  • Please log in to reply
5 replies to this topic

#1 shams

shams
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 08 May 2006 - 02:42 AM

hi,
these are php codes to update the mysql enteries, when i run them the final updated.php message is "Welcome Done" but when i check the mysql table home it is not updated, this is the /var/log/httpd/error_log message:
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
[Sun May 07 08:09:53 2006] [error] [client 127.0.0.1] PHP Notice: Undefined variable: ud_id in /var/www/html/tbp/test/updated.php on line 9, referer: [a href=\"http://127.0.0.1/tbp/test/update.php\" target=\"_blank\"]http://127.0.0.1/tbp/test/update.php[/a]
[/quote]

this is the update.php:
<html>
<body>
<?php
$id = $_POST['id'];
$name = $_POST['name'];
mysql_connect("localhost", "root") or die(mysql_error());
mysql_select_db("mydb") or die(mysql_error());

$query = "SELECT * FROM home WHERE id='$id' or name='$name'";
$result = mysql_query($query);
$num = mysql_numrows($result);

$i=0;
while ($i < $num) {
$id = mysql_result($result,$i,"id");
$name = mysql_result($result,$i,"name");
$address = mysql_result($result,$i,"address");
?>
[!--html--][div class=\'htmltop\']HTML[/div][div class=\'htmlmain\'][!--html1--]<form action="updated.php" method="POST">
<input type="hidden" name="ud_id" value="<? echo $id; ?>">
Name: <input type="text" name="ud_name" value="<? echo $name; ?>"><br>
Address: <input type="text" name="ud_address" value="<? echo $address; ?>"><br>
<input type="submit" value="UPDATE"> </form> <?php ++$i; } ?>
</body>
</html>
[!--html2--][/div][!--html3--]

and this is the updated.php:
<?php
$id = $_POST['ud_id'];
$name = $_POST['ud_name'];
$address = $_POST['ud_address'];
mysql_connect("localhost", "root") or die(mysql_error());
mysql_select_db("mydb") or die(mysql_error());

$query = "UPDATE home SET name='$ud_name', address='$ud_address' WHERE id='$ud_id'";

$rt = mysql_query($query);
if($rt) { echo "Welcome Done";}
else { echo "sorry";}
?> 
please any one can help?




#2 darga333

darga333
  • Members
  • PipPipPip
  • Advanced Member
  • 53 posts

Posted 08 May 2006 - 02:46 AM

[!--quoteo(post=372172:date=May 7 2006, 10:42 PM:name=shams)--][div class=\'quotetop\']QUOTE(shams @ May 7 2006, 10:42 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
hi,
these are php codes to update the mysql enteries, when i run them the final updated.php message is "Welcome Done" but when i check the mysql table home it is not updated, this is the /var/log/httpd/error_log message:
this is the update.php:
<html>
<body>
<?php
$id = $_POST['id'];
$name = $_POST['name'];
mysql_connect("localhost", "root") or die(mysql_error());
mysql_select_db("mydb") or die(mysql_error());

$query = "SELECT * FROM home WHERE id='$id' or name='$name'";
$result = mysql_query($query);
$num = mysql_numrows($result);

$i=0;
while ($i < $num) {
$id = mysql_result($result,$i,"id");
$name = mysql_result($result,$i,"name");
$address = mysql_result($result,$i,"address");
?>
[!--html--][div class=\'htmltop\']HTML[/div][div class=\'htmlmain\'][!--html1--]<form action="updated.php" method="POST">
<input type="hidden" name="ud_id" value="<? echo $id; ?>">
Name: <input type="text" name="ud_name" value="<? echo $name; ?>"><br>
Address: <input type="text" name="ud_address" value="<? echo $address; ?>"><br>
<input type="submit" value="UPDATE"> </form> <?php ++$i; } ?>
</body>
</html>[!--html2--][/div][!--html3--]

and this is the updated.php:
<?php
$id = $_POST['ud_id'];
$name = $_POST['ud_name'];
$address = $_POST['ud_address'];
mysql_connect("localhost", "root") or die(mysql_error());
mysql_select_db("mydb") or die(mysql_error());

$query = "UPDATE home SET name='$ud_name', address='$ud_address' WHERE id='$ud_id'";

$rt = mysql_query($query);
if($rt) { echo "Welcome Done";}
else { echo "sorry";}
?> 
please any one can help?
[/quote]


change this

$query = "UPDATE home SET name='$ud_name', address='$ud_address' WHERE id='$ud_id'";

to this
$query = "UPDATE home SET name='$name', address='$address' WHERE id='$id'";

:)

you can either do that or you can set the variables equal to the $_GET['ud_name'], $_GET['ud_address'], and $_GET['ud_id'] respectively

#3 shams

shams
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 08 May 2006 - 02:55 PM

thanks fro reply i tired the both methods but no one wrok.

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 08 May 2006 - 03:27 PM

Replace updated.php with this version and be sure to upload it to your server. Then test and let us know what error message you get.

Things to check: database table name and field names exactly match those used in the query, including case.

<?php
$ud_id = $_POST['ud_id'];
$name = $_POST['ud_name'];
$address = $_POST['ud_address'];
mysql_connect("localhost", "root") or die(mysql_error());
mysql_select_db("mydb") or die(mysql_error());

$query = "UPDATE home SET name='$ud_name', address='$ud_address' WHERE id='$ud_id'";

$rt = mysql_query($query) or die("Error: ". mysql_error(). "<br>with query ". $query);
if($rt) { 
    echo "Welcome Done";
} else { 
    echo "sorry";
}
?>

Legend has it that reading the manual never killed anyone.
My site

#5 avo

avo
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts
  • Locationstaffordshire uk

Posted 08 May 2006 - 03:30 PM

HI

Try This :

$query = UPDATE home SET name = '".$_POST['ud_name']."', address = '".$_POST['ud_address']."' WHERE id = '".$_POST['ud_id']."'";

There should be no need for you to set the variables aswell this way .post directly into the update statement.

another check you could do is to echo $query somewhere on your page once you press the submit button on your form it will echo the query statement you can then check if the correct information is been provoided to the update statement. as it is echoed out on your page.


Hope it helps.
Im loving it ........

#6 shams

shams
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 08 May 2006 - 11:35 PM

thanks for replies i tired the both methods the ouput was done but nothing was changed, i finally got the correct code that worked for me this is:
<?php
$ud_id = $_POST['ud_id'];
$ud_name = $_POST['ud_name'];
$ud_address = $_POST['ud_address'];
mysql_connect("localhost", "root") or die(mysql_error());
mysql_select_db("mydb") or die(mysql_error());

$query = "UPDATE home SET name='$ud_name', address='$ud_address' WHERE id='$ud_id' or name='$ud_name'";





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users