Jump to content

Use a PHP variable in a MySQL query?


TeamCIT

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/190674-use-a-php-variable-in-a-mysql-query/
Share on other sites

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?

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");

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.