Difficulty in adding data in db using PHP and MYSQL

0

Well, I'm starting the studies in php and mysql, and I'm having a hard time doing something theoretically simple, which would be a form that sends the information provided to a local db. When pressing send, nothing happens, the db table remains intact. Thanks in advance for the time you have to help me

<?php
session_start();

$mysqli = new mysqli('localhost', 'root', '', 'dbteste');

if($_SERVER['REQUEST_METHOD'] == 'POST'){

    if($_POST['password'] == $_POST['confirmpassword']){
        $username = $_POST['username'];
        $email = $_POST['email'];
        $password = $_POST['password']; //md5 hash passwor security

        $sql = "INSERT INTO user (username, password, email) VALUES($username, $password, $email)";

        $mysqli->query($sql);

    }
}

?>
    <h1>Register</h1>
<form class="form" action="create-account.php" method="post" enctype="multipart/form-data" autocomplete="off">
    <input type="text" name="username" value="" placeholder="username" required/>
    <input type="password" name="password" value="" placeholder="password" required/>
    <input type="password" name="confirmpassword" value="" placeholder="confirm password" required/>
    <input type="text" name="email" value="" placeholder="e-mail" required/>
    <input type="submit" name="createacount" value="create acount">
</form>
    
asked by anonymous 04.05.2017 / 14:07

2 answers

0

I believe it is an error in creating the SQL command, try using:

$sql = "INSERT INTO user (username, password, email) VALUES('".$username."', '."$password."', '".$email."')";

Use these commands to show the errors if any:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

I also recommend that you do some research on SQL Injection.

    
04.05.2017 / 16:03
0

PHP variables in VALUES need to be enclosed in single quotes!

$ sql="INSERT INTO user (username, password, email) VALUES ('$ username', '$ password', '$ email')";

I do not know about $ _SERVER, I do it like this:

$ con = mysqli_connect ("localhost", "root", "") or die (mysqli_error ()); mysqli_select_db ($ with, $ bank) or die (mysqli_error ());

    
06.05.2017 / 07:11