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
https://forums.phpfreaks.com/topic/214461-updating-query/
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

Link to comment
https://forums.phpfreaks.com/topic/214461-updating-query/#findComment-1116304
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
https://forums.phpfreaks.com/topic/214461-updating-query/#findComment-1116363
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
https://forums.phpfreaks.com/topic/214461-updating-query/#findComment-1116372
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
https://forums.phpfreaks.com/topic/214461-updating-query/#findComment-1116380
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
https://forums.phpfreaks.com/topic/214461-updating-query/#findComment-1116400
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
https://forums.phpfreaks.com/topic/214461-updating-query/#findComment-1116405
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
https://forums.phpfreaks.com/topic/214461-updating-query/#findComment-1116410
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.