MySQL & PHP Search form

Codes here !

Moderators: gesf, Michalio

Post Reply
lyci2001

I have a problem with my project acctualy evrithing works but when I put the reserch city who is not in the database at the place to have a messege I got this : Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\index.php on line 59. Please someone help! Thanks

Code: Select all

<?php
require('inc_connexion.php');

?>
<!doctype html>
<html>
    <head>
        <title>acceuil</title>
        <link rel="stylesheet" type="text/css" href="css/style.css" />
    </head>
    <body>
        <div class="content">
            <center>
                <h1>Formulaire de recherche de villes</h1>
<?php
if(isset($_GET['submit']))
{
//On recupere les valeurs du formulaire : on utilise mysqli_real_escape_string pour se proteger legerement des injections sql (meme si il faudrait plutot des requetes preparés)
print_r($_GET);

$first_name =  $mysqli->real_escape_string($_GET['first_name']);
$name =  $mysqli->real_escape_string($_GET['name']);
$city_name =  $mysqli->real_escape_string($_GET['user_city_name']);

if(empty($first_name) OR empty($name) OR empty($city_name)){

    $message = '<p>veuillez remplir tous les champs</p>';

    }else{

        if($mysqli->query('INSERT INTO users(first_name, name, city_name_user) VALUES (" '.$first_name. ' ", " ' .$name . ' ", " ' .$city_name . ' ") ')){

            $message = '<p>votre prénom ' .$first_name.' et votre nom ' .$name . ' ont ete enregistre.</p>';

                }else{

                    $message = '<p>votre prénom ' .$first_name.' et votre nom ' .$name . ' n ont pas ete enregistre.</p>';
                }
        }

            if($name AND $city_name ){

                $req = $mysqli->query('SELECT*FROM users WHERE first_name = " ' .$first_name . ' " AND name = " ' .$name. ' " ');

                    $message = '<p>votre ville n est pas dans la liste.</p>';

                    print_r($req);

                $row = $req->fetch_array();

                $id_name = $row['user_id'];

                  if($result = $mysqli->query("SELECT city_id FROM city WHERE city_name = '" . $city_name . "'"));

                print_r($result);

                $row = $result->fetch_array();

                    $id = $row['city_id'];

                        if($mysqli->query ('INSERT INTO user_searchs(user_id, city_id) VALUES (" '.$id_name. ' " , " ' .$id. ' " ) ')){

                            $message = '<p>vos informations ont ete enregistre.</p>';

                                }else{

                                    $message = '<p>vos informations n ont pas ete enregistre.</p>';
                                }
                        }
        }       
?>

<?php if(isset($message)) echo $message; ?>

                <form action="index.php" method="GET">
                    <p>quel est votre prenom : <input type="text" name="first_name" placeholder="votre prénom" /></p>
                    <p>quel est votre nom : <input type="text" name="name" placeholder="votre nom" /></p>
                    <p>quel ville recherchez vous : <input type="text" name="user_city_name" placeholder="votre ville" /></p>
                    <p><input type="submit" name="submit" value="recherche" /></p>
                </form>
            </center>

         </div>

    </body>

</html>
simonbrahan

Which line is line 59?
lyci2001

This is the line 59:
$id = $row['city_id'];
Acctualy she work when you make your testing with some city who is alredy in the database, but when you test with some city who's not in the database done this:
C:\xampp\htdocs\index.php on line 59.
hyper

The problem that you have is that you are looking for a key in your array and the key does not exist.

A couple of possible solutions:

1) Include the city_id in your SQL query as a parameter

2) Check that the key exists in your array
simonbrahan

If you're looking for a city that isn't in the database, what do you want $row to be?
lyci2001

Yes it is the $row is looking the city in the database. The things go perfect when I test with some of the city who are in the database. But when I put some city who is not in the database I have a error my cycle didn't exit to return the value od $ message but tell me error.
simonbrahan

Ok. The line above that:

Code: Select all

$row = $result->fetch_array();
attempts to fetch a row from your result. If there is no result then "$row" will be "NULL" - that's what's happening here. You need to check that "$row" is actually what you expect before attempting to use it.
hamishheney5

I'm having a hard time dealing with the error I got when connecting MySQL to the PHP search form.

www.septickansascity.com
Colmilluke72

I have the same problem. I am thankful that find this forum, it gives me an idea of how to fix issues. popcorn ceiling removal chattanooga
Post Reply