我有一個SQL表,並在那裏大約5000行。我編寫了一個用於搜索主鍵並使用WHERE-IN子句顯示結果的php代碼。當我只搜索一個主鍵時,它給了我想要的結果,但是如果我搜索多個主鍵,它不會給出任何結果。附上我的代碼。我是這個領域的初學者。任何人都可以幫我解決.. ??SQL查詢搜索不返回任何結果
<!DOCTYPE html>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="Demo project">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="bootstrap.css">
</head>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "site_info";
$conn = mysql_connect($servername, $username, $password);
mysql_select_db("site_info");
$siteid= $_GET['search1'];
$sql = "SELECT * FROM total_database WHERE primary_key IN ('$siteid')";
$result = mysql_query($sql);
if(!$result){die(mysql_error());}
?>
<body>
<div class="container">
<table class="table table-hover">
<tr>
<th>column 1</th>
<th>column 2</th>
<th>column 3</th>
<th>column 4</th>
<th>column 5</th>
</tr>
<?php
if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_array($result)) {
echo "<form action=info.php method=GET>";
echo"<tr>";
echo "<td>" .$row["column1"]. "</td>";
echo "<td>" .$row["column2"]. "</td>";
echo "<td>" .$row["column3"]. "</td>";
echo "<td>" .$row["column4"]. "</td>";
echo "<td>" .$row["column5"]. "</td>";
echo "<input type=hidden name=site_id value=".$row["primary_key"]."></input>";
echo "<td>" ."<input class=btn type=submit value=select". "></td>";
echo"</tr>";
echo "</form>";
}
}
?>
</table>
</div>
</body>
<script type="text/javascript"></script>
警告!你的代碼容易受到[SQL注入攻擊](https://en.wikipedia.org/wiki/SQL_injection) – dotnetom
你檢查過'$ siteid'的值嗎? –
是的,我已經迴應並檢查。它顯示正確的值。 – Akshay