Jump to content

Recommended Posts

Hi guys and girls

 

Right im not directly asking for code but i am aware how to set up and make a single search in a form against a database the issue ive come across is that i was to be able for the user to search against 3 different columns in the table each with their own textbox and submit button just wonderign if i would do this on 3 different forms or if it is easy ?

 

Thank you

Link to comment
https://forums.phpfreaks.com/topic/257918-search/
Share on other sites

Ok i basiclaly have a list of houses each with attributes price street name area etc

 

I want the user to be able to search againt price street name area etc all on one page it can be with one submit button or seperate ones

 

jsut wonderign hwo i woudl check which textboxes have information in them or search using information in all of them ?

Link to comment
https://forums.phpfreaks.com/topic/257918-search/#findComment-1321991
Share on other sites

In that case, you would only need one form. You could do something like

if (!empty($_POST)) {
$price  = $_POST['price'];
$street = $_POST['street'];
$area   = $_POST['area'];

$where = array();

if (!empty($price)) {
	$where[] = "price LIKE '%$price%'";
}

if (!empty($street)) {
	$where[] = "street LIKE '%$street%'";
}

if (!empty($area)) {
	$where[] = "area LIKE '%$area%'";
}

$where = implode(' OR ', $where);

$result = mysql_query('SELECT * FROM houses WHERE ' . $where);

if (mysql_num_rows($result) > 0) {
	while($row = mysql_fetch_assoc($result)) {
		echo 'results...<br />';
	}
} else {
	echo 'no matches!';
}
}

?>

<form action="" method="post">
  Price: <input type="text" name="price" /><br />
  Street: <input type="text" name="street" /><br />
  Area: <input type="text" name="area" /><br />
  <input type="submit" name="submit" value="Search" />
</form>

Link to comment
https://forums.phpfreaks.com/topic/257918-search/#findComment-1321993
Share on other sites

Hi thanx for that so atm ive got the following:

 

<?php 
session_start();
include("db.php");
        check_valid_user();


mysql_connect("localhost", "root") or die(mysql_error());											// makes a connection
mysql_select_db("Computers1") or die('I cannot connect to the database because: ' . mysql_error()); //conects to the database



if (!empty($_POST)) {
$Price  = $_POST['Price'];
$Model = $_POST['Model'];
$Storage = $_POST['Storage'];

$where = array();

if (!empty($Price)) {
	$where[] = "price LIKE '%$Price%'";
}

if (!empty($Model)) {
	$where[] = "Model LIKE '%$Model%'";
}

if (!empty($Storage)) {
	$where[] = "Storage LIKE '%$Storage%'";
}

$where = implode(' OR ', $where);

$result = mysql_query('SELECT * FROM items WHERE ' . $where);

if (mysql_num_rows($result) > 0) {
	while($row = mysql_fetch_assoc($result)) {
		echo 'results...<br />';

$id = $row["itemId"];
$Model = $row["Computer_Model"];
$Price= $row["Computer_Price"];
$Storage =  $row["Computer_Storage"];
$Processor = $row["Computer_Processor_Name"];
$Monitor = $row["Computer_Monitor_Size"];
$desc= $row["Description"];		

echo '<tr><td>';

echo "<h3>$id<h3>";
echo "Price :<strong> $Price 	</strong><br />";
echo "Model :<strong> $Model</strong><br />";
echo "Storage :<strong> $Storage</strong><br />";
echo "Monitor :<strong> $Monitor</strong><br />";
echo "Processor :<strong> $Processor</strong><br />";
echo "desc :<strong> $desc	</strong><br />";
	echo '</td></tr>';

	}
} else {
	echo 'no matches!';
}
}

?>

<form action="" method="POST">
  Price: <input type="text" name="Price" /><br />
  Model: <input type="text" name="Model" /><br />
  Storage: <input type="text" name="Storage" /><br />
  <input type="submit" name="submit" value="Search" />
</form>

 

Im testing it on a databse of computers until it works fully so i dont mess up the other one :P seems silly but it works for me :D

 

The error im now getting is even if the ebtry is a match it says no matches and Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\dancomp\Search.php on line 35 error apears after a search is made.

Link to comment
https://forums.phpfreaks.com/topic/257918-search/#findComment-1321995
Share on other sites

you need to run all these user-submitted variables through mysql_real_escape_string to avoid SQL injection.

 

Also, I wouldn't search based on price, it seems silly to have a user search for "2" and get back something that costs $82.50.

 

 

Link to comment
https://forums.phpfreaks.com/topic/257918-search/#findComment-1322033
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.