Forum Moderators: coopster
i'm new to PHP and MySQL and am trying to set up a simple form to query a small single table database.
I've got a simple HTML file with a simple form in which calls getname.php (HTML code is below)
<html>
<head>
<title>PHP TEST</title>
</head>
<form method='get' action='getname.php'>
<p>
Whose order? <input type='text' name='orderer_name'>
</p>
<p>
<input type='submit'>
</p>
</form>
</body>
</html>
The PHP script I'm trying is as follows -
<?
mysql_connect("localhost", "root")
or die("Could not connect: " . mysql_error());
mysql_select_db("test2");
if ($orderer_name == "")
{$orderer_name = '%';}
$result = mysql_query ("SELECT * FROM houseorder WHERE orderer_name = $orderer_name%")
or die("Something went wrong: <p>" .mysql_errno().": ".mysql_error()."<BR>");
if ($row = mysql_fetch_array($result)) {
do {
print $row["date"];
print (" ");
print $row["time"];
print (" ");
print $row["orderer_name"];
print (" ");
print $row["costcentre"];
print (" ");
print $row["code"];
print (" ");
print $row["description"];
print (" ");
print $row["packsize"];
print (" ");
print $row["requested"];
print ("<p>");
} while($row = mysql_fetch_array($result));
} else {print "Sorry, no records were found!";}
?>
If I enter the orderer_name as James it falls over and produces the following error -
Something went wrong:1064: 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 '' at line 1
$result = mysql_query ("SELECT * FROM houseorder WHERE orderer_name = "James")
or die("Something went wrong: <p>" .mysql_errno().": ".mysql_error()."<BR>");
And this works. It returns the correct results from the table. Which seems to be that same as entering the name in the form?
I'm sure i'm doing something wrong somewhere as I guess this is quite basic, but it's driving me up the wall!
I'm guessing that there is something wrong with my variable $orderer_name and MySQL doesn't like it.
I even added the following line
echo "<p>$orderer_name<p>";
and it showed
James
Has anyone got any ideas?
P.S. I've tried using the [ code] [ /code] styles but it looks wrong on the preview. I hope it's readable on the proper post.
P.S.S It didn't so i removed some of them.