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:
  #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: 3
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: 3
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: 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'");]

?????

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: 5
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: 3
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




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

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




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 8 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek