Jump to content

Updating query


dean7

Recommended Posts

Hi all, for my website ive got a fuel station which ive coded so users can fill up there there car fuel tanks, but the code all works part from one part of it.

 

The code:

 

The part of the code which isnt working:

<?php
$usermoney = $fetch->money;
$costs2 = $fetch_fuelstation->price * $fueldiff;
if ($costs2 > $usermoney){
echo "You dont have enouth money.";
} elseif ($costs2 <= $usermoney){
$nmoney = $usermoney - $costs2;

mysql_query("UPDATE users SET money='$nmoney' WHERE username='$username'") or die (mysql_error());
}
?>	

Ive got a varibal called $usermoney which selects the users current money from the database, but what im trying todo is take the fuel price away from there current money, but its not subtracting the money as it should.

 

Anyone see why its not taking the money?

Thanks.

Link to comment
Share on other sites

There are no errors generated, right?

 

What is the data type of the of the `money` field in the database? Is it DECIMAL, FLOAT, VARCHAR, or what?

 

If it's a numeric field type, try the query without the quotes around $nmoney, i.e. SET money = $nmoney

Sorry about late reply but its just a VARCHAR field in the database.

Link to comment
Share on other sites

Change your query execution to not include the query string. Store it in a variable, temporarily comment out the execution and echo the variable to see if it looks right.

 

$query = "UPDATE users SET money='$nmoney' WHERE username='$username'";
//mysql_query($query) or die (mysql_error());
echo $query;

Link to comment
Share on other sites

Change your query execution to not include the query string. Store it in a variable, temporarily comment out the execution and echo the variable to see if it looks right.

 

$query = "UPDATE users SET money='$nmoney' WHERE username='$username'";
//mysql_query($query) or die (mysql_error());
echo $query;

Ah thanks :), for doing that its manged to give me an error which could be good:

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #11' at line 1

 

But how would I get around this error?

 

 

Link to comment
Share on other sites

Change your query string to this:

$query = "UPDATE users SET money='".$nmoney."' WHERE username='".$username."'";

 

There is no difference between this query and the one he already had.  This has nothing to do with his problem.  Pikachu2000 already identified the issue.

Link to comment
Share on other sites

Change your query string to this:

$query = "UPDATE users SET money='".$nmoney."' WHERE username='".$username."'";

 

There is no difference between this query and the one he already had.  This has nothing to do with his problem.  Pikachu2000 already identified the issue.

I've had problems using the syntax he's using, so that is why I suggested changing the string to what I posted.

Link to comment
Share on other sites

Change your query string to this:

$query = "UPDATE users SET money='".$nmoney."' WHERE username='".$username."'";

 

There is no difference between this query and the one he already had.  This has nothing to do with his problem.  Pikachu2000 already identified the issue.

I've had problems using the syntax he's using, so that is why I suggested changing the string to what I posted.

 

I'm sorry to be harsh, but that's poor advice.  I can only suggest that you really study PHP "interpolation".  The only issue with interpolation is that you can not include array variables using single quotes around the key, but even this has a workaround.

 

 

$r['day'] = 'monday';

echo "today is $r['day']";
// syntax error

echo "today is $r[day]";
// works but requires constant lookup

echo "today is {$r['day']}";
// works 

 

Speaking personally, it is both easier to read and easier to maintain interpolated strings in my opinion.

 

Having to concatenate every single variable is a lot of work, and a major headache when you have to alter queries. 

 

 

Link to comment
Share on other sites

Either $username or $nmoney contains a query result resource instead of a valid value. You'll need to figure out which one, and why.

I blive that it is $nmoney which contains the resource instead of a valid value, but how would I sort that to contain a valid value?

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.