Charan Balse Charan Balse - 3 months ago 15
PHP Question

PHP MySQLi INSERT Query not working

Continuing from my previous 2 posts 'PHP and MYSQL database connection and table creation only once' and 'Undefined variable in php registration from', i dont have any errors until I use below code to INSERT the POSTED DATA into my database. As I'm new to MYSQLI I'm not able to understand my errors and debug them. PLZ HELP..

//code for input field validation

if (!empty($error)) //send to Database if there's no error '
{
$query = $mysqli->prepare("INSERT INTO users ( Name, Username, Password, Email) VALUES (?, ?, ?, ?, ?)");
$query ->bind_param('sssdi',$fullname, $username, $password2, $email);
$stmt->execute();
$newId = $stmt->insert_id;
$stmt->close();
//$mysqli->query($query);
$result = @mysqli_query($query);
if (!$result) {
$error['ack'] = 'Failed to Register Your Account...!';
} else {
$error['ack2'] = 'Account Registered Successfully...!';
}
//$mysqli->close();//Close the DB Connection


My REGISTRATION.PHP form is available in the 'Undefined variable in php registration from' link.

This is my complete code
Memberid)); ";
mysql_select_db('USERS');
$retval = mysql_query($sql, $connect);
if (!$retval) {
die('COULD NOT CREATE TABLE\n: ' . mysql_error());
}
;
mysql_close($connect);
//end of DB_connection

$fullname = "";
$username = "";
$password = "";
$password2 = "";
$email = "";

if (isset($_POST['submit'])) {
session_start();
$error = array(); //Declare an array to store error messages

//validation for fullname
if (!empty($_POST['fullname'])) {
$fullname = mysql_real_escape_string(trim($_POST['fullname']));
} else {
$error['fullname'] = 'Enter Fullname...';
}

//validation for username
if (!empty($_POST['username'])){
$username = mysql_real_escape_string(trim($_POST['username']));
} else {
$error['username'] = 'Enter Username...';
}

//Validation for password and confirm password
if (!empty($_POST['password'])) {
if ($_POST['password'] != $_POST['password2']) {
$error['password2'] = 'Passwords do not match...';
} else {
$password2 = mysql_real_escape_string($_POST['password']);
}
} else {
$error['password'] = 'Enter Password...';
}

//validation for e-mail
if (!empty($_POST['email'])) {
$email = mysql_real_escape_string($_POST['email']);
} else {
$error['email'] = 'Enter your Email...';
}

if (!empty($error)) //send to Database if there's no error '
{
$query = $mysqli->prepare("INSERT INTO users ( Name, Username, Password, Email) VALUES (?, ?, ?, ?, ?)");
$query ->bind_param('sssdi',$fullname, $username, $password2, $email);
$stmt->execute();
$newId = $stmt->insert_id;
$stmt->close();
//$mysqli->query($query);
$result = @mysqli_query($query);
if (!$result) {
$error['ack'] = 'Failed to Register Your Account...!';
} else {
$error['ack2'] = 'Account Registered Successfully...!';
}


}//end of main if
?>
<html>
<form action="register.php" method="post" id="user_registration">
<p id="head">Create Account</p>

<input type="text" id="fullname" name="fullname"/>
**<span class="error" id="fullname"><?php echo $error_name; ?></span>**

<input type="text" id="username" name="username"/>
<span id="availability_status"></span>
**<span class="error" id="username"><?php echo $error_username; ?></span>**

<input type="password" id="password" name="password"/>
**<span class="error" id="password"><?php echo $error_password; ?></span>**

<input type="password" id="password2" name="password2"/>
**<span class="error" id="divCheckPasswordMatch"><?php echo $error_password2;?></span>**

<input type="email" id="email" name="email"/>
**<span class="error" id="email"><?php echo $error_email; ?></span>**

<p class="submit">
<button type="submit"id="submit" name="submit" value="Register”>Register</button>
</p>




Answer

change your code something like this

$query = $mysqli->prepare("INSERT INTO users (Name, Username, Password, Email) VALUES (?, ?, ?, ?)");
 $query->bind_param("ssss", $fullname, $username, $password2, $email);
 $query->execute();

also double check your field name's are with first capital letter or not, if yes then change the field name as per your table.

Comments