|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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? |
|
#2
|
|||
|
|||
|
RE: Problem with row update
|
|
#3
|
|||
|
|||
|
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'");] ????? |
|
#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 |
|
#5
|
|||
|
|||
|
RE: Problem with row update
That did it...knew I was missing something. Thnx alot, youre a lifesaver.
|
![]() |
| Viewing: Codewalkers Forums > Other Technologies > Database Help > Problem with row update |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|
|