After some trying i think it is just a problem with the syntax. I have the following code :
<body>
<?php
if ($submit)
{
$db = mysql_connect("localhost", "USERNAME", "********");
mysql_select_db("DBNAME", $db);
$result = mysql_query("SELECT name FROM user WHERE id=$id", $db) or die("fail");
printf(mysql_result($result,0,0));
}
else
{
?>
<form method="post" action="<?php echo $PHP_SELF?>">
<input type="text" name="id">
<input type="submit" name="submit" value="submit">
</form>
<?php
}
?>
</body>
The table has two colums id (tinyint) and name(varchar).
When i use the above script and enter an int it works perfectly and returns the corresponding name.
Now i want to submit a name and have the id returned. I use the following code :
<body>
<?php
if ($submit)
{
$db = mysql_connect("localhost", "USERNAME", "********");
mysql_select_db("DBNAME", $db);
$result = mysql_query("SELECT id FROM user WHERE name=$name", $db) or die("fail");
printf(mysql_result($result,0,0));
}
else
{
?>
<form method="post" action="<?php echo $PHP_SELF?>">
<input type="text" name="name">
<input type="submit" name="submit" value="submit">
</form>
<?php
}
?>
</body>
If i enter a name it ALWAYS fails the query.
However if i replace name=$name with name='ANYNAMEINDB' it gives the right id.
Why doesn't it work if i use a variable?
I think it has something to do with the syntaxis but what?