Jump to content

UPDATE statement not updating?


jmrothermel

Recommended Posts

Ok I have a few commands going on at once.  All seem to be executing except the first UPDATE. 

$update = mysql_query("UPDATE members SET money='$new' WHERE username='$username'");

Could anyone tell me why that might be?  Here is the code I am using.

 

if($a == 'feed') {	
$user = $_GET['$username'];
$date = date("y-m-d");

$check= mysql_query("SELECT * FROM pets WHERE `user`='$username'");
$check_num= mysql_num_rows($check);

if ($check_num == "0"){
print "Record could not be found";
}else{
$sql = mysql_query("SELECT `money` FROM `members` WHERE `username`='$username'");
$new = $sql - 5000;			
$update = mysql_query("UPDATE members SET money='$new' WHERE username='$username'");
$new2 = $MONEY + 5000;
$insert=mysql_query("INSERT INTO `petsfed` SET `user`='$u', `fed` = '$username', `date` = '$date'");
$update2=mysql_query("UPDATE `members` SET `money`='$new2' WHERE `username`='$u'"); 
$insert2=mysql_query("UPDATE `pets` SET `lastfed`='$date' WHERE `user`='$username'") or die (mysql_error());
if($insert && $insert2 && $update && $update2) { printf ("Pets Fed: %d\n", mysql_affected_rows());

Link to comment
Share on other sites

change it to this and see if it displays any errors for you:

 

$update = mysql_query("UPDATE members SET money='$new' WHERE username='$username'") or die("Update error: ".mysql_error());

 

EDIT: Also, use use $user on the second line, and $username in the rest of the code. Should line 2 be $username?

Link to comment
Share on other sites

$sql = mysql_query("SELECT `money` FROM `members` WHERE `username`='$username'");

$new = $sql - 5000;

$update = mysql_query("UPDATE members SET money='$new' WHERE username='$username'");

 

$sql will be a resourceid. you need to do a mysql_fetch_array or mysql_fetch_rows first to get the actual value of money

Link to comment
Share on other sites

silly me...i can't believe i missed this...

 

$sql = mysql_query("SELECT `money` FROM `members` WHERE `username`='$username'");

the above returns a mysql resource...not the actual value from the money field...you still have to select that from the resource. here is some updated code...although i still don't understand some parts of it (see my comments in the code)

<?php
if($a == 'feed') {  
  $username = $_GET['$username']; //Updated to be $username
  $date = date("y-m-d");

  $check = mysql_query("SELECT * FROM `pets` WHERE `user`='$username'") or die("MySQL Error: ".mysql_error());
  if(!mysql_num_rows($check)){ //Shortened this up a bit
    print "Record could not be found";
  }else{
    $sql = mysql_query("SELECT `money` FROM `members` WHERE `username`='$username'") or die("MySQL Error: ".mysql_error());
    $member = mysql_fetch_assoc($sql);
    if(!$member){
      print "Could not find member";
    }else{
      //Update member's money
      $new = $member['money'] - 5000;     
      $update = mysql_query("UPDATE `members` SET `money`='$new' WHERE `username`='$username'");
      
      $new2 = $MONEY + 5000; //WHERE is $MONEY coming from???
      //WHERE is $u coming from???
      $insert=mysql_query("INSERT INTO `petsfed` SET `user`='$u', `fed` = '$username', `date` = '$date'") or die("MySQL Error: ".mysql_error());
      $update2=mysql_query("UPDATE `members` SET `money`='$new2' WHERE `username`='$u'") or die("MySQL Error: ".mysql_error()); 
      $insert2=mysql_query("UPDATE `pets` SET `lastfed`='$date' WHERE `user`='$username'") or die("MySQL Error: ".mysql_error());
      if($insert && $insert2 && $update && $update2) { 
        printf ("Pets Fed: %d\n", mysql_affected_rows());
      }
    }
  }
}
?>

 

 

p.s. - try to start indenting stuff, it makes it WAY easier to read

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.