473,322 Members | 1,501 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,322 software developers and data experts.

How to search database using php

hi i have a problem i need to search my database for the registration number..the problem is i have 2 tables in that database such as cd and sd.. how can i search it?

i already have an idea its using textbox and combobox and a submit button but i dont how to implement it..please help me guyz thanks

here is the form

Expand|Select|Wrap|Line Numbers
  1.  <form method="post" action="searcherresults.php">
  2.  
  3.   <select name="table" size="1">
  4.  <option value="cd">cd</option>
  5.  <option value="sd">sd</option>
  6.  </select>
  7.  <input type="text" name="search" size=25 maxlength=25>
  8.  <input type="Submit" name="Submit" value="Submit">
  9.  </form>
  10.  
  11.  

and this searchresult.php

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3.  
  4.  $search=$_POST['search'];
  5.  $table=$_POST['table'];
  6.  
  7.  
  8.  
  9.  
  10.  
  11.                                     $dbhost = 'localhost';
  12.                                     $dbuser = 'root';
  13.                                     $dbpass = '';
  14.  
  15.                                     $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
  16.  
  17.                                     $dbname = 'msic';
  18.                                     mysql_select_db($dbname);
  19.  $sql = mysql_query ("SELECT * FROM '%{$table}%' WHERE Rnum LIKE '%{$search}%'");
  20. while ($row = mysql_fetch_array($sql)){
  21.     echo 'ID: '.$row['Idko'];
  22.     echo '<br/> Registration Number: '.$row['Rnum'];
  23.     echo '<br/> Name: '.$row['Fname'];
  24.     echo ' '.$row['Lname'];
  25.     echo '<br/>Contact Number: '.$row['Contact'];
  26.     echo '<br/>Rate: '.$row['Rate'];
  27.     echo '<br/> Pick Up location: '.$row['Origin'];
  28.     echo '<br/>Pick Up Date: '.$row['Pickday'] . $row['Pickmonth'] ;
  29.     echo '<br/>Pick time: '.$row['Pickhr'];
  30.     echo ' '.$row['Pickmin'];
  31.     echo '<br/>Return Date: '.$row['Rday'] . $row['Rmonth'] ;
  32.     echo '<br/>Return time: '.$row['Rhr'] ;
  33.     echo ' '.$row['Rmin'];
  34.     echo '<br/> Destination: '.$row['Destination'];
  35.     echo '<br/> Vehicle Type: '.$row['Vehicle'];
  36.     echo '<br/><br/>';
  37.     }
  38.  
  39.  ?>
  40.  
  41.  
May 14 '12 #1
18 1586
Dormilich
8,658 Expert Mod 8TB
as a matter of fact, you search in a database by using the database (resp. its language SQL). PHP is only there to transform the user input into SQL and the returned DB data into (readable) HTML.
May 14 '12 #2
have any idea how can i search my database with a use of dropdown menu, combo box?i'm really stuck in here dude..
May 14 '12 #3
Dormilich
8,658 Expert Mod 8TB
searching a DB from scratch is a lot of code, you know? and depending on how each part looks (you have at least HTML, CSS, PHP, SQL (not to forget the DB layout) and possibly JavaScript), it can become quite a handful.
May 14 '12 #4
i've already made a bit of coding and im having an error
Expand|Select|Wrap|Line Numbers
  1. Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\msicrevised\searcherresults.php on line 20
May 14 '12 #5
Dormilich
8,658 Expert Mod 8TB
that means that your query failed due to an SQL syntax error.
May 14 '12 #6
can you point it out where i made the mistake..and can you give me a an alternative solution.thanks
May 14 '12 #7
Dormilich
8,658 Expert Mod 8TB
the error is made on line 20. detailed info you can get from mysql_error().
May 14 '12 #8
i already review my code i cant kind point out where i made the mistake...can you help me debug it.. thanks help is much appreciated
May 14 '12 #9
Dormilich
8,658 Expert Mod 8TB
can you help me debug it
as I said, use mysql_error()
May 14 '12 #10
the error that i encounter when i put mysql_error() is

Expand|Select|Wrap|Line Numbers
  1. Parse error: syntax error, unexpected T_LOGICAL_OR in C:\xampp\htdocs\msicrevised\searcherresults.php on line 19
May 14 '12 #11
Dormilich
8,658 Expert Mod 8TB
a little bit more code would be incredibly helpful ...
May 14 '12 #12
actually i really dont have any idea what to do next whit that error that i am encountering:(
May 14 '12 #13
is their a problem with this part?

Expand|Select|Wrap|Line Numbers
  1.  $sql = mysql_query ("SELECT * FROM '%{$table}%' WHERE Rnum LIKE '%{$search}%'");
May 14 '12 #14
Dormilich
8,658 Expert Mod 8TB
that’s correct.

edit: table names must not be string quoted and can’t contain wildcard characters.
May 14 '12 #15
what can you say bout it? on my own understanding the error is this part '%{$table}%' and '%{$search}%'
May 14 '12 #16
but table name must be dynamic and must cope with what the user choose in the form that he submit..or you have a better idea to do it..
May 14 '12 #17
hey dude i found an alternative i got it working but maybe its a bad coding i dunno..i use if statement
May 14 '12 #18
Rabbit
12,516 Expert Mod 8TB
The fact that the user needs to choose the table dynamically indicates that your data is in a non-normalized format and you should really think about normalizing it so you don't have to deal with choosing a table.
May 14 '12 #19

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

Similar topics

0
by: Kate Perry | last post by:
I am trying to execute a query on a db2 database using the dbi module. I'm wondering if someone can take a look at my code and tell me what I'm doing wrong. I'm assuming it's a problem with my...
2
by: mp | last post by:
Hi, Is posible to make incremental search of some databse (table) with c#. I am looking for some example. I know how to do that with Delphi, but I am new in C# .NET and I need your help. ...
9
by: Peter | last post by:
Hello£¬everyone, My program will collect a testing machine's data ,save the data and deal with the data everyday. I want to use vb.net to create database, add and delete tables or modify the...
1
by: Casey | last post by:
Hi, How would one allow for possible multiple search criteria using FREETEXTTABLE. For example, my table "listings" has a full-text search catalog, and I may want to: SELECT * FROM listings...
0
by: wingtong | last post by:
Our Deltek accounting software will be migrating from a Jet database to a SQL express database. In the past, when using the Jet database, I have been able to make a copy of the JET database to my...
2
by: Cheeese | last post by:
Hello All, I need to search a particular keyword in a SQL database using VB6. I found that the asterix (*) does not work for the query and the record list obtained is empty. Here is...
4
by: kinannawaz | last post by:
I have been given a project to build a search engine using c++ i am enrolled in data structure course. I want to convert an html file into a .txt file than i have to search the page with the string...
0
by: mrsoft100 | last post by:
Dear Members I have written a program whose intention is to track fee payment by students in a school. the program is intended to track as many students as possible. My program works very well and...
12
lifeisgreat20009
by: lifeisgreat20009 | last post by:
I am a newbie to Struts and JSP...I have been working on the code below for 5 hours now..I googled a lot but couldn't get much help so finally I am here.. Hoping of getting my problem solved. Please...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.