473,387 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to search data from multiple table in mysql using php

Hi guys I’m new to this site and i really need a professional help out here. I am trying to make a search form in my page and basically i can search my database already but i need to revised the code because the process of my search form depends on a dropdown menu
which contains the table names in my database basically the searching will depend to the selected service form the dropdown menu. what i want to happen is to** eliminate the dropdown menu and make an automatic searching throughout my tables in the database and fetch the necessary data.. thanks in advance
**
this is the searchform.html
Expand|Select|Wrap|Line Numbers
  1.     <html>
  2.     <body bgcolor="#CCCCCC">
  3.     <form method="post" action="searchresult.php" name="form2">
  4.     <p><img src="images/europcar.jpg" width="560" height="162" /></p>
  5.     <p align="center">Please dont live any blank space while searching</p>
  6.     <p align="center">Search Reservation Number </p>
  7.     <p align="center">
  8.     <input type="text" name="search" size=25 maxlength=25 >
  9.     <select name="table" size="1">
  10.     <option selected="selected"></option>
  11.     <option value="sd">SelfDrive</option>
  12.     <option value="sdti">SelfDrive texas</option>
  13.     <option value="ch">Carhire</option>
  14.     <option value="chti">Carhire texas</option>
  15.     <option value="cd">Chauffeur Drive</option>
  16.     <option value="cdti">Chauffeur Drive texas</option>
  17.     <option value="pudo">Pick up Drop off</option>
  18.     <option value="pudoti">Pick up Drop off texas</option>
  19.     </select>
  20.     <input type="Submit" name="Submit" value="Submit">
  21.     </p>
  22.     </form>
  23.     </body>
  24.     </html>
  25.  
  26.  
Expand|Select|Wrap|Line Numbers
  1.     **this part is the searching
  2.     searchresult.php**
  3.     <?php
  4.     $table=$_POST['table'];
  5.     $search=$_POST['search'];
  6.     if ($search == "" or $table=="")
  7.     {
  8.     echo"<a href='a.html'> back </a>";
  9.     echo "<p>You forgot to enter the reservation number";
  10.     exit;
  11.     }
  12.     $dbhost = 'localhost';
  13.     $dbuser = 'europcar_msic';
  14.     $dbpass = 'msictran';
  15.     $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
  16.     $dbname = 'europcar_msic';
  17.     mysql_select_db($dbname);
  18.     if ($table=="cd")
  19.     {
  20.     $sql = mysql_query("select * from cd where Rnum like '$search'");
  21.     }
  22.     if ($table=="sd")
  23.     {
  24.     $sql = mysql_query("select * from sd where Rnum like '$search'");
  25.     }
  26.     if ($table=="ch")
  27.     {
  28.     $sql = mysql_query("select * from ch where Rnum like '$search'");
  29.     }
  30.     if ($table=="pudo")
  31.     {
  32.     $sql = mysql_query("select * from pudo where Rnum like '$search'");
  33.     }
  34.     if ($table=="sdti")
  35.     {
  36.     $sql = mysql_query("select * from sdtexas where Rnum like '$search'");
  37.     }
  38.     if ($table=="chti")
  39.     {
  40.     $sql = mysql_query("select * from chtexas where Rnum like '$search'");
  41.     }
  42.     if ($table=="cdti")
  43.     {
  44.     $sql = mysql_query("select * from cdtexas where Rnum like '$search'");
  45.     }
  46.     if ($table=="pudoti")
  47.     {
  48.     $sql = mysql_query("select * from pudotexas where Rnum like '$search'");
  49.     }
  50.     while ($row=mysql_fetch_array($sql))
  51.     {
  52.     echo " <table width='100%' border='0' align='center' cellpadding='0' cellspacing='0' bgcolor='#CCCCCC' > ";
  53.     echo " <tr bgcolor='green'>";
  54.     echo " <td width='5%' align='center' height='10' ><p><font color='white'><strong><B>Reservation Number </th>";
  55.     echo "<td width='5%' align='center' height='10' ><p><font color='white'><b>Name</th>";
  56.     echo "<td width='5%' align='center' height='10' ><p><font color='white'><b>Vehicle Type</th>";
  57.     echo "<td width='5%' align='center' height='10' ><p><font color='white'><b>Origin</th>";
  58.     echo "<td width='5%' align='center' height='10' ><p><font color='white'><b>Pick up date</th>";
  59.     echo "<td width='5%' align='center' height='10' ><p><font color='white'><b>Time</th>";
  60.     echo "<td width='5%' align='center' height='10' ><p><font color='white'><b>Destination</th>";
  61.     echo "<td width='5%' align='center' height='10' ><p><font color='white'><b>Return Date</th>";
  62.     echo "<td width='5%' align='center' height='10' ><p><font color='white'><b> Return Time</th>";
  63.     echo "<td width='5%' align='center' height='10' ><p><font color='white'><b> Contact Number</th>";
  64.     echo "<td width='5%' align='center' height='10' ><p><font color='white'><b> Address</th>";
  65.     echo "</tr>";
  66.     echo"<tr>";
  67.     echo"<a href='a.html'> back </a>";
  68.     echo"<tr>";
  69.     echo "<td width='5%' align='center' height='10' > $row[Rnum]</td>";
  70.     echo "<td width='5%' align='center' height='10' >$row[Fname] $row[Lname]</td>";
  71.     echo "<td width='5%' align='center' height='10' >$row[Vehicle]</td>";
  72.     echo "<td width='5%' align='center' height='10' >$row[Origin]</td>";
  73.     echo "<td width='5%' align='center' height='10' >$row[Pickday] $row[Pickmonth] </td>";
  74.     echo "<td width='5%' align='center' height='10' >$row[Pickhour] : $row[Pickmin]</td>";
  75.     echo "<td width='5%' align='center' height='10' >$row[Destination]</td>";
  76.     echo "<td width='5%' align='center' height='10' >$row[Rday] $row[Rmonth] </td>";
  77.     echo "<td width='5%' align='center' height='10' >$row[Rhour] : $row[Rmin]</td>";
  78.     echo "<td width='5%' align='center' height='10' >$row[Contact]</td>";
  79.     echo "<td width='5%' align='center' height='10' >$row[Zip],$row[Address],$row[City] $row[Country]</td>";
  80.     echo '<br/><br/>';
  81.     }
  82.     //This counts the number or results - and if there wasn't any it gives them a little message explaining that
  83.     $anymatches=mysql_num_rows($sql);
  84.     if ($anymatches == 0)
  85.     {
  86.     echo"<a href='searchform.html'> back </a>";
  87.     echo "Sorry,Invalid Reservation Number or you are looking at the wrong place double check the number and search it again";
  88.     }
  89.     {
  90.     }
  91.     ?>
  92.  
  93.  
  94.  
