Jump to content

Simple Sql Query Not Working


Recommended Posts

I am trying to get my search bar to search three fields from one table and if one of the fields from the three is matched, display the applicable field. I have the following code which should work.

$query_searchbar = "SELECT producttype, manufacturer, modelnumber FROM tblproduct WHERE producttype AND manufacturer AND modelnumber='$_POST[search]'";

This is my form which is running the code.

<form id="search" name="search" method="post" action="searchresults.php">   <input type="text" name="search" id="search" />   </form>

As the searchbar is in the header, I do not want a submit button, it runs by pressing enter.This is the entire page for the results page which is currently not displaying anything.

<?php require_once('Connections/conn_comm.php'); ?><?php//initialize the sessionif (!isset($_SESSION['MM_Username'])) {  session_start();}// ** Logout the current user. **$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){  $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);}if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){  //to fully log out a visitor we need to clear the session varialbles  $_SESSION['MM_Username'] = NULL;  $_SESSION['MM_UserGroup'] = NULL;  $_SESSION['PrevUrl'] = NULL;  unset($_SESSION['MM_Username']);  unset($_SESSION['MM_UserGroup']);  unset($_SESSION['PrevUrl']);	  $logoutGoTo = "loggedout.php";  if ($logoutGoTo) {	header("Location: $logoutGoTo");	exit;  }}?><?phpif (!function_exists("GetSQLValueString")) {function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") {  if (PHP_VERSION < 6) {	$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;  }  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);  switch ($theType) {	case "text":	  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";	  break;		case "long":	case "int":	  $theValue = ($theValue != "") ? intval($theValue) : "NULL";	  break;	case "double":	  $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";	  break;	case "date":	  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";	  break;	case "defined":	  $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;	  break;  }  return $theValue;}}$maxRows_cart = 1;$pageNum_cart = 0;if (isset($_GET['pageNum_cart'])) {  $pageNum_cart = $_GET['pageNum_cart'];}$startRow_cart = $pageNum_cart * $maxRows_cart;$colname_cart = "-1";if (isset($_GET['MM_Username'])) {  $colname_cart = $_GET['MM_Username'];}mysql_select_db($database_conn_comm, $conn_comm);$query_cart = sprintf("SELECT * FROM tblcart WHERE username = %s", GetSQLValueString($colname_cart, "text"));$query_limit_cart = sprintf("%s LIMIT %d, %d", $query_cart, $startRow_cart, $maxRows_cart);$cart = mysql_query($query_limit_cart, $conn_comm) or die(mysql_error());$row_cart = mysql_fetch_assoc($cart);if (isset($_GET['totalRows_cart'])) {  $totalRows_cart = $_GET['totalRows_cart'];} else {  $all_cart = mysql_query($query_cart);  $totalRows_cart = mysql_num_rows($all_cart);}$totalPages_cart = ceil($totalRows_cart/$maxRows_cart)-1;mysql_select_db($database_conn_comm, $conn_comm);$query_searchbar = "SELECT producttype, manufacturer, modelnumber FROM tblproduct WHERE producttype AND manufacturer AND modelnumber='$_POST[search]'";$searchbar = mysql_query($query_searchbar, $conn_comm) or die(mysql_error());$row_searchbar = mysql_fetch_assoc($searchbar);$totalRows_searchbar = mysql_num_rows($searchbar);?><?php// *** Validate request to login to this site.if(!isset($_SESSION['MM_Username'])){  session_start();}$loginFormAction = $_SERVER['PHP_SELF'];if (isset($_GET['accesscheck'])) {  $_SESSION['PrevUrl'] = $_GET['accesscheck'];}if (isset($_POST['username'])) {  $loginUsername=$_POST['username'];  $password=$_POST['password'];  $MM_fldUserAuthorization = "";  $MM_redirectLoginSuccess = "loggedin.php";  $MM_redirectLoginFailed = "sorry.php";  $MM_redirecttoReferrer = false;  mysql_select_db($database_conn_comm, $conn_comm);    $LoginRS__query=sprintf("SELECT username, password FROM tbluser WHERE username=%s AND password=%s",	GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));      $LoginRS = mysql_query($LoginRS__query, $conn_comm) or die(mysql_error());  $loginFoundUser = mysql_num_rows($LoginRS);  if ($loginFoundUser) {	 $loginStrGroup = "";		//declare two session variables and assign them	$_SESSION['MM_Username'] = $loginUsername;	$_SESSION['MM_UserGroup'] = $loginStrGroup;		  	if (isset($_SESSION['PrevUrl']) && false) {	  $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];		}	header("Location: " . $MM_redirectLoginSuccess );  }  else {	header("Location: ". $MM_redirectLoginFailed );  }}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Communicate - the best in mobile communications</title><link rel="stylesheet" type="text/css" href="_css/main.css" /><link rel="stylesheet" type="text/css" href="_css/style.css" />	<script src="jquery-1.2.1.min.js" type="text/javascript"></script>	<script src="menu.js" type="text/javascript"></script><!--[if IE]><style type="text/css"> /* place css fixes for all versions of IE in this conditional comment */.thrColElsHdr #sidebar1, .thrColElsHdr #sidebar2 { padding-top: 30px; }.thrColElsHdr #mainContent { zoom: 1; padding-top: 15px; }/* the above proprietary zoom property gives IE the hasLayout it needs to avoid several bugs */</style><![endif]--></head><body class="thrColElsHdr"><div id="container">  <div id="header">   <div id="search">   <form id="search" name="search" method="post" action="searchresults.php">   <input type="text" name="search" id="search" />   </form>   </div>     <!-- end #header -->  </div>      <div id="sidebar1"><ul id="menu">						<li><a href="http://www.himeshs.co.uk/communicate/index.php">Home</a></li>									 														<li>							<a href="#">Manufacturer</a>							<ul> 							  <li><a href="http://www.himeshs.co.uk/communicate/allmanufacturer.php">All</a></li> 							<li><a href="http://www.himeshs.co.uk/communicate/se.php">Sony Ericsson</a></li> 							<li><a href="http://www.himeshs.co.uk/communicate/apple.php">Apple</a></li> 							<li><a href="http://www.himeshs.co.uk/communicate/nokia.php">Nokia</a></li> 							<li><a href="http://www.himeshs.co.uk/communicate/samsung.php">Samsung</a></li> 							</ul> 						</li>													 <li>							<a href="#">Type</a>								<ul> 							<li><a href="http://www.himeshs.co.uk/communicate/alltype.php">All</a></li> 							<li><a href="http://www.himeshs.co.uk/communicate/slide.php">Slide</a></li> 							<li><a href="http://www.himeshs.co.uk/communicate/candy.php">Candy Bar</a></li> 							<li><a href="http://www.himeshs.co.uk/communicate/flip.php">Flip</a></li>					  							</ul> 	  </li>																  <li>							<a href="#">Features</a>							<ul> 							<li><a href="http://www.himeshs.co.uk/communicate/camera.php">Camera</a></li> 							<li><a href="http://www.himeshs.co.uk/communicate/music.php">Music</a></li> 							<li><a href="http://www.himeshs.co.uk/communicate/internet.php">Internet</a></li>												  							</ul> 						</li>									<li><a href="http://www.himeshs.co.uk/communicate/aboutus.php">About us</a></li>							<li><a href="http://www.himeshs.co.uk/communicate/contact.php">Contact us</a></li>	</ul>  <!-- end #sidebar1 -->  </div>      <div id="sidebar2">    <?phpif(isset($_SESSION['MM_Username'])){  // Member is logged in so we have to display welcome message with userid and one logout linkdo { $html = <<<END_OF_HTMLWelcome {$_SESSION['MM_Username']}  <table border="0">	<tr>	  <td>Manufacturer</td>	   <td> {$row_cart['manufacturer']} </td>	  </tr>	 	  <tr>	   	<td>Model Number</td>		<td>  {$row_cart['modelnumber']} </td>		</tr>	  <tr>		<td>Quantity</td>		<td>  {$row_cart['quantity']} </td>	  </tr>	  <tr>		 <td>Price</td>		 <td> {$row_cart['price']} </td>	  </tr>	     </table>  END_OF_HTML;	echo $html;} while ($row_cart = mysql_fetch_assoc($cart)); } else {  // Member has not logged in so we can display the login form allowing member to login with user id and passwordecho <<<END_OF_HTML	  <form id="form1" name="form1" method="POST" action="{$loginFormAction}">			 <input type="text" name="username" id="username" /><br />	  Password<br />	<input type="password" name="password" id="password" /> <br /><input name="Submit" type="submit" value="Submit" /></form>END_OF_HTML;}?>  <a href="<?php echo $logoutAction ?>">Log out</a>  <!-- end #sidebar2 --></div>      <div id="mainContent">	<h1><?php echo $row_searchbar['manufacturer']; ?></h1>	<p><?php echo $row_searchbar['modelnumber']; ?></p>	<p><?php echo $row_searchbar['producttype']; ?></p>	<!-- end #mainContent -->  </div>		<!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats --><br class="clearfloat" />   <div id="footer">	<p>Footer</p>  <!-- end #footer --></div><!-- end #container --></div></body></html><?phpmysql_free_result($cart);mysql_free_result($searchbar);?>

Link to comment
Share on other sites

Write your conditionals out completely, and use OR instead of AND. All 3 of your fields won't be equal to your search term at the same time, right?$item = $_POST['search'];$query_searchbar = "SELECT producttype, manufacturer, modelnumber FROM tblproduct WHERE producttype='$item' OR manufacture='$item' OR modelnumber='$item'";I have not looked at the rest of your code. It might be a good idea to test your queries in a separate file that is as simple as possible.

Edited by Deirdre's Dad
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...