Solved

While loop update using php mysql

Posted on 2009-07-02
10
396 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
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
 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

685 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