PDA

View Full Version : If i had hair i'd be pulling it out! HELP!



ump
08-28-2008, 12:14 AM
Hi all,
I'm pretty new to PHP and i'm stuck.
I'm trying to query a database full of zip codes by a specific location ID. If the ZIP code entered in the form matches one of the zips associated with the ID i would like it to continue on the next form page. If not, simply say "sorry your zip code not found". Make sense?
Here's what i have so far:


<?php
$cxn = mysqli_connect("host","username" ,"pass", "db");
$zipEntry = $_POST['zip'];
$query = "SELECT ZIP FROM ZipCodes WHERE locationID = '550'";
$result = mysqli_query($cxn,$query)
or die ("Couldnt Execute.");
$row = mysqli_fetch_field($result);

if ( $zipEntry == $row['ZIP'] )
{
(go to some page. this i don't know how to code either)
}

{
echo "Sorry your Zip not found";
}
mysqli_close($cxn);
?>



I can get the code to return the first ZIP associated with the locationID, but if i search for any Zips within that location...nothing.

Thanks for the help!

Nile
08-28-2008, 12:16 AM
This should work:


<?php
$cxn = mysqli_connect("host","username" ,"pass", "db");
$zipEntry = $_POST['zip'];
$query = "SELECT ZIP FROM ZipCodes WHERE locationID = '550'";
$result = mysqli_query($cxn,$query)
or die ("Couldnt Execute.");
$row = mysqli_fetch_field($result);

if ( $zipEntry == $row['ZIP'] )
{
header("Location: url here");
} else {
echo "Sorry your Zip not found";
}
mysqli_close($cxn);
?>

ump
08-28-2008, 12:35 AM
"Warning: mysqli_query() expects parameter 1 to be mysqli, null given... on line 17
Couldnt Execute."
**scratching head**

Nile
08-28-2008, 12:41 AM
Please make sure that this is all your code.

ump
08-28-2008, 12:48 AM
I appreciate the help. Here's everything i have. Now im getting "Couldnt Execute"


<?php
$cxn = mysqli_connect("host" , "user" ,"pass", "db");
$zipEntry = $_POST['zip'];
$query = "SELECT ZIP FROM ZipCodes WHERE locationID = '550'";
$result = mysqli_query($cxn,$query)
or die ("Couldnt Execute.");
$row = mysqli_fetch_assoc($result);

if ( $zipEntry == $row['ZIP'] )
{
header("Location: http://www.google.com");
} else {
echo "Sorry your Zip not found";
}
mysqli_close($cxn);
?>

Nile
08-28-2008, 02:51 AM
Make sure that your in the need for assoc, instead of row, or array.

ump
08-28-2008, 05:07 PM
Thanks for the help so far! I've got it working however the code only works if i input the first zipcode for the locationID. If i submit any other Zips (even though they're in that location) it comes back as not found. I need it to query all Zips for the locationID.


<?php
$cxn = mysql_connect("host","user","pass")
or die ("Cant Connect. Something is broke");
$db = mysql_select_db( "dbname", $cxn );
$zipEntry = $_POST['zip'];
$query = "SELECT ZIP FROM ZipCodes WHERE locationID = '550'";
$result = mysql_query($query,$cxn)
or die (mysql_error());
$row = mysql_fetch_array($result);

if ( $zipEntry == $row['ZIP'] )
{
header("Location:http://www.google.com");
}
else
{
echo "Sorry your Zip not found";
}
mysql_close($cxn);
?>

ump
09-01-2008, 03:43 PM
Thanks to my girl who is a SQL Query nut letting me know im missing an "AND" in my statement. I appreciate the help!!