User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 455,974 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,766 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 2008 | Replies: 4 | Solved
Reply
Join Date: Nov 2007
Posts: 99
Reputation: kings is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
kings's Avatar
kings kings is offline Offline
Junior Poster in Training

update sql

  #1  
Nov 28th, 2007
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['name'];
$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['name'];
mysql_query("UPDATE table1 SET name='$name' where name='$name1'") or die("ERROR:".mysql_error());
echo "thanks";
mysql_close($link);
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2007
Location: Cavite,Philippines
Posts: 508
Reputation: ryan_vietnow is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 68
ryan_vietnow's Avatar
ryan_vietnow ryan_vietnow is offline Offline
Posting Pro

Re: update sql

  #2  
Nov 28th, 2007
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'")
"death is the cure of all diseases..."
http://ryantetek.wordpress.com
Reply With Quote  
Join Date: Nov 2007
Posts: 99
Reputation: kings is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
kings's Avatar
kings kings is offline Offline
Junior Poster in Training

Re: update sql

  #3  
Nov 28th, 2007
now there is no bug but it is not getting updated
Reply With Quote  
Join Date: Aug 2007
Posts: 10
Reputation: ruman_eee is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
ruman_eee ruman_eee is offline Offline
Newbie Poster

Troubleshooting Re: update sql

  #4  
Dec 4th, 2007
I think this will help you:

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

$x = $_GET['name'];
$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 .

Originally Posted by kings View Post
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['name'];
$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['name'];
mysql_query("UPDATE table1 SET name='$name' where name='$name1'") or die("ERROR:".mysql_error());
echo "thanks";
mysql_close($link);
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: update sql

  #5  
Dec 4th, 2007
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.

  1. <?php //page1.php
  2. $link=mysql_connect($hostname, $username, $password);
  3. mysql_select_db($dbid) or die("unable to connect");
  4. $x = $_GET['name'];
  5. $result=mysql_query("SELECT * FROM table1 where name='$x'") or die("ERROR:".mysql_error());
  6. $row=mysql_fetch_array($result,MYSQL_ASSOC); //since it returns only 1 row.
  7. print '<form method="POST" action="page2.php">';
  8. print '<input type="hidden" name="oldname" value="'.$x.'">';
  9. print 'Name<input type="text" maxlength="19" size="53" name="name" value="'.$row['name'].'" />';
  10. print '<input type="submit" name="submit" value="Update">';
  11. mysql_close($link);
  12. ?>
  13.  
  14. <?php //page2.php
  15. $link=mysql_connect($hostname, $username, $password);
  16. mysql_select_db($dbid) or die("unable to connect");
  17. $old_name=$_POST['oldname'];
  18. $new_name=$_POST['name'];
  19. mysql_query("UPDATE table1 SET name='$new_name' where name='$old_name'") or die("ERROR:".mysql_error());
  20. echo "thanks";
  21. mysql_close($link);
  22. ?>
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 9:18 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC