Jump to content

Another small issue - SELECT error


spacepoet

Recommended Posts

Hi:

 

I have another small issue like the one I just posted about.

 

I am trying to get the full state name based upon the state abbreviation.

 

Like this:

<?php

$query=mysql_query("SELECT full_state FROM zip_codes WHERE abbr_state = abbr_state") or die("Could not get data from db: ".mysql_error());

$full_state=$result['full_state'];

?>

...

State Name: <input type="text" name="full_state" value="<?php echo $full_state; ?>" /><br />
[code]

No errors, but also no "full_state" appears.

(It is pulling in the correct "abbr_state")

What am I missing here?

Link to comment
Share on other sites

No, that code is wrong. $result['full_state'] won't exist because the data is never fetched form the result resource.

 

<?php
$abbr_state = mysql_real_escape_string($_POST['abbr_state']);
$query = "SELECT full_state FROM zip_codes WHERE abbr_state = '$abbr_state'";
if( !$result = mysql_query($query) ) {
echo "<br>Query: $query<br>Failed with error: " . mysql_error() . '<br>';
} else {
$array = mysql_fetch_assoc($result);
$full_state=$array['full_state'];
}
?>

Link to comment
Share on other sites

Hi:

 

Thanks, but that didn't work either. . .

 

This is the full page I'm working with (now including your code):

<?php

include('include/myConn.php');
include('include/myCodeLib.php');

$abbr_state = $_REQUEST['abbr_state'];

$error = NULL;
$lat = NULL;
$lat = NULL;
$city = NULL;
$full_state = NULL;
$abbr_state = NULL;
$zip = NULL;


if(isset($_POST['submit'])) {

$lat = $_POST['lat'];
$lon = $_POST['lon'];
$city = $_POST['city'];
$full_state = $_POST['full_state'];
$abbr_state = $_POST['abbr_state'];
$zip = $_POST['zip'];


if(empty($city)) {
  $error .= '
  
Enter your City

';
}

if($error == NULL) {

$sql = sprintf("INSERT INTO zip_codes(lat,lon,city,full_state,abbr_state,zip) VALUES ('%s','%s','%s','%s','%s','%s')",

mysql_real_escape_string($lat),
mysql_real_escape_string($lon),
mysql_real_escape_string($city),
mysql_real_escape_string($full_state),
mysql_real_escape_string($abbr_state),
mysql_real_escape_string($zip));

if(mysql_query($sql)) {

$lat = $_REQUEST['lat'];
$lon = $_REQUEST['lon'];
$city = $_REQUEST['city'];
$full_state = $_REQUEST['full_state'];
$abbr_state = $_REQUEST['abbr_state'];
$zip = $_REQUEST['zip'];

header("Location: CityList.php?abbr_state=$abbr_state");

  }

  else {
    $error .= 'There was an error in our database, please try again!';
  }
}
}

?>

<html>
<body>


<?php

$abbr_state = mysql_real_escape_string($_POST['abbr_state']);
$query = "SELECT full_state FROM zip_codes WHERE abbr_state = '$abbr_state'";
if( !$result = mysql_query($query) ) {
   echo "<br>Query: $query<br>Failed with error: " . mysql_error() . '<br>';
} else {
   $array = mysql_fetch_assoc($result);
   $full_state=$array['full_state'];
}
?>



<?php echo '<p><span class="textError">' . $error . '</span></p>';?>

<form name="myForm" action="" method="post">

Latitude: <input type="text" name="lat" value="<?php echo $lat; ?>" /><br />
Longitude: <input type="text" name="lon" value="<?php echo $lon; ?>" /><br />
City Name: <input type="text" name="city" value="<?php echo $city; ?>" /><br />
State Name: <input type="text" name="full_state" value="<?php echo $full_state; ?>" /><br />
State Abbreviation: <input type="text" name="abbr_state" value="<?php echo $_GET['abbr_state'] ?>" readonly /><br />
Zip Code: <input type="text" name="zip" value="<?php echo $zip; ?>" /><br />

<input type="submit" name="submit" value="Submit New City" />

</form>

</body>
</html> 

 

Can you see what I'm missing?

Link to comment
Share on other sites

Hi:

 

I mean the field is empty:

State Name: <input type="text" name="full_state" value="<?php echo $full_state; ?>" /><br />

 

I'm trying to auto-populate that field with the full state name.

 

I'm not getting any errors, which is why I'm a bit baffled.

 

This is the link used to get to the page:

