iRoot121 Posted April 23, 2014 Share Posted April 23, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/287973-mysql-query-doesnt-do-anything/ Share on other sites More sharing options...
mac_gyver Posted April 23, 2014 Share Posted April 23, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/287973-mysql-query-doesnt-do-anything/#findComment-1477091 Share on other sites More sharing options...
iRoot121 Posted April 23, 2014 Author Share Posted April 23, 2014 (edited) 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. Edited April 23, 2014 by iRoot121 Quote Link to comment https://forums.phpfreaks.com/topic/287973-mysql-query-doesnt-do-anything/#findComment-1477093 Share on other sites More sharing options...
mac_gyver Posted April 23, 2014 Share Posted April 23, 2014 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)? Quote Link to comment https://forums.phpfreaks.com/topic/287973-mysql-query-doesnt-do-anything/#findComment-1477095 Share on other sites More sharing options...
ginerjm Posted April 23, 2014 Share Posted April 23, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/287973-mysql-query-doesnt-do-anything/#findComment-1477098 Share on other sites More sharing options...
iRoot121 Posted April 23, 2014 Author Share Posted April 23, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/287973-mysql-query-doesnt-do-anything/#findComment-1477102 Share on other sites More sharing options...
iRoot121 Posted April 23, 2014 Author Share Posted April 23, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/287973-mysql-query-doesnt-do-anything/#findComment-1477103 Share on other sites More sharing options...
ginerjm Posted April 23, 2014 Share Posted April 23, 2014 Can you post all that code so we can see it too? Quote Link to comment https://forums.phpfreaks.com/topic/287973-mysql-query-doesnt-do-anything/#findComment-1477107 Share on other sites More sharing options...
iRoot121 Posted April 23, 2014 Author Share Posted April 23, 2014 (edited) 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(); } Edited April 23, 2014 by iRoot121 Quote Link to comment https://forums.phpfreaks.com/topic/287973-mysql-query-doesnt-do-anything/#findComment-1477109 Share on other sites More sharing options...
mac_gyver Posted April 23, 2014 Share Posted April 23, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/287973-mysql-query-doesnt-do-anything/#findComment-1477110 Share on other sites More sharing options...
iRoot121 Posted April 23, 2014 Author Share Posted April 23, 2014 (edited) 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 Edited April 23, 2014 by iRoot121 Quote Link to comment https://forums.phpfreaks.com/topic/287973-mysql-query-doesnt-do-anything/#findComment-1477112 Share on other sites More sharing options...
Psycho Posted April 23, 2014 Share Posted April 23, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/287973-mysql-query-doesnt-do-anything/#findComment-1477117 Share on other sites More sharing options...
mac_gyver Posted April 23, 2014 Share Posted April 23, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/287973-mysql-query-doesnt-do-anything/#findComment-1477122 Share on other sites More sharing options...
Solution iRoot121 Posted April 23, 2014 Author Solution Share Posted April 23, 2014 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! Quote Link to comment https://forums.phpfreaks.com/topic/287973-mysql-query-doesnt-do-anything/#findComment-1477124 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.