2013-04-30 94 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