<a href="NewCity.php?abbr_state=<?php echo $abbr_state ?>">Insert New City</a>

 

Maybe I should try carrying-over the "abbr_state" AND the "full_state" from that link??

Link to comment
Share on other sites

Hi:

 

It pulls the value from a database on the first page like this:

<?php

include('include/myConn.php');

$result = mysql_query("SELECT city,abbr_state,zip FROM zip_codes WHERE abbr_state = '" . mysql_real_escape_string ( $_GET['abbr_state'] ) . "' ORDER BY `city` ASC");

while($row = mysql_fetch_array($result)){
echo $row['city']. " - ". $row['zip']. " - ". $row['abbr_state'] ;
echo "<br />";
}

?>

......
<a href="CityList.php?abbr_state=AL">Alabama</a><br />
<a href="CityList.php?abbr_state=AK">Alaska</a><br />
<a href="CityList.php?abbr_state=AZ">Arizona</a><br />
etc...

......

<a href="NewCity.php?abbr_state=<?php echo $abbr_state ?>">Insert New City</a>

 

And yes, it is assigning the the correct value for each state.

 

Maybe I should pull the full_state from here and send it over this way

(I am currently not SELECTING it, but I obviously can)?

 

How would I adjust the link, if that's the route I take?

 

Thanks for the help.

Link to comment
Share on other sites

You have this: $abbr_state = $_REQUEST['abbr_state']; inside the if( isset($_POST['submit']) ) { conditional. Therefore if the page is arrived at via a link such as script.php?abbr_state=TX, the condition is FALSE, and the value is not assigned to $abbr_state.

Link to comment
Share on other sites

OK, but why then does this field:

State Abbreviation: <input type="text" name="abbr_state" value="<?php echo $_GET['abbr_state'] ?>" readonly /><br />

 

Get the proper data (state abbreviation) filled in?

 

Maybe I should try to pull the full_state in this way as well.

 

Didn't think this was going to be so hard .. lol ..

Link to comment
Share on other sites

Would you try print_r($_POST); and see if it returns any value for "Full_State"?

 

It looks like your using both $_GET and $_POST and I am not sure why? - If you get all the needed values from the post array from the first page - why even mess with the get from the url?

 

 

Link to comment
Share on other sites

Hi:

 

I'm flying a bit blind on this, but I know it's all working fine .. just this one little issue I can't figure out.

 

Would you try print_r($_POST); and see if it returns any value for "Full_State"?

 

I will add it, but I'm honestly not sure where or how - I'm still pretty new with the PHP syntax and such,

which is why some of this is a bit maddening to figure out.

 

:)

Link to comment
Share on other sites

No problem,

 

Try putting the print_r just before

if(isset($_POST['submit'])) {

 

That way you can see the contents of the $_POST array before doing anything with it. The main question we need to help you is "where are you pulling your data from? once we know that we can test where the code is not working.

Link to comment
Share on other sites

Hi:

 

Well the print_r($_POST); did not return any data, but I was able to get it to work doing in this way:

<?php
$abbr_state = $_REQUEST['abbr_state'];
$full_state = $_REQUEST['full_state'];
?>
...
<a href="CityList.php?abbr_state=WY&full_state=Wyoming">Wyoming</a>
...
<a href="NewCity.php?abbr_state=<?php echo $abbr_state; ?>&full_state=<?php echo $full_state; ?>">Insert New City</a>

 

Then on the next page:

$abbr_state = $_REQUEST['abbr_state'];
$full_state = $_REQUEST['full_state'];

...
State Name: <input type="text" name="full_state" value="<?php echo $_GET['full_state'] ?>" readonly /><br />
State Abbreviation: <input type="text" name="abbr_state" value="<?php echo $_GET['abbr_state'] ?>" readonly /><br />

 

Still would like to know why the original way didn't work, but at least I can proceed with this project.

 

Thanks for the pointers!

 

One thing:

 

print_r($_POST);

 

basically used to print errors?

Link to comment
Share on other sites

print_r() is usefull for printing out objects and their contents. So if you have an array that you want to see what values are inside of it - print_r() is your friend. It is mostly used for debugging.

 

If the $_POST array was totally empty that means that you did not fill in the pervious page. $_POST are values that are sent through a form on submit with an action of "post" (at least that is one common way of sending post values). $_GET is passed through the url so http://example.com/index.php?name=bob&age=36 has 2 values stored in the $_GET array... name=>bob, age=>36 etc.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.