PDA

View Full Version : WHERE `id` = 'a-number-here' problem


error_22
05-05-2006, 07:33 PM
Hi, I have a problem:


echo "<form action='send.php' method='POST'>";
echo "<select name='pageid'>";

$sql = "SELECT * FROM `page` ORDER by `id`";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
echo "<option value='{$row['id']}'>{$row['title']}</option>";
}

echo "</select><input type='submit' value='save'></form>";


in send.php:


$id = $_POST['pageid'];
$sql = "SELECT * FROM `page` WHERE ?id? = '$id'";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
{ echo "something goes here"; }


I get this when i hit the save button:

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '?id? = '16'' at line 1

What am i doing wrong?

Paul M
05-05-2006, 09:16 PM
What are those funny characters around page and id, try removing them.

error_22
05-05-2006, 11:19 PM
nope same thing. Ive had this problem before. It only occurs when the value contains numbers. Am I the only one with this problem? :S

harmor19
05-06-2006, 05:42 AM
try removing the space id='$id'

Xorlev
05-08-2006, 02:36 AM
You somehow managed to use front ticks. Here's how I'd rewrite it (fix + my style):

$sql = 'SELECT * FROM `page` WHERE `id` = "' . $id . '"';

error_22
05-11-2006, 05:08 PM
very very strange....i changed:
$id = $_POST['pageid'];
to:
$id = $_REQUEST['pageid'];
and now it works. I was wondering, whats the difference between $_REQUEST and $_POST?

Thanks for all the help guys!

Adrian Schneider
05-11-2006, 05:13 PM
$_POST is from forms where the method is set to post. $_GET is from the query string (showthread.php?t=5), and $_REQUEST combines the two, as well as a few others.