May 17 '12 #1
3 13414
Rabbit
12,516 Expert Mod 8TB
Use a UNION ALL to combine all your different queries into one. However, you should think about normalizing your data instead of having it spread out over so many tables.
May 17 '12 #2
hi rabbit

im having trouble using union can you give me an example code that is running because when i try to making sample search using UNION theirs an error saying

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\msicrevised\searcherresults.php on line 32

Expand|Select|Wrap|Line Numbers
  1. Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\msicrevised\searcherresults.php on line 68
this is my codes

Expand|Select|Wrap|Line Numbers
  1. $sql = mysql_query("select * from sd where Rnum like '$search'; UNION select * from sdtexas where Rnum like '$search'; UNION select * from pudo where Rnum like '$search'") ;
am i doing it correctly?
May 18 '12 #3
Rabbit
12,516 Expert Mod 8TB
Here's an example of union.
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM Table1
  2. UNION ALL
  3. SELECT * FROM Table2
May 18 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: LRW | last post by:
Well, I have another question that's hard to ask, so I may end up over explaining the question. I have about 40 tables, all with a similar column structure. I want to be able to search through...
1
by: M Wells | last post by:
Hi All, I'm trying to implement encryption on certain data fields in my MySQL database and I'm experiencing ongoing problems. I seem to be able to encrypt the data without issues, but can't...
1
by: Ray | last post by:
Hello all, I am attempting to delete multiple rows from multiple tables as follows: DELETE FROM attachments,responses,response_lines WHERE attachments.id IN(2,7,11) AND...
2
by: Scott Cannon | last post by:
I am trying to query 3 tables all related by Clinet_ID. The Clients table, Monthly_Expenses table and Monthly_Income table. Each client can have 0>M instances of expenses, past due expenses, and...
5
by: JP SIngh | last post by:
Hi All This is a complicated one, not for the faint hearted :) :) :) Please help if you can how to achieve this search. We have a freetext search entry box to allow users to search the...
1
by: ariel gons | last post by:
This is one of my project and also my thesis in school. How can I get data on database MYSQL using javascript function? Is there anybody here can help me with this problem.. Can anyone give...
1
Merlin1857
by: Merlin1857 | last post by:
How to search multiple fields using ASP A major issue for me when I first started writing in VB Script was constructing the ability to search a table using multiple field input from a form and...
1
by: billa856 | last post by:
Hi, My project is in MS Access. In that I have one Form(Search) which I am using for Search data in my Table(PRODUCTION). Now in this form I have one Combobox(PalletNo) and one button(Search)...
1
by: craigcairley | last post by:
Hi guys and gals, I am trying to do a search of a table in my DB but one of the fields is CompanyID which links to another table that has all the company information so I don't have to write the...
0
by: rahul more | last post by:
My aim is - Access data from table and show it into a form's textboxes , comboxes for edition of data. I am using listview. I am selecting particular row of listview by clicking on that row and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.