phpnoob
phpnoob

Reputation: 5

php mysql postcode check

Hello I am only starting to learn php can anyone help. I need to be able to check database for delivery postcode.

I have a table called delivery with two columns one is ID and other Postcode.

There will be several pre-recorded postcodes in database. I have an input in a form for user to input postcode. I need to check this postcode with database and if it exists continue if not show error.

Heres what I have so far.

<?php
include 'mysql_connect.php';

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

        $postcodeQuery = sprintf("SELECT `Postcode` FROM delivery WHERE Postcode = '%s' LIMIT 1",
                    mysql_real_escape_string($_POST['inputPostcode']));

        $postcodeResult = mysql_query($postcodeQuery);
        $delivery           = mysql_fetch_array($postcodeResult, MYSQLI_ASSOC);

        if ($_POST['inputPostcode'] == 'postcode')

        echo 'We deliver to your area'.($_POST['postcode']);

        } else{

        echo 'We do not deliver to your area';

}

?>

Please help kinda stuck!!

Upvotes: 0

Views: 403

Answers (1)

Lars
Lars

Reputation: 5799

<?php
include 'mysql_connect.php';

if(isset($_POST['submit'])) {
        echo 'Please enter an email address';

        $postcodeQuery = sprintf("SELECT `Postcode` FROM delivery WHERE Postcode = '%s' LIMIT 1",
                    mysql_real_escape_string($_POST['inputPostcode']));

        $postcodeResult = mysql_query($postcodeQuery);
        $delivery       = mysql_fetch_array($postcodeResult, MYSQLI_ASSOC);

        if ($_POST['inputPostcode'] == $delivery[0])
            echo 'We deliver to your area '.($_POST['postcode']);
        else
            echo 'We do not deliver to your area';

}

This one should work - have a look at the manual page for mysql_fetch_array() for the example.

Upvotes: 1

Related Questions