Jump to content

PHP with MySQL pulling data


kadams

Recommended Posts

Hi There,

 

I wonder if somebody could possibly help me.  I have some code which basically should allow a user to enter a city in and it should display the prefixes from my database.  If it cannot match the city the user has entered to what is in the database it should say it does not exist.

 

The problem I have it that it does not tell me if it cannot find a city it still says "Cities found for: blah blah" and displays nothing when it should display an error message.

 

My code is listed as below:

 

<!CITY FINDER FORM >

<html>

<form action="finder1.php" method="get">

<fieldset>

<legend>Enter your City</legend>

<input type="text" name="userinputcity" />

<input type="submit" value="Search Cities"/>

</fieldset>

</form>

 

<!POSTCODE FINDER FORM>

<form action="finder1.php" method="get">

<fieldset>

<legend>Enter your postcode prefix (S1,NG)</legend>

<input type="text" name="userinputarea" />

<input type="submit" value="Search area's"/>

</fieldset>

</form>

</html>

 

 

<!COLLECTS THE USER'S INPUT AND PUTS IT INTO THE $USERINPUT VARIABLE>

<?php $userinputcity = ""; ?>

<?php $result = ""; ?>

<?php $userinputcity = $_GET['userinputcity']; ?>

 

 

<?php

$checkprefix = "SELECT prefix from store WHERE city = '$userinputcity'";

 

 

//IF USER HAS PUT SOMETHING INTO TEXT BOX CHECK IT

if($userinputcity){

        print "Area's found for: " . $userinputcity;

 

                }

        else{

        print "Enter City or area prefix to search";

                }

 

 

$result = mysql_query($checkprefix) or die(mysql_error());

 

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

       

       

       

        if($row['city'] = '$userinputcity')

       

        {

                print "<br />";

 

                print "<br />";

 

                print "<br />";

 

                echo $row['prefix'];

 

        }

        else

        { 

 

                print "<br />";

                print "Cities not found, sorry.";

        }

 

}

 

 

 

?>

 

Link to comment
Share on other sites

Personally I would do something like:

 

<?php
if (isset($_POST['submit'])) {
  $userinputcity = $_GET['userinputcity'];
  
  if (empty($userinputcity)) {
    echo 'Please enter a city name.';
  }
  else {
    $result = mysql_query(sprintf("SELECT prefix FROM store WHERE city = '%s'", $userinputcity));
    
    if (mysql_num_rows($result) > 0) {
      echo 'Cities found for: ' . $userinputcity;
      
      while ($row = mysql_fetch_array($result)) {
        echo $row['prefix'] . '<br />';
      }
    }
    else {
      echo 'No cities found.';
    }
  }
}
?>

<!CITY FINDER FORM >
<html>
<form action="finder1.php" method="get">
<fieldset>
<legend>Enter your City</legend>
<input type="text" name="userinputcity" />
<input type="submit" value="Search Cities" name="submit" />
</fieldset>
</form>

<!POSTCODE FINDER FORM>
<form action="finder1.php" method="get">
<fieldset>
<legend>Enter your postcode prefix (S1,NG)</legend>
<input type="text" name="userinputarea" />
<input type="submit" value="Search area's" name="submit" />
</fieldset>
</form>
</html>

Link to comment
Share on other sites

Hi Guys,

 

Many thanks for your previous help with this question.  I'd like to ask one final question if I may.  At present the current set-up works, however there is 2 seperate boxes for a user to enter either a city in one box or a postcode prefix in another box.

 

I'd like to have the 2 merged together so that a user can enter either of these things in the one text box and still get the same results taken from my database.

 

Can anybody help me as to how I may do this, or provide me with the code to make this work?

 

Any further help would be much appreciated.

 

Thanks,

 

Kyle

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.