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



## machineros (Nov 5, 2006)

*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 :*


> CREATE TABLE `auto` (
> `id` int(4) NOT NULL,
> `company` varchar(12) NOT NULL,
> `model` varchar(12) NOT NULL,
> ...


*
The form looks as follow :*

```
<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 :*


```
//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 ?* :4-dontkno 

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.. :4-dontkno ray: 
_Thanks and waiting for your answers and help,
this was my 1st post to da forum _ray:


----------



## DJ-Zep (Mar 11, 2006)

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.


----------



## machineros (Nov 5, 2006)

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

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


```
<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>
```



```
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 :wave: :4-dontkno


----------



## DJ-Zep (Mar 11, 2006)

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.


----------



## machineros (Nov 5, 2006)

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 :4-dontkno


----------



## DJ-Zep (Mar 11, 2006)

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! :grin:


----------

