I work with XYZ PHP Code WordPress plugin. Why this code dont’t work?
Table structure: id (int, AI), name (varchar), mail (varchar), message(varchar), date (datetime)
if(isset($_POST['add']) && $_POST['name'] && $_POST['mail'] && $_POST['message'])
{
global $wpdb;
$name= $_POST['name'];
$mail= $_POST['mail'];
$message= $_POST['message'];
$date= current_time('mysql');
$table_name = $wpdb->prefix . 'mytable';
$wpdb->insert( $table_name, array(
'name' => $name,
'mail' => $mail,
'message' => $message,
'date'=> $date));
}
2
Answers
Did you check your variable if it has null value?
or Is your structure allowing nullable values?
You are only checking if
$_POST['add']
has been set, and are allowing for$_POST['name']
,$_POST['mail']
and$_POST['message']
to be nullable.Ensure your table schema allows for these fields to be null. If it doesn’t allow null values, wrap these variables in
isset()
.isset($variable)
determines if a variable is declared and is different than null.To understand more why this code isn’t executing correctly, you should take a look at the error logs.