MySQL & PHP Search form

Codes here !

Moderators: egami, macek, gesf

Post Reply
User avatar
lyci2001
New php-forum User
New php-forum User
Posts: 3
Joined: Wed Dec 23, 2020 6:51 am

Wed Dec 23, 2020 6:59 am

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
php-forum Super User
php-forum Super User
Posts: 153
Joined: Mon Jun 08, 2020 2:00 am
Contact:

Wed Dec 23, 2020 2:45 pm

Which line is line 59?
User avatar
lyci2001
New php-forum User
New php-forum User
Posts: 3
Joined: Wed Dec 23, 2020 6:51 am

Thu Dec 24, 2020 6:24 am

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.
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 1222
Joined: Mon Feb 22, 2016 5:52 pm

Thu Dec 24, 2020 10:52 am

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
php-forum Super User
php-forum Super User
Posts: 153
Joined: Mon Jun 08, 2020 2:00 am
Contact:

Thu Dec 24, 2020 3:00 pm

If you're looking for a city that isn't in the database, what do you want $row to be?
User avatar
lyci2001
New php-forum User
New php-forum User
Posts: 3
Joined: Wed Dec 23, 2020 6:51 am

Fri Dec 25, 2020 8:43 am

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
php-forum Super User
php-forum Super User
Posts: 153
Joined: Mon Jun 08, 2020 2:00 am
Contact:

Mon Dec 28, 2020 9:07 am

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.
wernereegranad75
New php-forum User
New php-forum User
Posts: 5
Joined: Tue Apr 20, 2021 7:32 pm

Tue Apr 20, 2021 7:38 pm

When trying to access array offset on value of type null in its path will give me an error, can anyone help me fixt this issue? Anyone here playing the sims mobile game download? It is a life simulation game where you can create a virtual world for your character. There are lots of amazing things you can do here, one of them is to customize your character. You can also try different game mode which was added from the latest update. This game is available for pc windows 7 and higher version of OS
hamishheney5
New php-forum User
New php-forum User
Posts: 1
Joined: Mon May 03, 2021 1:26 am

Mon May 03, 2021 1:30 am

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

www.septickansascity.com
Colmilluke72
New php-forum User
New php-forum User
Posts: 1
Joined: Thu Aug 26, 2021 4:29 am

Thu Aug 26, 2021 4:36 am

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