Solved

While loop update using php mysql

Posted on 2009-07-02
10
388 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

707 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

16 Experts available now in Live!

Get 1:1 Help Now