English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

like if i have a table with 2 columns (red, blue) and if the user selects in a dropdown menu blue can i write "Insert into colors $blue = 'true'"

2007-09-24 20:34:14 · 4 answers · asked by Danbbb 2 in Computers & Internet Programming & Design

4 answers

Yes, variable replacement in strings is done before the SQL query. So the SQL server will only see the final string; it won't know there was ever a variable in there.

I'm assuming you know that particular line of SQL won't work. And for clarity sake, you should name the variable $color or something that makes sense regardless of the user's choice. Maybe I'm just reading too much into your example...

2007-09-24 20:54:10 · answer #1 · answered by Anonymous · 0 0

yes you can use variables in a php mysql statement.

2007-09-24 20:40:43 · answer #2 · answered by PistolPete 2 · 0 0

Do it this way:
$sql = 'INSERT INTO `tablename` ( ` ' . $varname . ' ` )';
$sql .= ' VALUES (" ' . $varvalue . ' " ) ';
mysql_query($sql) or die("Query failed : " . mysql_error());

Note the ` (reverse of ´ ). Spaces added for clarity.

2007-09-24 21:04:49 · answer #3 · answered by just "JR" 7 · 0 0

$query='insert into my_table values (red, blue) values ("'.$blue.'", "")';

2007-09-27 07:47:56 · answer #4 · answered by gamcitic 2 · 0 0

fedest.com, questions and answers