skip to Main Content

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


  1. Did you check your variable if it has null value?

    or Is your structure allowing nullable values?

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search