Don Higinson Don Higinson - 8 months ago 30
PHP Question

PHP fails to insert data into MYSQL database. Inserts a blank row instead

I have been all over google and have been trying to figure out why this keeps happening. I know similar questions have been asked on this site but their answers are no luck to me. It does connect to the database but doesn't display a record (empty record for each column). The php code has extra commented out sections for testing. Any help is appreciated.

Here's the HTML form

<form action="../controller/register1.php" method="POST" >
First Name&nbsp;<input class="inputs" type="text" name="firstname" placeholder="Enter your firstname" required>
Last Name &nbsp;<input class="inputs" type="text" name="lastname" placeholder="Enter your lastname" required>
User Name <input class="inputs" type="text" name="username" placeholder="Enter your username" required>
Password &nbsp;&nbsp;<input class="inputs" type="password" name="password" required>
Email &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input class="inputs" type="text" name="email" >
<input class="inputs" type="submit" name="submit" value="Register" />


Here's the PHP


$connection = mysqli_connect("localhost","charcoun_donaldh","Rusty1992","charcoun_phplogin","3306") or die("Could not connect!");

if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();

//$firstname = stripslashes($firstname);
$firstname = mysqli_real_escape_string($connection, $_POST['$firstname']);

//$lastname = stripslashes($lastname);
$lastname = mysqli_real_escape_string($connection, $_POST['$lastname']);

//$username = stripslashes($username);
$username = mysqli_real_escape_string($connection, $_POST['$username']);

//$password = stripslashes($password);
$password = mysqli_real_escape_string($connection, $_POST['$password']);

//$email = stripslashes($email);
$email = mysqli_real_escape_string($connection, $_POST['$email']);

$sql = "INSERT INTO users (firstname, lastname, username, password, email)
VALUES ('$firstname','$lastname','$username','$password','$email')"; //Use ".md5($password)." for encrypted
//$result = mysqli_query($connection, $sql);

if(!mysqli_query($connection, $sql)){
die('Error: ' . mysqli_error($connection));
echo "<br/><h3>You have been registered successfully. Click here to return <a href='../view/index.php'>Home.</a></h3>";



Remove the $ when you are retrieving the POST variables. For example, change $_POST['$firstname'] to $_POST['firstname']. Also, if you try using $_POST['firstname'] you should be getting an

Undefined index: $firstname

error. If you aren't getting an error, check to see that you have display errors turned on.