V
V
Vyacheslav Marvin2020-05-06 14:17:19
PHP
Vyacheslav Marvin, 2020-05-06 14:17:19

Passing empty values ​​to the database, how to solve it?

Hello.

Faced such a problem. When loading the page, it passes empty values ​​to the database, after which it gives an error.

$link = mysqli_connect($host, $user, $password, $database) or die("Ошибка " . mysqli_error($link));

    // выполняем операции с базой данных
    $name = $_POST['name'];
    $lastname = $_POST['lastname'];
    $email = $_POST['email'];
    $pass = $_POST['pass'];

    $sql = "INSERT INTO `users` (`name`, `lastname`, `email`, `password`) VALUES ('$name', '$lastname', '$email', '$pass')";

    if (mysqli_query($link, $sql)) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($link);
    }

    // закрываем подключение
        mysqli_close($link);


Gives this error
Notice: Undefined index: email in D:\XAMPP\htdocs\PB\registration.php on line 24
As far as I understand. It does not see the values ​​of these variables. But they are not even filled in, for some reason he immediately sends blanks to the database. They are written there, and after reloading the page it gives an error
Warning: mysqli_error() expects parameter 1 to be mysqli, null given


If I connect a send check, by type
if (isset($_POST[]))
then nothing changes. The warnings remain.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Entelis, 2020-05-06
@Marcheslav

...
Well, everything is written the same.
You have opened the page.
Executed Since there is no post request, it does not exist, null is written to the $name variable and a notice is thrown about it. Notis does not stop the script and everything is executed further. The base includes an insert of null, which is absolutely valid. should generally cause a syntax error, because this is a meaningless entry. If check - then check specific values
$name = $_POST['name'];
if (isset($_POST[]))

if (isset($_POST['name']) && isset($_POST['lastname']) && ... и тд) {

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question