php - Mysqli Prepare Statement Insert Not Inserting -
i'm connected database. when echo of variables, work, not insert database table. have table name correct. here code:
<?php $pid = '1'; $pname = 'name'; $poster_id = '2'; $poster_name = 'name2'; $message = 'this message'; $datetime = date("m d, y"); // insert database $ins = "insert 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 help in advance.
you have few variables don't match.
$poster_id
- $poster_name
- $message
that aligned , in binds:
$user_id, $user, $comment
this should work now:
<?php $pid = '1'; $pname = 'name'; $poster_id = '2'; $poster_name = 'name2'; $message = 'this message'; $datetime = date("m d, y"); // insert database $ins = "insert 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(); ?>
yet, should replace $stmt->execute();
if(!$stmt->execute()){trigger_error("there error....".$con->error, e_user_warning);}
in order grab errors.
also add together error reporting top of file(s) help find errors.
<?php error_reporting(e_all); ini_set('display_errors', 1); // rest of code
which have signaled undefined variable warning.
sidenote: error reporting should done in staging, , never production.
an insight
as pointed out ghost:
$datetime
format m d, y
suspicious too, screw y-m-d h:i:s
's format of column datetime
if indeed way.
therefore may need change
$datetime = date("m d, y");
to
$datetime = date("y-m-d h:i:s");
or
$datetime = date("y-m-d");
depending on column type set to.
php mysqli
No comments:
Post a Comment