Php 在SQL查询中利用用户输入

Php 在SQL查询中利用用户输入,php,sql,where,Php,Sql,Where,我正在尝试更新给定用户输入的表。一旦用户点击表单上的submit,我希望查询的WHERE部分反映用户输入的邮政编码。这是我到目前为止所做的,但它不起作用。任何帮助都将不胜感激 <form id="user-location" method="post" action="#"> <input id="addressInput" name="addressInput" type="text"> <input id="submit" onclick

我正在尝试更新给定用户输入的表。一旦用户点击表单上的submit,我希望查询的WHERE部分反映用户输入的邮政编码。这是我到目前为止所做的,但它不起作用。任何帮助都将不胜感激

<form id="user-location" method="post" action="#">
      <input id="addressInput" name="addressInput" type="text">
      <input id="submit" onclick="searchLocations()" value="GO" type="button">
</form>


更改为
,然后使用条件语句

例如:
if(设置($\u POST['submit'])

下面是一个预先准备好的语句方法

你现在(或打算使用)的方式会让你有更多的选择

使用
mysqli.*
函数时使用它总是更好

  • 使用事先准备好的陈述

其中zip='$\u POST[addressInput]'
而不是
其中zip='echo$\u POST['addressInput']'
另外,您不应该使用此方法;你对SQL注入持开放态度;使用事先准备好的陈述。确保您的
searchLocations()
也有一个有效的JS函数。如果不使用prepare语句,请至少使用
$zip=mysqli\u real\u escape\u string($con,$\u POST['addressInput'])
并使用
WHERE-zip='$zip'
。请参阅关于使用准备好的语句。感谢您的帮助!我做了这些更改,但它似乎没有更新我的表。我的提交按钮正确吗?(我也删除了searchLocations())不客气。您是否在同一页面中同时使用表单和PHP@用户2155400如果是,请将
更改为
,然后使用条件语句。
<?php
$con=mysqli_connect("localhost","######","######","######");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM Prospects WHERE zip = 'echo $_POST['addressInput']'");

echo "<table width='540' cellpadding='0' border='0' cellspacing='0'>
<tr>
<th>Under 4</th>
<th>5 - 9</th>
<th>10 - 14</th>
<th>15 - 17</th>
<th>18 - 20</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['cy_pop_04'] . "</td>";
  echo "<td>" . $row['cy_pop_59'] . "</td>";
  echo "<td>" . $row['cy_pop_1014'] . "</td>";
  echo "<td>" . $row['cy_pop_1517'] . "</td>";
  echo "<td>" . $row['cy_pop_1820'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?>
<?php
$con=mysqli_connect("localhost","######","######","######");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

if(isset($_POST['submit'])){
$zip = $_POST['addressInput'];

if($query = $con->prepare("SELECT * FROM Prospects WHERE zip=?")){
    $query->bind_param("s", $zip);
    $query->execute();
}

echo "<table width='540' cellpadding='0' border='0' cellspacing='0'>
<tr>
<th>Under 4</th>
<th>5 - 9</th>
<th>10 - 14</th>
<th>15 - 17</th>
<th>18 - 20</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['cy_pop_04'] . "</td>";
  echo "<td>" . $row['cy_pop_59'] . "</td>";
  echo "<td>" . $row['cy_pop_1014'] . "</td>";
  echo "<td>" . $row['cy_pop_1517'] . "</td>";
  echo "<td>" . $row['cy_pop_1820'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

} // closing brace for if(isset($_POST['submit']))

mysqli_close($con);
?>
WHERE zip = 'echo $_POST['addressInput']'
             ^^^^        ^            ^