Forum Moderators: coopster

Message Too Old, No Replies

UPDATE is updating too many rows.

The UPDATE function works but it is updating everything instead of just 1

         

dwbiz05

2:06 pm on Aug 2, 2008 (gmt 0)

10+ Year Member



Here is the problem. I want to show records in order, one each time the page is loaded.
I use the "credits" column to do this. If the number is 0 it can be shown. once it is shown it is changed to 1 so it will not be shown.
if there is none to show, it changes them all back to 0 and it all starts over again.

So, I do a search for an active record (some records are not active) that has 0 in the credits column.

That works great. However, when it updates the credits, it updates all the active records, not just the one specified in the UPDATE query.

each record has a unique "mid" that I used to try to make sure it only updates that one record but it is not working.

Any help would be great. here is the code:


<?
include ("config/db_config.php");

$found_ad = mysql_query("SELECT * FROM jx_members WHERE status = 'active' and credits = 0 LIMIT 1");
if (found_ad){
while ($found = mysql_fetch_array($found_ad)){
$dude = $found['mid'];
$url = $found['custom_field_value_1'];
$pic = $found['custom_field_value_2'];
}
$add_cred_now = mysql_query("UPDATE jx_members SET credits = 1 WHERE mid = '".$dude."' LIMIT 1");
if($add_cred_now){
?>
Display here works fine...
<?
}
}
else {
$all_done = mysql_query("UPDATE jx_members SET credits = 0 WHERE status = 'active'");
if($all_done){
$found_ad = mysql_query("SELECT * FROM jx_members WHERE status = 'active' and credits = 0 LIMIT 1");
while ($found = mysql_fetch_array($found_ad)){
$dude = $found['mid'];
$url = $found['custom_field_value_1'];
$pic = $found['custom_field_value_2'];
}
$add_cred_now = mysql_query("UPDATE jx_members SET credits = 1 WHERE mid = '".$dude."' LIMIT 1");
if($add_cred_now){
?>
Display here works fine...
<?
}
}
else {
echo "Error 5546";
}
}
?>

cameraman

7:08 pm on Aug 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, dwbiz05.

I think most of your problem is here on your third line:
if (found_ad){

You need a dollar sign in front of the variable name.
While you're debugging a script you might find it helpful to have at the top:
error_reporting(E_ALL);

Which causes it to report errors like that one.

Not a problem but as a side note, when you're only retrieving one record you don't need a while loop:
while ($found = mysql_fetch_array($found_ad)){

becomes simply:
$found = mysql_fetch_array($found_ad);

dwbiz05

3:17 pm on Aug 4, 2008 (gmt 0)

10+ Year Member



That was a problem. However, I have fixed those issues.

It is still working incorrectly.
If I remove the html (where it says "Display here works fine") it appears to work correctly. Don't know why that would be.