Mysqli Prepare Expression Insert Without Inserting

I am already connected to the database. When I iterate over all the variables, they work, but they just won't be inserted into my database table. I have the correct table name. Here is the code:

<?php

$pid = '1'; 
$pname = 'name'; 
$poster_id = '2';
$poster_name = 'name2'; 
$message = 'This is the message';

$datetime = date("M d, Y");

// insert into database
$ins  = "INSERT INTO messages (profile_id, profile_name, poster_id, poster_name, message, countnum, postdate) VALUES (?, ?, ?, ?, ?, ?, ?)";

$stmt = $con->prepare($ins);
$num = 1;
$stmt->bind_param('isissis', $pid, $pname, $user_id, $user, $comment, $num, $datetime);
$stmt->execute();

?>

      

Thanks for any help in advance.

+3


source to share


1 answer


You have several variables that don't match.

$poster_id

- $poster_name

-$message

which are aligned with your links and in them:

$user_id, $user, $comment


This should now work:

<?php

$pid = '1'; 
$pname = 'name'; 
$poster_id = '2';
$poster_name = 'name2'; 
$message = 'This is the message';

$datetime = date("M d, Y");

// insert into database
$ins  = "INSERT INTO messages (profile_id, profile_name, poster_id, poster_name, message, countnum, postdate) VALUES (?, ?, ?, ?, ?, ?, ?)";

$stmt = $con->prepare($ins);
$num = 1;
$stmt->bind_param('isissis', $pid, $pname, $poster_id, $poster_name, $message, $num, $datetime);
$stmt->execute();

?>

      

However, you must replace $stmt->execute();

withif(!$stmt->execute()){trigger_error("there was an error....".$con->error, E_USER_WARNING);}

to catch errors.

Also add a bug report to the top of your file (s) to help you find bugs.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code 

      

which would signal an undefined variable warning.



Sidenote: Bug reports should only be done at the stage of staging and should never be done.


Insight

As stated by Ghost:

$datetime

format M d, Y

is suspicious too, it might mess up Y-m-d H:i:s

the column format DATETIME

if it really is.

so you might need to change

$datetime = date("M d, Y");

      

to

$datetime = date("Y-m-d H:i:s");

      

or

$datetime = date("Y-m-d");

      

depending on the type of your column.

+6


source







All Articles