0

Whenever I submit my php form, the data shows up blank in the database. What is not working? I've tried to setup the code different, but no matter what I do, the values ends up being blank.

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if (isset($_POST["email"]))
{
    $login = $_POST["email"];
} 
else 
{
    $login = null;
}

if (isset($_POST["psw"]))
{
    $psw = $_POST["psw"];
} 
else 
{
    $psw = null;
}

$login2 = mysqli_real_escape_string($conn, $login);
$psw2 = mysqli_real_escape_string($conn, $psw);  



$verify = "INSERT INTO test (email, password) VALUES ('$login2', '$psw2')";
$verify2 = mysqli_query($conn, $verify);


$conn->close();  
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149

1 Answers1

-4

Try this:

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if(isset($_POST['login']) and isset($_POST['email'])){
    $insert = mysqli_query("INSERT INTO test VALUES('".$_POST['login']."', '".$_POST['email']."')");
    if($insert){
        echo "Dados inseridos.";
    }
}


$conn->close();  
Laurel
  • 5,965
  • 14
  • 31
  • 57
  • 1
    *"Você pode fazer da seguinte forma:"* - what does that mean in English? oh and leaving them open to an sql injection; nice. and when does `$_POST["psw"]` come into the picture? and where did you get `$_POST['login']` from? do you have clairvoyant powers? *lol* – Funk Forty Niner Aug 13 '16 at 21:36
  • Andre, you literally have just copied a random simple php login script. – AJ Riley Aug 13 '16 at 21:42
  • Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared statement and parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Aug 13 '16 at 22:38
  • 1
    Why should the OP "use this code"? A **good answer** will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO that may find this question and be reading your answer. – RiggsFolly Aug 13 '16 at 22:38