2013-04-30 92 views
0

我一直在工作一些代码,其中包含一个表单,要求您输入客户的ID,一旦表单提交后,表单中的PHP将访问数据库并显示输入ID的信息表。从数据库生成特定数据?

但是,我的PHP似乎并没有工作,当我输入一个ID和命中提交我得到这个错误信息“你的SQL语法错误;检查对应于您的MySQL服务器版本的手册正确的语法使用近 '= '987451'' 在1" 号线

这里是我的HTML:

<body> 

<h1>Task 8</h1> 

<form id="customerform" action="task8.php" method="get"> 

<p>please fill in the following form</p> 
<p>Customer ID: <input type="text" name="custID" /><br/> 
<p><input type="submit" value="Submit"> 
<input type="reset" value="Reset"></p> 
</form> 

</body> 

这是我的PHP:

<body> 

<?php 
$conn = mysql_connect("localhost", "twa312", "dam6av9a"); 
mysql_select_db("warehouse312", $conn) 
or die ('Database not found ' . mysql_error()); 

$cust = $_GET["custID"]; 
$sql = "select * from orders"; 
$sql = $sql . "where customerID = '$cust'"; 
$rs = mysql_query($sql, $conn) 
or die ('Problem with query' . mysql_error()); 
?> 

<p>information for customer ID <?php echo $cust ?> :</p> 

<?php if (mysql_num_rows($rs)>0){ ?> 

<table width="700" border="1" cellpadding="10" summary="Customer Details"> 

<tr> 
<th>Order Number</th> 
<th>Order Date</th> 
<th>Shipping Date</th> 
</tr> 

<?php while ($row = mysql_fetch_array($rs)) { ?> 

<tr> 
<td><?php echo $row["orderNumber"]?></td> 
<td><?php echo $row["orderDate"]?></td> 
<td><?php echo $row["shippingDate"]?></td> 
</tr> 

<?php } mysql_close($conn); ?> 

</table> 

<?php } 
else {?> <p>No customer with ID <?php echo $cust ?> in the database</p> 
<?php } ?> 

</body> 

如果您需要更多informati只要问,任何帮助将非常感激!

+0

请停止使用'mysql_'功能,他们弃用。改为使用'mysqli_''或PDO。 – akluth 2013-04-30 14:52:34

回答

4

你错过了一个空格表名的姓名和WHERE之间:

$sql = "select * from orders"; 
$sql = $sql . "where customerID = '$cust'"; 

应该

$sql = "select * from orders"; 
$sql = $sql . " where customerID = '$cust'"; 

或只是

$sql = "select * from orders where customerID = '$cust'"; 

Please, don't use mysql_* functions in new code。他们不再维护and are officially deprecated。查看red box?请改为了解prepared statements,并使用PDOMySQLi - this article将帮助您决定哪个。如果您选择PDO,here is a good tutorial

而且,你大开SQL injections

+1

新的PDO接口非常面向对象,作为一名.NET程序员十多年来一直很适合我,但是还有更多的**脚本可以发送准备好的和参数化的查询吗? – 2013-04-30 14:54:30

+0

什么是“脚本像方式”? – 2013-04-30 15:01:17

+0

我有一种感觉,它会像一个缺失的空间恼人的东西,非常感谢! – bigsenator 2013-04-30 15:05:08