hi
i am not able to update my table.plz do tell me the bug in my code.....
when update link is clicked in the first page..the table should get updated
page 1
$link=mysql_connect($hostname, $username, $password);
mysql_select_db($dbid) or die("unable to connect");

$x = $_GET;
$result=mysql_query("SELECT * FROM table1 where name='$x'") or die("ERROR:".mysql_error());
for ($i = 0; $i < mysql_num_rows($result); ++$i)
{
$x= mysql_fetch_row($result);

print 'Name<input type="text" maxlength="19" size="53" name="name" value="'.$x[0].'" />';
print "<a href='page2.php?name=".$x[0]."'>Update</a>";
}
mysql_close($link);


page 2

$link=mysql_connect($hostname, $username, $password);
mysql_select_db($dbid) or die("unable to connect");
$name1 = $_GET;
mysql_query("UPDATE table1 SET name='$name' where name='$name1'") or die("ERROR:".mysql_error());
echo "thanks";
mysql_close($link);

Recommended Answers

All 4 Replies

i think it's here:

mysql_query("UPDATE table1 SET name='$name' where name='$name1'")

should be:

mysql_query("UPDATE table1 SET name='$name1' where name='$name1'")

now there is no bug but it is not getting updated

I think this will help you:

$link=mysql_connect($hostname, $username, $password);
mysql_select_db($dbid,$link) or die("unable to connect");

$x = $_GET;
$result=mysql_query("SELECT * FROM table1 where name='$x'") or die("ERROR:".mysql_error());

$x=mysql_fetch_array($result);
if($x[0]!=null){

echo $x[0];
or,
printf($x[0]);
}

then write the update code .

hi
i am not able to update my table.plz do tell me the bug in my code.....
when update link is clicked in the first page..the table should get updated
page 1
$link=mysql_connect($hostname, $username, $password);
mysql_select_db($dbid) or die("unable to connect");

$x = $_GET;
$result=mysql_query("SELECT * FROM table1 where name='$x'") or die("ERROR:".mysql_error());
for ($i = 0; $i < mysql_num_rows($result); ++$i)
{
$x= mysql_fetch_row($result);

print 'Name<input type="text" maxlength="19" size="53" name="name" value="'.$x[0].'" />';
print "<a href='page2.php?name=".$x[0]."'>Update</a>";
}
mysql_close($link);


page 2

$link=mysql_connect($hostname, $username, $password);
mysql_select_db($dbid) or die("unable to connect");
$name1 = $_GET;
mysql_query("UPDATE table1 SET name='$name' where name='$name1'") or die("ERROR:".mysql_error());
echo "thanks";
mysql_close($link);

why would it update when you are not changing anything ? You should have a form to make the changes. Here, you are just fetching the record from the table, putting it in a textbox, then giving a link with the same value. Try this instead.

<?php  //page1.php
$link=mysql_connect($hostname, $username, $password);
mysql_select_db($dbid) or die("unable to connect");
$x = $_GET['name'];
$result=mysql_query("SELECT * FROM table1 where name='$x'") or die("ERROR:".mysql_error());
$row=mysql_fetch_array($result,MYSQL_ASSOC); //since it returns only 1 row.
print '<form method="POST" action="page2.php">';
print '<input type="hidden" name="oldname" value="'.$x.'">';
print 'Name<input type="text" maxlength="19" size="53" name="name" value="'.$row['name'].'" />';
print '<input type="submit" name="submit" value="Update">';
mysql_close($link);
?>

<?php //page2.php
$link=mysql_connect($hostname, $username, $password);
mysql_select_db($dbid) or die("unable to connect");
$old_name=$_POST['oldname'];
$new_name=$_POST['name'];
mysql_query("UPDATE table1 SET name='$new_name' where name='$old_name'") or die("ERROR:".mysql_error());
echo "thanks";
mysql_close($link);
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.