Welcome to Tech Support Forum home to more then 136,000 problems solved. Issues have included: Spyware, Malware, Virus Issues, Windows, Microsoft, Linux, Networking, Security, Hardware, and Gaming Getting your problem solved is as easy as:
1. Registering for a free account
2. Asking your question
3. Receiving an answer

Registered members:
* Get free support
* Communicate privately with other members (PM).
* Removal of this message
* See fewer ads.
* And much more..

 





Want to know how to post a question? click here Having problems with spyware and pop-ups? First Steps
Go Back   Tech Support Forum > Design Forum > Web Design & Programming
User Name
Password
Site Map Register Donate Rules Blogs Mark Forums Read

Web Design & Programming Discussion of web design, and server-side & client-side scripting

Reply
 
Thread Tools
Old 11-05-2006, 05:37 AM   #1 (permalink)
Registered User
 
machineros's Avatar
 
Join Date: Nov 2006
Posts: 3
OS: Win


Question Help me to connect a form with php to mysql !!

Hello guys,
i need your help please..
am trying to "connect" with Php a FORM to my mysql database so that i can execute like this any possible query that will result from the use of this form.
My database keeps data for cars and the table that i made is here :

Quote:
CREATE TABLE `auto` (
`id` int(4) NOT NULL,
`company` varchar(12) NOT NULL,
`model` varchar(12) NOT NULL,
`color` varchar(12) NOT NULL,
`fuel` varchar(12) NOT NULL,
`year` int(4) NOT NULL,
`price` int(7) NOT NULL
)

The form looks as follow :

Code:
<form method="post" action="search.php">

<input type="text" name="search" size=25 maxlength=25>

<select name="search2">
<option value="">""</option>
<option value=skoda>fiat</option>
<option value=hunday>hunday</option>
<option value=bmw>reno</option>
<option value=reno>toyota</option>
<option value=fiat>skoda</option>
<option value=mercedes>audi</option>


<input type="Submit" name="Submit" value="Submit">

</form>

And the not succesfull php code that i tried to write here :

Code:
//connect to mysql

mysql_connect("localhost","labuerga","labuerga"); 
	

mysql_select_db("amaxadiko"); 

$search=$_POST["search"]        ;
$search2=$_POST["search2"]        ;
//get the mysql and store them in $result

$result = mysql_query("SELECT * FROM auto WHERE company = '" . $_POST['$search'] . "' AND model = '" . $_POST['search2'] ."'");

//grab all the content
while($r=mysql_fetch_array($result))
{	
  
   $marka=$r["company"]        ;
   $modelo=$r["model"]        ;
   
   
   //display the row
   echo "$company <br> $model <br><br>";
}

At general, this that i dont understand the most is which & how we can write the correct query in the php code from the moment that every user that uses the FORM can according to his choices and combination to have a different query ?

As you see am quite confused..much :) :( ..so plz try to help me guys and code examples are always better for me to understand
Help to make it work..
Thanks and waiting for your answers and help,
this was my 1st post to da forum

Last edited by machineros : 11-05-2006 at 05:43 AM.
machineros is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Bookmark on Thread SoupReddit!
Reply With Quote
Old 11-05-2006, 12:36 PM   #2 (permalink)
Design Team
 
DJ-Zep's Avatar
 
Join Date: Mar 2006
Location: Philadelphia
Posts: 1,496
OS: Windows Server 2003

My System

In the last code that does all the processing, towards the bottom I think you spelled "model" wrong. You spelled it "modelo". Maybe that's the problem? Everything else looks perfect to me.
__________________

Nvu || Notepad++ || Apache || PHP || Keep TSF Alive

"The great thing about a computer notebook is that no matter how much you stuff into it, it doesn't get bigger or heavier."
DJ-Zep is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Bookmark on Thread SoupReddit!
Reply With Quote
Old 11-05-2006, 02:59 PM   #3 (permalink)
Registered User
 
machineros's Avatar
 
Join Date: Nov 2006
Posts: 3
OS: Win


DJ-Zep, finally is fixed by getting help & reading
..but i came to the point that i face an other problem now

Look here how it seems my form and php part now :

Code:
<form method="post" action="search.php">
 
  <select name="search"> 
  <option value="">""</option> 
  <option value="fiat">fiat</option>
   <option value="hunday">hunday</option> 
   <option value="reno">reno</option> 
   <option value="toyota">toyota</option> 
   <option value="skoda">skoda</option> 
   <option value="audi">audi</option>
    </select> 
    
    <input type="text" name="search2" size=25 maxlength=25>
    
    <input type="Submit" name="Submit" value="Submit"> </form>


Code:
mysql_connect("localhost","nn","nnn");


mysql_select_db("amaxadiko");

$search=$_POST['search'];
$search2=$_POST['search2'];


$result = mysql_query("SELECT * FROM auto WHERE company = '$search' AND model = '$search2'");

$numRows = mysql_num_rows($result);

if( $numRows == "0" ){

echo "No Results Found";

} else {

while($r=mysql_fetch_array($result))
{

$company=$r['company'];
$model=$r['model'];



echo "$company<br> $model<br>";
}

}
So now the problem-question is :
How we can fix it in such a way so that when one of the 2 elements(select - input ) is empty then to dont write echo "No Results Found"; but to write the result of the element that is selected only .
Par example, if is selected only in select SKODA, but the next input text is EMPTY then to dont write "No results found" but to write the SKODAs that exist in database .

..am waiting for your help
machineros is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Bookmark on Thread SoupReddit!
Reply With Quote
Old 11-05-2006, 06:55 PM   #4 (permalink)
Design Team
 
DJ-Zep's Avatar
 
Join Date: Mar 2006
Location: Philadelphia
Posts: 1,496
OS: Windows Server 2003

My System

Would you mind giving me another example of what you're looking for? I think I know what you mean, but I'm not certain.
__________________

Nvu || Notepad++ || Apache || PHP || Keep TSF Alive

"The great thing about a computer notebook is that no matter how much you stuff into it, it doesn't get bigger or heavier."
DJ-Zep is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Bookmark on Thread SoupReddit!
Reply With Quote
Old 11-06-2006, 01:24 AM   #5 (permalink)
Registered User
 
machineros's Avatar
 
Join Date: Nov 2006
Posts: 3
OS: Win


Question

I mean when we have this case :

As you can see one of those 2 elements of the FORM the input text in this case is empty, so in this case the result that am going to take is the => echo "No Results" . But i want when one element is empty then to show-write results from the elemenets that are not empty.

In this case par example, i want to write all skodas even that we havent set any model in the input and remains empty . So i want to get results when a field is empty and others are not and not the echo "No results". How we can make it ? plz help

Last edited by machineros : 11-06-2006 at 01:27 AM.
machineros is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Bookmark on Thread SoupReddit!
Reply With Quote
Old 11-07-2006, 07:23 PM   #6 (permalink)
Design Team
 
DJ-Zep's Avatar
 
Join Date: Mar 2006
Location: Philadelphia
Posts: 1,496
OS: Windows Server 2003

My System

I see what you're saying. I'm trying to compile a forevery statements nested within an if statement so that when the field is empty it will do what you want it to. I just need a little time to make sure everything is going right. I haven't forgotten about you!
__________________

Nvu || Notepad++ || Apache || PHP || Keep TSF Alive

"The great thing about a computer notebook is that no matter how much you stuff into it, it doesn't get bigger or heavier."
DJ-Zep is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Bookmark on Thread SoupReddit!
Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



All times are GMT -7. The time now is 06:57 PM.



Copyright 2001 - 2008, Tech Support Forum

Search Engine Friendly URLs by vBSEO

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82