MySQL & PHP Search form

A

Anonymous

Guest
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:
<?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>
 
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.
 
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
 
If you're looking for a city that isn't in the database, what do you want $row to be?
 
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.
 
Ok. The line above that:

Code:
$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.
 
Back
Top