TeamCIT Posted February 2, 2010 Share Posted February 2, 2010 I was just wondering if anyone knows if it's possible to use a PHP variable in this piece of code: mysql_query("update destinations set selected=1 where id={$destChose}"); Instead of changing the value of the "selected" column to 1, I would like it to use a variable acting as a counter to update the value in the "selected" column however I am running into problems when I try to get this to work with this code: $orderCounter = 0; if($_POST['submitted'] == true) { $destChose = $_POST['destList']; if($destChose >= 1) { mysql_query("update destinations set selected='$orderCounter' where id={$destChose}"); $orderCounter++; $result = mysql_query("SELECT dest_name, dest_addr FROM destinations WHERE selected >= '1'"); while ($row = mysql_fetch_array($result)) { echo "<li>"; echo $row["dest_name"]; echo "</li>"; } Any tips or pointers would be greatly appreciated, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/190674-use-a-php-variable-in-a-mysql-query/ Share on other sites More sharing options...
premiso Posted February 2, 2010 Share Posted February 2, 2010 I was just wondering if anyone knows if it's possible to use a PHP variable in this piece of code:. Yes, it is possible. Quote Link to comment https://forums.phpfreaks.com/topic/190674-use-a-php-variable-in-a-mysql-query/#findComment-1005566 Share on other sites More sharing options...
TeamCIT Posted February 2, 2010 Author Share Posted February 2, 2010 I was just wondering if anyone knows if it's possible to use a PHP variable in this piece of code:. Yes, it is possible. Thank you for your input, do you see a reason why the code I am using would not be working properly? Quote Link to comment https://forums.phpfreaks.com/topic/190674-use-a-php-variable-in-a-mysql-query/#findComment-1005568 Share on other sites More sharing options...
coupe-r Posted February 2, 2010 Share Posted February 2, 2010 Try mysql_query("update destinations SET selected= '".orderCounter."' WHERE id="); Quote Link to comment https://forums.phpfreaks.com/topic/190674-use-a-php-variable-in-a-mysql-query/#findComment-1005581 Share on other sites More sharing options...
TeamCIT Posted February 2, 2010 Author Share Posted February 2, 2010 Try mysql_query("update destinations SET selected= '".orderCounter."' WHERE id="); I tried this but I am getting a parse error. I tried it without the "."s and without the " ' "s too. Any other suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/190674-use-a-php-variable-in-a-mysql-query/#findComment-1005589 Share on other sites More sharing options...
mikesta707 Posted February 2, 2010 Share Posted February 2, 2010 whats the parse error? What line? can you show the code as it stands now something as simple as $sql = "update ... SET selected = $var WHERE ...."; should work Quote Link to comment https://forums.phpfreaks.com/topic/190674-use-a-php-variable-in-a-mysql-query/#findComment-1005590 Share on other sites More sharing options...
coupe-r Posted February 2, 2010 Share Posted February 2, 2010 Try mysql_query("update destinations SET selected= '".orderCounter."' WHERE id="); I meant to write: mysql_query("update destinations SET selected= '".$orderCounter."' WHERE id= '".$destChose."'"); I tried this but I am getting a parse error. I tried it without the "."s and without the " ' "s too. Any other suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/190674-use-a-php-variable-in-a-mysql-query/#findComment-1005597 Share on other sites More sharing options...
Hussam Posted February 2, 2010 Share Posted February 2, 2010 I Think the problem is with the ' around the variable $orderCounter in the SQL statement because its an integer not a string. Also you can't use {} within SQL statement, you don't need that anyway, if you need it (which is not gonna happen ever, I think) you can use it but then you need to concatenate strings and keep it out of the quotes. Try this: mysql_query("UPDATE destinations SET selected= $orderCounter WHERE id = $destChose"); Quote Link to comment https://forums.phpfreaks.com/topic/190674-use-a-php-variable-in-a-mysql-query/#findComment-1005648 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.