-2

I want to insert item into my database, but it seems cannot insert into it, here is my code, anyone know the reason ?

after the submission, I check my table 'info', it also nothing changed.

https://i.stack.imgur.com/z9rxI.jpg

this is the front-end of signup page, and the test.php after the submission

Joye
  • 1
  • 1

3 Answers3

1

You do not have ' wrapped around your key accessors on your $_POST variables that you pass directly to the query function (which you shouldn't...see below). However instead of showing you how to correct that, I will instead show you how to secure yourself from SQL injection a bit better.

As it stands, you're super vulnerable to SQL injection by simply allowing the user to post data directly to your database. Instead use a prepared statement to combat this particular case.

$stmt = $mysqli->prepare('INSERT INTO info(username, password, first_name, last_name, location, email, pwv) VALUES(?,?,?,?,?,?,?)');
$stmt->bind_param('sssssss',
    $_POST['username'],
    $_POST['password'],
    $_POST['firstname'],
    $_POST['lastname'],
    $_POST['location'],
    $_POST['email'],
    $_POST['pwv']);
$stmt->execute();
$stmt->store_result();

if( count( $stmt->num_rows ) > 0 ) {
    //this is success
}
Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110
0

Set your $_POST to variables:

$username = $_POST['username'];
$password = $_POST['password'];
$last_name = $_POST['last_name'];
$location = $_POST['location'];
$email = $_POST['email'];
$pwd = $_POST['pwv'];

$sql = "INSERT INTO info(username, password, first_name, last_name, location, email, pwv)
VALUES('$username','$password','$first_name','$last_name','$location','$email','$pwv');
Nere
  • 4,097
  • 5
  • 31
  • 71
0
<?php 
    $servername = "localhost"; 
    $username = "username";
    $password = "password";
    $dbname = "database";

    $conn = mysql_connect($servername,$username,$password)or die(mysql_error());
    mysql_select_db($dbname,$conn);

    $sql = mysql_query("INSERT INTO `info` (`username`,`password`,`first_name`,`last_name`,`location`,`email`,`pwv`)
            VALUES('".$_POST['username']."','".$_POST['password']."','".$_POST['firstname']."','".$_POST['lastname']."','".$_POST['location']."','".$_POST['email']."','".$_POST['pwv']."')")or die(mysql_error());
?>
ImBhavin95
  • 1,494
  • 2
  • 16
  • 29