Click here to Skip to main content
15,904,822 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I got this error

Fatal error: Uncaught TypeError: mysqli_query(): Argument #1 ($mysql) must be of type mysqli, bool given in C:\xampp\htdocs\Login\signup.php:16 Stack trace: #0 C:\xampp\htdocs\Login\signup.php(16): mysqli_query(false, 'insert into use...') #1 {main} thrown in C:\xampp\htdocs\Login\signup.php on line 16


Please help me on how to fix it. Here's the code

if (!empty($user_name) && !empty($password)) {
    // save to db
    $user_id = random_num(20);
    $query = "insert into users (user_id, user_name, password) values ('$user_id',' $user_name',' $password')";
    mysqli_query($query);

     header("Location: login.php");
     die;
  } else {
    {
      echo "Please enter some valid information";
    }
  }
}


What I have tried:

I tried adding mysqli_query($con, $query);
Posted
Updated 13-Nov-21 0:10am

If you check the documentation for PHP: mysqli::query - Manual[^] you will clearly see that when the query statement fails the call returns the boolean value FALSE. Do not assume that your API calls always do what you think; check the return values first.
 
Share this answer
 
v2
Concerning the data you're inserting, you should never store passwords as plain text. When your system is compromised, all usernames and passwords are easily readable and this will cause huge problems. To handle passwords correctly, have a look at Password Storage: How to do it.[^]

Another thing is that you concatenate the input from the user directly to the SQL statement. This leaves you wide open to SQL injection - Wikipedia[^]. To prevent this, use bind variables. For more info, see PHP: mysqli_stmt::bind_param - Manual[^]

Third observation is the key generation. At the moment you generate a random number you use as the id for the user. There's no guarantee that the number isn't used previously. A better way is to let the database to generate the key. For example if you're using MySQL, have a look at MySQL :: MySQL 8.0 Reference Manual :: 3.6.9 Using AUTO_INCREMENT[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900