I can not insert data in my table

0

I am learning to insert users in my database, but the data I insert does not appear in the usuarios table, what is the problem? This is my code:

<?php
  if (isset($_POST['submit'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $connection = mysqli_connect('localhost', 'root', '', 'cs base de datos');
    if ($connection) {
      echo "We are connected";
    } else{
      die("failed");
    }
    $query = "INSERT INTO usuarios(username, password) ";
    $query .= "VALUES ('$username', '$password')"; 

    $result = mysqli_query($connection, $query); 

    if (!$result . mysqli_error()) {
      die('Query failed'); 
    }
  }

?>

For some reason, the users that I try to add when I type the username and password on the login form do not appear.

    
asked by Leon 14.04.2018 в 12:56
source

3 answers

0

There are several things you should check:

1- If you are using isset($_POST['submit']) to verify if information has been sent by POST, check that effectively in your html form you have some <input> with <name="submit"> .

<input type="submit" name="submit" value="Enviar">

or

<input type="hidden" name="submit" value="True">

2- If the column id is as " autoincrement " in the database

3- Once the issue is working, you should save the password at least as a hash. PHP has several built-in hash functions like MD5 (not recommended), SHA, etc.

$password = hash('sha256', $_POST['password'], False);
    
answered by 14.04.2018 / 19:50
source
0

The code is correct. You should add what error it returns, but I get the feeling that when creating the table you have not set the field id as Auto_Increment . I see you use phpmyadmin Delete the table (it's empty), create it again by checking A_I when creating the field id and I think your problems will be solved.

    
answered by 14.04.2018 в 18:57
0

Reviewing your code more closely, you can see that here:

if (!$result . mysqli_error()) {

you make an incorrect evaluation. It's like you do this:

$result=TRUE;
$error="Hubo un error"; 
if (!$result.$error){
    die ("Query failed");
}

The code will always enter the if because you're asking if the concatenation !$result . mysqli_error() is not FALSE . And it never will be, nor should this happen:

$result=FALSE;
$error="Hubo un error"; 
if (!$result.$error){
    die ("Query failed");
}

Here I would also enter the if .

Then, taking into account that $result would store the result of mysqli_query and that this would be FALSE if the query failed, you can write the code like this:

<?php
  if (isset($_POST['submit'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $connection = mysqli_connect('localhost', 'root', '', 'cs base de datos');
    if ($connection) {
      echo "We are connected";
    } else{
      die("failed");
    }
    $query = "INSERT INTO usuarios (username, password) ";
    $query .= "VALUES ('$username', '$password')"; 

    $result = mysqli_query($connection, $query); 

    if (!$result) {
      die('Query failed '.mysqli_error()); 
    } else {
      echo "Se insertaron: ".mysqli_affected_rows($connection). " registros";
    }
  }

?>

That way, when it fails, it will tell you what the error was, because you are evaluating only the variable $result and not a concatenation of it.

If it still does not work, comment on the error that is printed on the screen, next to Query failed .

Note that I have used mysqli_affected_rows to indicate the number of records inserted, when the query works. If this function does not fall within the scope of your course, you can remove it.

  

NOTE:

     

Once the present error is corrected, consider shielding your code   against the very serious security risk called SQL Injection, a   through which a malicious user could take control not   only from your database, but from the entire system . If this type of solution is acceptable for your case (since you have commented that it is a course), you can tell me in comment and I show you how to shield the code.

    
answered by 14.04.2018 в 19:20