SunQuest
           Database Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesDatabase Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old September 30th, 2003, 10:33 PM
mdhall mdhall is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 158 mdhall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Problem with row update

I'm trying to set up a script to be able to edit the fields of a database row. The update script I have is changing all of the info in all the rows instead of just one row. Heres what I'm using.
This is set up to change employee data in a mysql database.

The form data, passes an employee number and email address to "update.php".

<form action="update.php" method="post">
Employee Number
<input type=text size=10 name="empnumberedit">
Email address
<input type=text name="emailedit" size=30>

The update script.
[<?
include("dbinfo.inc.php");
$dbh=mysql_connect ("localhost", "$username", "$password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("$database");
$query=("SELECT * FROM contacts WHERE empnumber='$empnumberedit' AND email='$emailedit'");

$result=mysql_query($query);
$num=mysql_num_rows($result);
mysql_close();

$i=0;
while ($i < $num) {
$id=mysql_result($result,$i,"id");
$empnumber=mysql_result($result,$i,"empnumber");
$fname=mysql_result($result,$i,"fname");
$lname=mysql_result($result,$i,"lname");
$dept=mysql_result($result,$i,"dept");
$email=mysql_result($result,$i,"email");
$phone=mysql_result($result,$i,"phone");


?>

<form action="updated.php">
<input type="hidden" name="ud_id" value="<? echo "$id"; ?>">
<font face=verdana, times size=2><b>
Employee Number: <br>
<input type="text" name="ud_empnumber" value="<? echo" $empnumber"?>"><br>
First Name:<br>
<input type="text" name="ud_fname" value="<? echo "$fname"?>"><br>
Last Name:<br>
<input type="text" name="ud_lname" value="<? echo "$lname"?>"><br>
Department/Position:<br>
<input type="text" name="ud_dept" value="<? echo "$dept"?>"><br>
Email:<br>
<input type="text" name="ud_email" value="<? echo "$email"?>"><br>
Phone:<br>
<input type="text" name="ud_phone" value="<? echo "$phone"?>"><br>
<input type="Submit" value="Update">
</form></td>

<?
++$i;
}
?>]

And then "updated.php".
[<?
include("dbinfo.inc.php");
$dbh=mysql_connect ("localhost", "$username", "$password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("$database");

$query=("UPDATE contacts SET empnumber='$ud_empnumber', fname='$ud_fname', lname='$ud_lname', dept='$ud_dept', email='$ud_email', phone='$ud_phone'");
@mysql_select_db($database) or die( "Unable to select database");
mysql_query($query);
echo "Contact Info Updated";
mysql_close();
echo "<meta http-equiv="refresh" content="0; url=changed.html" />n";
?>]
Seems like I'm missing something, but I don't know what. Instead of updating just one row, it updates all of the rows, the only thing that doesnt change is the "id" field. Any ideas?

Reply With Quote
  #2  
Old October 1st, 2003, 07:12 AM
Ashkhan Ashkhan is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 372 Ashkhan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 24 m 57 sec
Reputation Power: 2
RE: Problem with row update

In your update query you must specify which row you want to update. You can do this in WHERE clause.

php Code:
Original - php Code
  1.  
  2. $query=("UPDATE contacts SET empnumber='$ud_empnumber', fname='$ud_fname', lname='$ud_lname', dept='$ud_dept', email='$ud_email', phone='$ud_phone' WHERE id=$someid");

Reply With Quote
  #3  
Old October 1st, 2003, 11:36 AM
mdhall mdhall is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 158 mdhall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: Problem with row update

I tried this, using single quotes and no quotes in the where statement, but now it isn't changing anything.

[ $query=("UPDATE contacts SET empnumber='$ud_empnumber', fname='$ud_fname', lname='$ud_lname', dept='$ud_dept', email='$ud_email', phone='$ud_phone' WHERE id='$id'");]

?????

Reply With Quote
  #4  
Old October 1st, 2003, 03:06 PM
Blindeddie Blindeddie is offline
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: NJ - USA
Posts: 2,152 Blindeddie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 4
RE: Problem with row update

the name of the hidden field that holds the id on the form is ud_id, so the query should look like this.

$query=("UPDATE contacts SET empnumber='$ud_empnumber', fname='$ud_fname', lname='$ud_lname', dept='$ud_dept', email='$ud_email', phone='$ud_phone' WHERE id='$ud_id'");]


you need to reference $ud_id in the where statement, not just $id

Reply With Quote
  #5  
Old October 1st, 2003, 03:15 PM
mdhall mdhall is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 158 mdhall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: Problem with row update

That did it...knew I was missing something. Thnx alot, youre a lifesaver.

Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesDatabase Help > Problem with row update


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway