-1

I am trying to insert a row into a database, I am properly connected to the database with select, insert, delete, and update permissions. I know because this was a previous problem that I fixed. this is my code:

$query = "INSERT INTO 'user_account'
             ('user_account_id', 'user_type_id', 'email', 'password', 'date_of_birth', 'gender', 
                'is_active', 'contact_number', 'sms_notification_active',
                'email_notification_active', 'user_image', 'registration_date')
          VALUES
             (:user_account_id, :user_type_id, :email, :password, :date_of_birth,
                :gender, :is_active, :contact_number, :sms_notification_active,
                :email_notification_active, :user_image, :registration_date)";

$statement = $db->prepare($query);
$statement->bindValue(':user_account_id', '1');
$statement->bindValue(':user_account_id', '1');
$statement->bindValue(':user_type_id', '1');
$statement->bindValue(':email', 'rd@msn.com');
$statement->bindValue(':password', '2Twinboys');
$statement->bindValue(':date_of_birth', '03/24/1992');
$statement->bindValue(':gender', 'M');
$statement->bindValue(':is_active', 'Y');
$statement->bindValue(':contact_number', '8164529832');
$statement->bindValue(':sms_notification_active', 'N');
$statement->bindValue(':email_notification_active', 'Y');
$statement->bindValue(':user_image', file_get_contents('id_image.jpg'));
$statement->bindValue(':registration_date', '10/25/2020');
if($statement->execute() === true) {
    $msg = "New account created successfully";
} else {
    $msg = "Error: " . $query . "<br>";
}
$statement->closeCursor();

then in my html I have:

<p><?php echo $msg; ?></p>

and the output is:

Error: INSERT INTO 'user_account' ('user_account_id', 'user_type_id', 'email', 'password', 'date_of_birth', 'gender', 'is_active', 'contact_number', 'sms_notification_active', 'email_notification_active', 'user_image', 'registration_date') VALUES (:user_account_id, :user_type_id, :email, :password, :date_of_birth, :gender, :is_active, :contact_number, :sms_notification_active, :email_notification_active, :user_image, :registration_date)

so can someone tell me why my insert never works?

  • 2
    Table names and column names should not be surrounded with single quotes. They should be left unquoted, or if necessary, use backticks – aynber May 11 '22 at 15:43
  • is that the whole error message? – GrafiCode May 11 '22 at 15:47
  • 2
    @GrafiCode It is, because OP wrote code which ignores any other, more useful, error messages that PDO might have generated :-). But actually, as anyber said, there are some obvious problems just from the dumped query. – ADyson May 11 '22 at 15:48
  • 1
    https://phpdelusions.net/pdo#errors - switch on errors so you can have mysql tell you why it failed. But yeah, table names belong in backticks not quote marks. Go back to some examples of mysql query syntax and study in a bit more detail. See also the duplicate in the blue box. – ADyson May 11 '22 at 15:50
  • 1
    You also should use date columns, not varchar/text. `10/25/2020` is not a mysql date format – user3783243 May 11 '22 at 15:54

0 Answers0