Jump to content

Suraj's Multiple Search using PHP and Mysql


josephbupe

Recommended Posts

Hi,

 

I found a script http://www.weberdev....?ExampleID=4406 by someone called "Suraj Thapaliya" for doing a multiple search using PHP and MySQL. I am trying to adopt it to my own use for one application am putting up together with the help of different scripts.

 

In my application, if I populate only one of the text boxes or combo boxes the search is executed fine. But the problem is that when I use two or more criterias I am getting an error like the one below:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'cmaterial = 'oil' Limit 0, 16' at line 1
; in which case I used the cmaterial combo box and ctitle text box as the criteria. The following are combo boxes: cmaterial, cobjecttype and ctechnic, while the rest are text boxes.

 

My code is as follows:

 

<?
include("includes/conn.php");
$ctitle=$_POST["ctitle"];
$csubject=$_POST["csubject"];
$creference=$_POST["creference"];
$cyear=$_POST["cyear"];
$cobjecttype=$_POST["cobjecttype"];
$cmaterial=$_POST["cmaterial"];
$ctechnic=$_POST["ctechnic"];
$cwidth=$_POST["cwidth"];
$cheight=$_POST["cheight"];
$cperiod=$_POST["cperiod"];
$cmarkings=$_POST["cmarkings"];
$o=$_POST["o"];
 $rest="";
 $text="Search Keywords : ";

 if($ctitle!="")
 {
 if($rest=="")
 {
 $rest.="where ctitle Like '$ctitle%' ";
 $text.="Item Title = $ctitle";
 }
 else
 {
 $rest.=" $o ctitle = '$ctitle' ";
 $text.=", Item Title = $ctitle";
 }
 }

 if($csubject!="")
 {
 if($rest=="")
 {
 $rest.="where csubject = '$csubject' ";
 $text.="Subject = $csubject ";
 }
 else
 {
 $rest.=" $o csubject = '$csubject' ";
 $text.=", Subject = $csubject ";
 }
 }

 if($creference!="")
 {
 if($rest=="")
 {
 $rest.="where creference = '$creference' ";
 $text.="Reference No. = $creference";
 }
 else
 {
 $rest.=" $o creference = '$creference' ";
 $text.=", Reference No. = $creference";
 }
 }


 if($cyear!="")
 {
 if($rest=="")
 {
 $rest.="where cyear Like '$cyear%' ";
 $text.="Year = $cyear";
 }
 else
 {
 $rest.=" $o cyear = '$cyear' ";
 $text.=", Year = $cyear";
 }
 }

 if($cobjecttype!="")
 {
 if($rest=="")
 {
 $rest.="where cobjecttype Like '$cobjecttype%' ";
 $text.="Object = $cobjecttype";
 }
 else
 {
 $rest.=" $o cobjecttype = '$cobjecttype' ";
 $text.=", Object = $cobjecttype";
 }
 }

 if($cmaterial!="")
 {
 if($rest=="")
 {
 $rest.="where cmaterial Like '$cmaterial%' ";
 $text.="Material = $cmaterial";
 }
 else
 {
 $rest.=" $o cmaterial = '$cmaterial' ";
 $text.=", Material = $cmaterial";
 }
 }

 if($ctechnic!="")
 {
 if($rest=="")
 {
 $rest.="where ctechnic Like '$ctechnic%' ";
 $text.="Technic = $ctechnic";
 }
 else
 {
 $rest.=" $o ctechnic = '$ctechnic' ";
 $text.=", Technic = $ctechnic";
 }
 }

 if($cwidth!="")
 {
 if($rest=="")
 {
 $rest.="where cwidth Like '$cwidth%' ";
 $text.="Width = $cwidth";
 }
 else
 {
 $rest.=" $o cwidth = '$cwidth' ";
 $text.=", Width = $cwidth";
 }
 }

 if($cheight!="")
 {
 if($rest=="")
 {
 $rest.="where cheight Like '$cheight%' ";
 $text.="Height = $cheight";
 }
 else
 {
 $rest.=" $o cheight = '$cheight' ";
 $text.=", Height = $cheight";
 }
 }

 if($cperiod!="")
 {
 if($rest=="")
 {
 $rest.="where cperiod Like '$cperiod%' ";
 $text.="Period = $cperiod";
 }
 else
 {
 $rest.=" $o cperiod = '$cperiod' ";
 $text.=", Period = $cperiod";
 }
 }

 if($cmarkings!="")
 {
 if($rest=="")
 {
 $rest.="where cmarkings Like '$cmarkings%' ";
 $text.="Markings = $cmarkings";
 }
 else
 {
 $rest.=" $o cmarkings = '$cmarkings' ";
 $text.=", Markings = $cmarkings";
 }
 }

 if($rest!="")
 {
 $rest=$rest;
 }
 else
 {
 //die("Enter Search Parameter<br><br><br><br><br><br><br><br><br><br><br>");
 }
//Number of items to display per page
$perpage = 16;
if(isset($_GET["page"]))
{
$page = intval($_GET["page"]);
}
else
{
$page = 1;
}
$calc = $perpage * $page;
$start = $calc - $perpage;
//////////////////

$sql="select * from $tbl_name $rest Limit $start, $perpage";
$result=mysql_query($sql) or die(mysql_error());
$num=mysql_num_rows($result);
?>

 

Please, what did I do wrong?

 

joseph

Edited by josephbupe
Link to comment
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.