Jump to content

MySQL query doesn't do anything.


iRoot121

Recommended Posts

Hi,

 

When I run an UPDATE query in my PHP code, nothing happends, no errors or anything. The weardest thing is, is that if I echo the query, it's the correct output. Any other queries that come after them are still executed.

 

This is my query code:

mysql_query("UPDATE users SET cash='".clean_string($info["cash"]-$_POST["bet"])."' WHERE login='".$info["login"]."'") or die(mysql_error());
mysql_query("UPDATE objecten SET bank='".clean_string($object["bank"]+$_POST["bet"])."' WHERE type='4' AND land='".$info["land"]."'") or die(mysql_error());

Thanks in advance,

iRoot121.

Link to comment
Share on other sites

your WHERE clause is likely false or the value being updated isn't valid for the field type.

 

which of those queries isn't working and it would be helpful if you posted the query that you echoed so that we could see it to see if there is anything obvious about it that could be causing the problem.

Link to comment
Share on other sites

your WHERE clause is likely false or the value being updated isn't valid for the field type.

 

which of those queries isn't working and it would be helpful if you posted the query that you echoed so that we could see it to see if there is anything obvious about it that could be causing the problem.

Both of the queries aren't working. And if I do:

echo "UPDATE users SET cash='".clean_string($info["cash"]-$_POST["bet"])."' WHERE login='".$info["login"]."'";
echo "UPDATE objecten SET bank='".clean_string($object["bank"]+$_POST["bet"])."' WHERE type='4' AND land='".$info["land"]."'";

I get UPDATE users SET cash='12000' WHERE login='Goed' AND UPDATE objecten SET bank='775807' WHERE type='4' AND land='1' as output, so the variables are set.

 

By the way, if I throw these outputs in PHPMyAdmin, the query works perfectly.

Link to comment
Share on other sites

you likely have some white-space/non-printing characters as part of the data. what does using var_dump() on each of the php variables - $info["cash"], $_POST["bet"], $info["login"], $object["bank"], and $info["land"] show (this is primary looking for the length to see if it matches the number of printing characters being displayed)?

Link to comment
Share on other sites

Numeric fields usually don't need quotes on the values.  That may cause the query to not be running.

 

Try this syntax:

 

 

$q = "update........ where.......";
echo "Query is <br>$q";
$qresults = MySQL_query($q);
if (!$qresults)
{
    echo "Error in query - msg is<br>" . MySQL_error();
    exit();
}

By assigning the query result to a variable you can check the success/failure of the query.

 

By assigning the query statement to a variable you can positively be sure that you are echoing the exact query statement.

Link to comment
Share on other sites

you likely have some white-space/non-printing characters as part of the data. what does using var_dump() on each of the php variables - $info["cash"], $_POST["bet"], $info["login"], $object["bank"], and $info["land"] show (this is primary looking for the length to see if it matches the number of printing characters being displayed)?

 

If I echo the var_dumps like this:

echo '$info["login"]: '.var_dump($info["login"]);
echo '$_POST["bet"]: '.var_dump($_POST["bet"]);
echo '$info["cash"]: '.var_dump($info["cash"]);
echo '$info["land"]: '.var_dump($info["land"]);

I get string(4) "Goed" $info["login"]: string(4) "1000" $_POST["bet"]: string(5) "14000" $info["cash"]: string(1) "1" $info["land"]: as output. Do you need any more information?

Link to comment
Share on other sites

Numeric fields usually don't need quotes on the values.  That may cause the query to not be running.

 

Try this syntax:

$q = "update........ where.......";
echo "Query is <br>$q";
$qresults = MySQL_query($q);
if (!$qresults)
{
    echo "Error in query - msg is<br>" . MySQL_error();
    exit();
}

By assigning the query result to a variable you can check the success/failure of the query.

 

By assigning the query statement to a variable you can positively be sure that you are echoing the exact query statement.

If I use that for both queries, I get:

Query is
UPDATE users SET cash='13000' WHERE login='Goed'

Query is
UPDATE objecten SET bank='9223372036854774807' WHERE type='4' AND land='1'

as output, no errors or whatever.

Link to comment
Share on other sites

Can you post all that code so we can see it too?

Do you mean the whole page? If so, do you want to have a pastebin, or should I just paste it here?

 

EDIT: Here is the code for the error handling:

$q = "UPDATE users SET cash='".clean_string($info["cash"]-$_POST["bet"])."' WHERE login='".$info["login"]."'";
echo "Query is <br>$q";
$qresults = MySQL_query($q);
if (!$qresults)
{
	echo "Error in query - msg is<br>" . MySQL_error();
	exit();
}
$q2 = "UPDATE objecten SET bank='".clean_string($object["bank"]+$_POST["bet"])."' WHERE type='4' AND land='".$info["land"]."'";
echo "Query is <br>$q2";
$qresults2 = MySQL_query($q2);
if (!$qresults2)
{
	echo "Error in query - msg is<br>" . MySQL_error();
	exit();
}

Link to comment
Share on other sites

here's a possibility, you are running that code a second time with an empty $_POST["bet"] value (or you have some other update code later in the program) and are updating the values back to the original values in $info["cash"] and $object["bank"]?

 

at this point (i.e. a page that's doing something unexplained), seeing all the code needed to reproduce the problem would lead to the quickest solution.

Link to comment
Share on other sites

here's a possibility, you are running that code a second time with an empty $_POST["bet"] value (or you have some other update code later in the program) and are updating the values back to the original values in $info["cash"] and $object["bank"]?

 

at this point (i.e. a page that's doing something unexplained), seeing all the code needed to reproduce the problem would lead to the quickest solution.

Yea, that could be possible. Here is the code if you want to take a look at it: http://pastebin.com/JDWZuf2b

The update query is on line 431

Link to comment
Share on other sites

How do you "know" the line with that query is even being run? There's no output associated with that line of code. So, what makes you think it should have executed? You state that the query isn't doing anything as if the query is not running on the MySQL side. I'm guessing it is not being executed within PHP. Instead of putting your query inside the actual query call, create the queries as string variables - makes it much easier to debug.

 

Anyway, modify line 431 to look like this

 

$query = "UPDATE users SET cash='".clean_string($info["cash"]-$_POST["bet"])."' WHERE login='".$info["login"]."'";
 mysql_query($query) or die(mysql_error());
echo "Debug: The following query was executed:<br>{$query}";

 

If you do not see that debug statement, then it means the logic in the code (if/else and other branching logic) is such that it will not be executed. If it does display, then we you should run a similar SELECT query (before and after the UPDATE) to see what records match that criteria and see what the 'cash' value is before and after.

Link to comment
Share on other sites

holy mixed html and code Batman. there's four different locations in that code that run those two update queries. at a minimum, you need to organize the logic so that the php code processing the form submission(s) comes first on the page, where those two queries would only exists once in the code. all the css, javascript and html markup should be separated from the main php logic and come at the end of the file.

Link to comment
Share on other sites

I've already got the solution: after the UPDATE query there was another query that said: update the cash of the user with the cash + an amount of cash. But the second query had the old cash value, not the updated one.

 

Thanks for helping anyway guys!

Link to comment
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.