Page 1 of 1

MYSQL PHP Trying to display a result

Posted: Fri Mar 04, 2011 3:58 am
by DojiWeb
Hello. I am new to PHP and mySQL. I am trying to enter a zipcode into a form and return the price associated with the zip code.
Here is my code.

MY Form

<form action="pricezip.php" method="post">

ZIP CODE:<br/>
<input type="text" value="<?php echo $zip;?>" name="zip"/>

<br/>


<input type="submit" value="submit changes"/>

</form>

----------------------------------

MySQL query

<?php

mysql_connect("blah","blah","blah") or die("Error: ".mysql_error());

mysql_select_db("blah");


$zip = $_POST['zip'];

$sql = mysql_query("select price from ID where zip =" ' %$zip%'');
while ($results = mysql_fetch_array($sql)){
echo 'ID: '.$results['ID'];
echo '<br/>ZIP: '.$results['zip'];
echo '<br/> Price: '.$results['price'];
echo '<br/> Round Trip Price: '.$results['rt'];
echo '<br/><br/>';
}


?>

Can someone maybe help me out.

My table in SQL looks like this and my table name is ID.

zip price rt
97006 45 85
97232 40 75

Re: MYSQL PHP Trying to display a result

Posted: Fri Mar 04, 2011 6:23 am
by litebearer
1. where do you set the value for $zip in the form?
2. In

Code: Select all

$sql = mysql_query("select price from ID where zip =" ' %$zip%'');
Do you want the $zip variable to be an EXACT match with the table value

Code: Select all

$sql = mysql_query("select price from ID where zip = '$zip'");
OR do you want to see if it is part of the value

Code: Select all

$sql = mysql_query("select price from ID where zip LIKE '%$zip%''');
NOTE: your use of single an double quotes need to open and close properly

Re: MYSQL PHP Trying to display a result

Posted: Wed Mar 09, 2011 4:27 pm
by DojiWeb
I finally figured it out.. but still working on the overall form..
I have figured out how to post the data and search the database..
But I am thinking I want to implement AJAX or Jquery to make the
UI of the form a little more attractive.. So, now I am learning about that.
Anyone who has a good link for mysql php and ajax, I would appreciate it.