-2
<?php
$servername = "localhost";
$username = "root";
$password = "";
mysql_connect("$servername", "$username", "$password") or die ("Could not connect!");
mysql_select_db("test") or die ("Could not connect to database!");
$output = '';
if(isset($_POST['name'])) {
$searchq = $_POST['name'];
$query = mysql_query("SELECT * FROM catalouge WHERE name LIKE '%$searchq%' OR cost LIKE '%$searchq%'") or die ("Could not search!");
$count = mysql_num_rows($query);
if($count == 0) {
$output = 'That item does not exist';
}else {
while ($row = mysql_fetch_array($query)) {
$name = $row['name'];
$cost = $row['cost'];
$output = '<div>Item Name: ' .$name. '<br>Cost: ' .$cost. '</div><br>';
}
}
}
?>
<html>
<body>
<form method="POST">
<input type="text" name="name" placeholder="Search for an item..."></input>
<input type="Submit" value=">>"></input>
<?php print("$output"); ?>
</form>
</body>
</html>
我真的需要这方面的帮助。我需要通过使用表单进行搜索来从数据库中打印/回显所有数据。我试图使用print_r
和var_dump
尝试显示数据库中的所有信息。从数据库中打印数据
我已经写了大约二十次今天,现在再次:不再使用mysql_ *函数!使用mysqli_ *或PDO,这些函数太旧,并且从PHP 7开始不推荐使用。 –
我被告知这段代码很危险,单个sql注入可能会破坏所有内容并使用pdo或mysqli ...请搜索sql注入 – Sykox