Solved

While loop update using php mysql

Posted on 2009-07-02
10
392 Views
Last Modified: 2013-12-13
Im trying to update two records stored in a table, im trying to get the while loop to update assigned and agent_id fields - unfortunately it updates all the records in the database with the same $agent_id variable value.

Would love some advice :)
<?php 
 

$i = 1;
 

$facebook = 2;
 
 

$agent_id = 3;
 
 

if ( $facebook >= 1) {
 

while ( $i <= $facebook) {
 
 

// Update current DB

    $sql_update = "UPDATE facebook_user SET assigned = '1', agent_id = '$agent_id'  WHERE agent_id = '75'";

    mysql_query($sql_update) or die(mysql_error());

		

 $i++; 
 

} 
 

}
 

?>

Open in new window

0
Comment
Question by:agulaid
  • 4
  • 3
  • 3
10 Comments
 
LVL 10

Expert Comment

by:mahome
ID: 24761576
I dont't know what you exactly want to do but you only have the agent_id in the where clause. I think you also need the i or any second parameter you want.
0
 

Author Comment

by:agulaid
ID: 24761596
UPDATE facebook_user SET assigned = '1', agent_id = '$agent_id'  WHERE agent_id = '75'"
UPDATE facebook_user SET assigned = '1', agent_id = '$agent_id'
  WHERE agent_id = '75'
"

Open in new window

0
 

Author Comment

by:agulaid
ID: 24761607
the agent_id is before WHERE clause also.
0
 
LVL 10

Expert Comment

by:mahome
ID: 24761624
??? That is the same query as above ???

What attributes do the two rows have you want to update?
You are always updating all rows that match the where clause, in your case all with agent_id = 75.
0
 

Author Comment

by:agulaid
ID: 24761656
I would like to limit the updates to the value of $facebook, in this case only update 2 records in the database with an agent_id of 3.
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 10

Accepted Solution

by:
mahome earned 500 total points
ID: 24761830
>> limit the updates to the value of $facebook
which colum stores the facebook value?

You need something like this

$sql_update = 

"UPDATE facebook_user SET assigned = '1', agent_id = '$agent_id' " . 

" WHERE agent_id = '75' and anotherColumn= '$i'";

Open in new window

0
 
LVL 5

Expert Comment

by:kingofnines
ID: 24765182
"only update 2 records in the database with an agent_id of 3."

Then your where clause needs to reflect that by limiting to records with agent_id = '3'. Not 75...

$sql_update = "UPDATE facebook_user SET assigned = '1', agent_id = '$agent_id'  WHERE agent_id = '3'";



Alan
0
 
LVL 5

Expert Comment

by:kingofnines
ID: 24765228
Wait, why are you assigning $agent_id to 3 before the query is executed? On one hand you are saying you want only records with agent_id = 3 to be updated. Then on the other hand you are hard coding $agent_id to 3 which causes all records to be updated. I'm not sure what you need at this point.


Alan
0
 

Author Comment

by:agulaid
ID: 24765373
I've managed to resolve it thanks:



$facebook = 2;
 
 

$agent_id = 5;
 
 

if ( $facebook >= 1) {
 

$i = 1;
 

while ( $i <= $facebook) {
 
 

// Update current DB

    $sql_update = "UPDATE facebook_user SET assigned = '1', agent_id = '$agent_id'  WHERE agent_id = '3'";

    $run_query = mysql_query($sql_update) or die(mysql_error());

		

 $i++; 
 

} 
 

}

Open in new window

0
 
LVL 5

Expert Comment

by:kingofnines
ID: 24765405
your query is not fixed. By setting $agent_id = 5 and then saying $agent_id = 3 in the where clause, no records will ever be updated.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now