?
Solved

While loop update using php mysql

Posted on 2009-07-02
10
Medium Priority
?
407 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

752 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