database saving only password

Asked

Viewed 68 times

-1

This is my first post here, I’ve been following the forum for a long time but I never wanted to interact, today this desire arose along with a little problem ...

I’m learning to html, css, php and etc... from tutorials, after numerous failures I end up achieving what I want. I have the habit of watching several tutorials about the same time and after watching all decide which to use, based on the divergence of information between them and my intuition. Until it works, but something always goes wrong and then I start looking for content to solve. One of the problems that haunts me is that I don’t know how to cite the mistake happened, like this one that’s happening now. So I think here would be the best option.

The problem: `

<?php </br>
session_start();</br>
$_SESSION['message']  = '';

$mysqli = new mysqli('localhost','root','','accounts'); </br>

if ($_SERVER['REQUEST_METHOD'] == 'POST') { <br>
    if ($_POST['password'] == $_POST['confirmpassword']){ <br>
        $username = $mysqli->real_escape_string($_POST['username']); <br>
        $email = $mysqli->real_escape_string($_POST['email']); <br>
        $password = md5($_POST['password']);

        $sql = "INSERT INTO users (username, email, password) "
        . "VALUES ('$username', '$email', '$password')";
        //if the query
        if ($mysqli->query($sql) === true) {
            $_SESSION['message'] = "deu certo";
            header("location: welcome.php");
        }

        else {
            $_SESSION['message'] = "Usuario nao pode ser add";
            }
        }
    }
?>

Hard to understand that code tag, my God.

mysql workbench

This is the table I created: but when I go to check the db only has this: phpmyadmin

This is my first question, if I did something wrong please tell me. Thank you!

  • 1

    Post the form that brings the values, because this is a problem in it, not in Insert. Print the variable on the screen $sql, see if the values are really coming to her. Need to go doing tests to see where the problem is.

2 answers

1


Your problem is in the form that brings the data via POST.

You didn’t tag it name="username" in its input:

Current:

<input id="username" type="text" placeholder="Username" required autofocus>

Correct:

<input id="username" type="text" placeholder="Username" name="username" required autofocus>

<input id="email" type="email" placeholder="Valid Email" name="email" required autocomplete>

Moreover:

Your field password you’re only with 10 caracteres.

The guy md5 generates 32 caracteres.

Increase the field in your bank.

More about MD5: https://pt.wikipedia.org/wiki/MD5

  • Post the form to see how your coming POST

  • Edit your answer, post all your form content.

  • For here gave characters exceeded, published in Codepen codepen.io/Rytter/pen/bYRzGE -

  • Your inputs are missing... I’ll edit my answer

  • Thank you very much, it worked super well. I don’t even know how to thank you.

  • Opa ! quiet. But whenever you post, edit and ask and post everything on it. So help a next.

Show 1 more comment

-1

Come on, first take these off <br/> from within PHP, if you want to use them put in a echo. To avoid problems with quotation marks etc on your SQL utilize {}:

$sql = "INSERT INTO users (username, email, password) VALUES ('{$username}', '{$email}', '{$password}')";

If you want to know if the data is really coming from a:

echo "<pre>";
print_r($_POST);
echo "</pre>";

So you will see the entire POST Array() so you can understand where the problem comes from, whether it is in the POST or not.

  • Friend, thank you for your attention.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.