Php 查询数据库以查找客户? 查询表格 输入客户名称:

Php 查询数据库以查找客户? 查询表格 输入客户名称:,php,html,mysql,Php,Html,Mysql,所以这里应该发生的是,我在数据库中搜索用户输入,这是一个名称。当我按ID搜索时,它工作得很好,但现在当我尝试搜索时,它会显示“无法查询数据库” “../connect.php”是到我的数据库的连接,customers是我数据库中的一个表,Name是其中的一行,我认为其他一切都应该是不言自明的。我真的不明白为什么它能用ID工作,但现在不工作。你需要在名称周围加引号 试试这个: $sql=“从Name='$Name'的客户中选择*; 这正是问题所在。谢谢 <html> <head&

所以这里应该发生的是,我在数据库中搜索用户输入,这是一个名称。当我按ID搜索时,它工作得很好,但现在当我尝试搜索时,它会显示“无法查询数据库”


“../connect.php”是到我的数据库的连接,customers是我数据库中的一个表,Name是其中的一行,我认为其他一切都应该是不言自明的。我真的不明白为什么它能用ID工作,但现在不工作。

你需要在
名称
周围加引号
试试这个:

$sql=“从Name='$Name'的客户中选择*;

这正是问题所在。谢谢
<html>
<head>
<title>Form for query</title>
</head>

<body>
<?php
  include('../connect.php');

if (isset($_POST['Name'])) {
  $name = $_POST['Name'];
}

if(!empty($name)) {

  $sql = "SELECT * from customers where Name = $name";

  $query = mysqli_query($db,$sql)
    or die("Cannot query the database.<br>" . mysql_error());

  $num_rows = mysqli_num_rows($query);
  if ($num_rows == 0) {print "There is no record with that id";}

  print "<table align=\"left\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\">\n";

  while($result = mysqli_fetch_array($query)) {
        $name = $result["Name"];
?>

<table>
  <tr>
    <td> <?php echo 'Customer:  '. $name; ?> </td>
  </tr>
</table>

<?php
  }
  } else {

?>

<html>
<body>

<form action="http://localhost/php/find_customer.php" method="post">

<tr>
    <td><b>Enter customer name:  </b></td>
    <td><input type="text" name="Name" size="30"></td>
</tr>
<br>
<tr>
        <td><input type="submit" name="upload" value="Submit"></td>
        <td><input type="reset" name="reset" value="Reset"></td>
</tr>

</table>
</form>

<?php
  }
?>

</body>
</html>