【问题标题】:Generate specific data from database?从数据库生成特定数据?
【发布时间】:2013-04-30 14:50:17
【问题描述】:

我一直在编写一些代码,其中包含一个要求您输入客户 ID 的表单,一旦提交表单,表单后面的 PHP 将访问数据库并显示一个关于 ID 的信息表输入。

但是我的 PHP 似乎无法正常工作,当我输入 ID 并点击提交时,我收到此错误消息“您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本对应的手册以获取正确的在第 1 行的 '= '987451'' 附近使用的语法

这是我的 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>

如果您需要更多信息,请尽管提出任何帮助,我们将不胜感激!

【问题讨论】:

  • 请停止使用mysql_ 函数,它们已被弃用。请改用mysqli_ 或 PDO。

标签: php database forms


【解决方案1】:

您的表名和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 敞开心扉

【讨论】:

  • 新的 PDO 接口是相当面向对象的,作为一个十多年的 .NET 程序员很适合我,但是有没有更多的类似脚本的方式发送准备好的和参数化的查询?
  • 什么是“类似脚本的方式”?
  • 我有一种感觉,就像缺少空格一样烦人,非常感谢!
猜你喜欢
  • 1970-01-01
  • 2012-07-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-06
  • 2011-09-13
  • 2011-08-17
  • 2010-10-23
  • 2016-12-04
相关资源
最近更新 更多