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
Solved

While loop update using php mysql

Posted on 2009-07-02
10
395 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Very Large data in MYSQL 7 73
php help 34 58
PHP and MSSQL Arrays and Variables 3 23
Log in through ID 5 17
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

809 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