If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Why does _POST not make SQL query

Started by vingler, Sep 21, 2022, 01:42 AM

Previous topic - Next topic

vinglerTopic starter

What could be the reason for _POST not working properly with SQL queries? Despite running the code correctly, I keep getting an unsuccessful message. Is there something wrong with my code or am I missing something?

<?php
 
if (isset($_POST["postlogin"])){
     
$urllogin $_POST["postlogin"];
     
$urlpassword md5($_POST["postpassword"]);
 
     
$check_user mysqli_query($connect"SELECT * FROM user WHERE login = '$urllogin' AND password = '$urlpassword'");
     
     if(
mysqli_num_rows(mysqli_query($connect$check_user) > 0))
     {
     echo 
"POST request successful: Correct";
     }
     else
     {
         echo 
"POST request successful: False";
     }
}
?>

<?php
 
    $connect 
mysqli_connect('localhost''user',  'password',  'database');
 
    if (!
$connect) {
        die(
'Error connecting to the database');
    }

Server log: PHP warning: mysqli_query() expects parameter 1 to be mysqli, null value specified in
Note: The variable is not defined.
  •  


virtuatechnologies

To prevent SQL injections, it is not safe to pass them in their raw form and instead, mysqli_real_escape_string must be used.

Additionally, the comparison operator > 0 is missing from the code. It is important to verify that connect.php is connected to index.php. The error message suggests that the $connect variable is empty.

To ensure the security of SQL queries, it is recommended to use parameterized prepared statements which can help prevent SQL injection attacks. In addition, it is always good practice to thoroughly validate and sanitize user inputs before running them in SQL queries.
  •  

indiaseos

Based on the provided code, it looks like the issue might be with the `$connect` variable not being defined. The `mysqli_connect()` function is used to establish a connection to the database, but it seems that the connection is not being successfully established.

You should make sure that the correct values for the host, username, password, and database name are provided when calling the `mysqli_connect()` function. Additionally, ensure that the PHP MySQL extension is enabled in your server configuration.

Here's an example of how you can define the `$connect` variable properly:

```php
$host = 'localhost';
$username = 'user';
$password = 'password';
$database = 'database';

$connect = mysqli_connect($host, $username, $password, $database);

if (!$connect) {
    die('Error connecting to the database');
}
```

Make sure to replace `'localhost'`, `'user'`, `'password'`, and `'database'` with the appropriate values for your setup.

Once you have successfully established the database connection, you can proceed with your SQL queries using `$connect` as the first parameter for the `mysqli_query()` function.


Here's an expanded version of your code with some improvements and explanations:

```php
<?php

$host = 'localhost';
$username = 'user';
$password = 'password';
$database = 'database';

$connect = mysqli_connect($host, $username, $password, $database);

if (!$connect) {
    die('Error connecting to the database');
}

if (isset($_POST["postlogin"]) && isset($_POST["postpassword"])) {
    $urllogin = $_POST["postlogin"];
    $urlpassword = md5($_POST["postpassword"]);

    // Make sure to use the $connect variable when calling mysqli_query()
    $check_user_query = "SELECT * FROM user WHERE login = '$urllogin' AND password = '$urlpassword'";
    $check_user_result = mysqli_query($connect, $check_user_query);
   
    if(mysqli_num_rows($check_user_result) > 0) {
        echo "POST request successful: Correct";
    }
    else {
        echo "POST request successful: False";
    }
}
```

In this updated code:

1. I added the necessary host, username, password, and database variables to establish a connection using `mysqli_connect()`.
2. I used `isset()` to check if both `$_POST["postlogin"]` and `$_POST["postpassword"]` exist before proceeding with database queries.
3. I assigned the SQL query to the `$check_user_query` variable for readability.
4. I called `mysqli_query()` using `$connect` as the first parameter for executing the SQL query.
5. I used `$check_user_result` to store the result returned by `mysqli_query()`.
6. Finally, I adjusted the condition in the `if` statement to use `mysqli_num_rows()` with `$check_user_result` to check if any rows were returned from the query.

Make sure to replace `'localhost'`, `'user'`, `'password'`, and `'database'` with the appropriate values for your setup.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...