我已经用我想到的接受用户输入的代码更新了这个问题。基本上我试图确保这段代码不受 SQL 注入的影响,因为它确实有用户输入。
<?php
$username = "XXXX";
$password = "XXXX";
?>
<font size="+3" face="Verdana">XXXX</font>
<br><br>
<form name="form" action="XXX.PHP" method="get">
<input type="text" name="q" size="60" />
<input type="submit" name="Submit" value="Search">
</form>
<table>
<?
$var = $_GET['q'];
try {
$conn = new PDO('mysql:host=localhost;dbname=XXXX', $username, $password);
$stmt = $conn->prepare("SELECT * FROM XXXX WHERE XXXX LIKE '%$var%' OR XXXX LIKE '%$var%' OR XXXX LIKE '%$var%'");
$stmt->execute(array());
$result = $stmt->fetchAll();
if ( count($result) ) {
foreach($result as $row) {
echo "<tr>";
echo "<td>" .$row['XXXX']. "</td>";
echo "<td>" .$row['XXXX']. "</td>";
echo "<td>" .$row['XXXX']. "</td>";
echo "</tr>";
}
}
else {
echo "No results found.";
}
}
catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>
</table>