user6554670 user6554670 - 1 year ago 74
MySQL Question

Please review/comment: php mysql javascript (not school)

Many thanks to everyone. I have entered the solution Drew pointed out but am still having some issues. This could be due to mysql settings however i believe i have autocommit set properly etc. I'll post back soon.

Answer Source

I fixed up whatever looked wrong, not sure if I missed something.

Edit: forgot to mention, lookup password_hash and implement that. Storing password in plaintext is a no go.

Edit 2: Added the password hash function to get your started. Use password_verify to verify passwords on login.

<form onsubmit="return register(this);">
    <input name="uname" type="text" placeholder="please enter a user name"   required><br />
    <input name="upassword" type="text" placeholder="please enter a password or passphrase" required><br />
    <input id="register" name="register" type="submit" class="btn btn-primary btn-lg outline"/>


function register(form) {
    $.post("register.php", $(form).serialize());
    return false;



ini_set('display_errors', 1);

define('USER', 'root');
define('PASS', 'password');

try {
    $conn = new PDO("mysql:host=localhost;dbname=users;", USER, PASS);

    define('uname', $_POST['uname']);
    $passwordHash = password_hash($_POST['upassword'], PASSWORD_DEFAULT);
    define('upassword', $passwordHash);

    $sql = 'INSERT INTO `users` (name , password) VALUES (?, ?)';
    $conn->prepare($sql)->execute([uname, upassword]);
catch(PDOException $e)
    echo $sql . "<br>" . $e->getMessage();
$conn = null;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download