Vinay Vinay - 1 year ago 77
MySQL Question

MySQL Insert query returning false

This is probably the most asked question here. I have made a simple user registration form. The values are not inserted into the database. I used echo and the query is returning false. The form was working well before but then i separated the name into first and last name and dropped the previous table and made a new one. Here is the registration page code:

<!DOCTYPE html>

global $con;

$user_first_name = $_POST['user_first_name'];
$user_last_name = $_POST['user_last_name'];
$email = $_POST['email'];
$password = $_POST['password'];
echo "<script type='text/javascript'>alert(\"$user_first_name\");</script>"; //This echo is returning username successfully!

$query = "insert into user(user_first_name, user_last_name, user_email, password) values('$user_first_name', '$user_last_name', '$email', '$password')";

if(mysqli_query($con, $query)){
$_SESSION['user'] = 'user';
$_SESSION['user_first_name'] = $user_first_name;
echo "header('Location:index.php')";

} else {
echo "<script type='text/javascript'>alert(\"Values not inserted\");</script>"; //This is running which means query is not successfull.
} else {
echo "<script type='text/javascript'>alert(\"Page didn't receive post values\");</script>";

<link rel="stylesheet" type="text/css" href="styles/register_style.css">

<title>New User Registration</title>
<div class="wrapper">
<div class="form_div">
<div class="form">
<form id="register_form" method="post" action="" autocomplete="autocomplete">
<td id="label">First Name: </td>
<td id="input"><input type="text" name="user_first_name" required="required" id="input_box"></td>
<td id="label">Last Name: </td>
<td id="input"><input type="text" name="user_last_name" required="required" id="input_box"></td>
<td id="label">Email: </td>
<td id="input"><input type="text" name="email" required="required" id="input_box"></td>
<td id="label">Password: </td>
<td id="input"><input type="password" name="password" id="input_box"></td>
<td id="label">Confirm Password: </td>
<td id="input"><input type="password" name="confirm_password" id="input_box"></td>
<tr id="button_row">
<td colspan="2"><input type="reset" value="Reset" id="button">
<input type="submit" value="Register" id="button" name="register_ok"></td>

And this is the table :Table Structure
The table is empty and the first alert is returning user first name and the second alert runs when the query returns false. It is returning false.
I think it may be a typo but cannot narrow it down. Any help would be welcome.

Answer Source

Change your column types from int to varchar. I'm talking about string columns (names and email). Mysql has an option to check for the data type you are trying to insert and fail if they don`t match.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download