Php 如何从mysql表中查询搜索结果?
我已经做了一个产品上传从和它的作品成功。但现在我想创建一个搜索表单,这样任何人都可以通过搜索产品标题、类别或价格来显示结果。我的表格包含标题、价格、说明、类别列 我的代码不起作用。我不知道如何正确地做这件事。当我想添加更多列时,它不起作用。我肯定不知道是什么错了,或者我应该怎么做 以下是我的产品搜索代码:Php 如何从mysql表中查询搜索结果?,php,mysql,sql,Php,Mysql,Sql,我已经做了一个产品上传从和它的作品成功。但现在我想创建一个搜索表单,这样任何人都可以通过搜索产品标题、类别或价格来显示结果。我的表格包含标题、价格、说明、类别列 我的代码不起作用。我不知道如何正确地做这件事。当我想添加更多列时,它不起作用。我肯定不知道是什么错了,或者我应该怎么做 以下是我的产品搜索代码: <?php // Connects to your Database mysql_connect("localhost", "root", "") or die
<?php
// Connects to your Database
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("product") or die(mysql_error()) ;
$search =$_POST['search'];
$data = mysql_query("SELECT * FROM manage WHERE title or category and price= '$search' ") or die(mysql_error());
//Puts it into an array
while($info = mysql_fetch_array( $data )) {
//Outputs the image and other data
echo "<img src=http://localhost/hasem/exresze/images/".$info['photo'] ."> <br>"; Echo "<b>Title:</b> ".$info['title'] . "<br> "; Echo "<b>Description:</b> ".$info['description'] . " <br>"; Echo "<b>Category:</b> ".$info['category'] . " <hr>"; }
?>
<form action="search_product.php" method="POST">
<input type="text" name="search">
<input type="submit" value="Search" />
</form>
?>
我想你需要这样的东西。这将搜索给定的标题、类别或价格 您基本上需要这样的搜索查询
SELECT
title, price, description, category, photo
FROM `manage`
WHERE
title LIKE '%$title%' OR
price LIKE '%$price%' OR
category LIKE '%$category%'
您可以对这段代码进行更多优化。这取决于你做过滤
<?php
//This is the directory where images will be saved
//This gets all the other information from the form
$title=$_POST['title'];
$price=$_POST['price'];
$category=$_POST['category'];
// Connects to your Database
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("product") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("SELECT title, price, description, category, photo FROM `manage` WHERE title LIKE '%$title%' OR price LIKE '%$price%' OR category LIKE '%$category%' ") ;
//Handle the result
?>
<div id="contact">
<h1>Add New Product.</h1>
<form action="admin.php" method="POST">
<fieldset>
<label for="title">Product Title<span>*</span></label>
<input type="text" name="title" id="name" placeholder="Enter your product title" />
<label for="price">Product Price $<span>*</span></label>
<input type="text" name="price" id="name" placeholder="Enter your product price" />
<label for="category">Product Category <span>*</span></label>
<select name="category" id = "myList">
<option value = "Cloth">Cloths</option>
<option value = "Honda">Honda</option>
<option value = "Book">Book</option>
<option value = "Computer">Computer</option>
</select>
<input type="submit" value="Search" />
</fieldset>
</form>
</div>
据我所知,您需要这样的查询:(我还在搜索字符串中添加了描述,这是一种常见做法)
mysql\u query('SELECT*FROM`manage`WHERE`title`像“%$search\u string%”或`description`像“%$search\u string%”或(`price`>$min\u price和`price`搜索选项有哪些代码?您需要先尝试编码,然后寻求代码方面的帮助,而不仅仅是寻求代码。要给您一个起点:从表单获取用户输入,请在数据库中选择他们输入的内容与数据库中的内容匹配的位置。仔细验证和清理他们的输入。
<?php
//This is the directory where images will be saved
//This gets all the other information from the form
$title=$_POST['title'];
$price=$_POST['price'];
$category=$_POST['category'];
// Connects to your Database
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("product") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("SELECT title, price, description, category, photo FROM `manage` WHERE title LIKE '%$title%' OR price LIKE '%$price%' OR category LIKE '%$category%' ") ;
//Handle the result
?>
<div id="contact">
<h1>Add New Product.</h1>
<form action="admin.php" method="POST">
<fieldset>
<label for="title">Product Title<span>*</span></label>
<input type="text" name="title" id="name" placeholder="Enter your product title" />
<label for="price">Product Price $<span>*</span></label>
<input type="text" name="price" id="name" placeholder="Enter your product price" />
<label for="category">Product Category <span>*</span></label>
<select name="category" id = "myList">
<option value = "Cloth">Cloths</option>
<option value = "Honda">Honda</option>
<option value = "Book">Book</option>
<option value = "Computer">Computer</option>
</select>
<input type="submit" value="Search" />
</fieldset>
</form>
</div>
mysql_query('SELECT * FROM `manage` WHERE `title` LIKE "%$search_string%" OR `description` LIKE "%$search_string" OR (`price`>$min_price AND `price`<$max_price') OR `category`=$category);