0

trying to create one plugin, getting some issue to submit the array data in wordpress database.

$formDATA gets the array value now issue is when

    $sql = "INSERT INTO `".$table_name ."` (`paid_arr`) VALUES '".$formDATA."'";

   $result  = $wpdb->insert($table_name,$formDATA);

getting strange warnings and errors not able to insert data in wp DB.

  Warning: array_keys() expects parameter 1 to be array, string given in /home3/mahesh/public_html/project/lal/wp-includes/wp-db.php on line 1316

 Warning: Invalid argument supplied for foreach() in /home3/mahesh/public_html/project/lal/wp-includes/wp-db.php on line 1318

  Warning: implode() [function.implode]: Invalid arguments passed in /home3/mahesh/public_html/project/lal/wp-includes/wp-db.php on line 1327

here is php code;

$formDATA = array(
    'fname'     => $fname,
    'lname'     => $lname,
    'email'     => $paidemail,
    'Phone'     => $paidphone,
    'gender'    => $gender,
    'hobbies'   => $interest,
    'hear'      => $city,
    'message'   => $message_field,
    'time'      => $curtime
 );

formsubmitpaid($formDATA);       

function formsubmitpaid( $formDATA ) {

global $wpdb;
$formDATA = json_encode($formDATA);
$table_name = $wpdb->prefix . "padi_forms";
$sql = "INSERT INTO `".$table_name ."` (`paid_arr`) VALUES '".$formDATA."'";
$result  = $wpdb->insert($table_name,$formDATA);

if( !$result ){
    echo '<script>alert("dfdf")</script>';
} else {
    echo '<script>alert("No")</script>';
 }
print_r($sql);
print_r($result);

} 

3 Answers 3

1

You should pass data as an array, not a json encoded string. So changing these line would work. Just remove the following line -

$formDATA = json_encode($formDATA);

if you want to see the reference, prepare sql this way

$sql = "INSERT INTO `".$table_name ."` (`paid_arr`) VALUES '".json_encode($formDATA)."'";
3
  • tried following code not getting any error but not inserting data in my sql : $sql = "INSERT INTO ".$table_name ." (paid_arr) VALUES '".json_encode($formDATA)."'"; $result = $wpdb->query( $wpdb->prepare($sql,$formDATA)); $wpdb->show_errors(); Commented Dec 18, 2013 at 6:47
  • No no no, not use the query. I mean, just delete the first line i mentioned, and leave everything as it is. Don't use the sql for inserting, i saw u used it for debugging, so i suggested you encode the data within the sql, not override the $formDATA variable
    – Shazzad
    Commented Dec 18, 2013 at 6:49
  • 1
    now getting following error: Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in Commented Dec 18, 2013 at 7:01
1

you can php serialize function to convert array to string before inserting to database. during retrieval, call unserialize function to get the array back from string.

to insert:

$result  = $wpdb->insert($table_name,serialize($formDATA));

to retrieve:

   $result = unserialize($wpdb->get_col("SELECT paid_arr from $table_name"));

you can use json_encode and json_decode as well. but json_* functions are not available in older version of php.

1

You need not have to json_encode the array as long as you have the table structure same as the form data key values. Reference: wpdb

$formDATA = array(
    'fname'     => $fname,
    'lname'     => $lname,
    'email'     => $paidemail,
    'Phone'     => $paidphone,
    'gender'    => $gender,
    'hobbies'   => $interest,
    'hear'      => $city,
    'message'   => $message_field,
    'time'      => $curtime
);

formsubmitpaid($formDATA);      

function formsubmitpaid($formDATA) {
    global $wpdb;
    $table_name = $wpdb->prefix . "padi_forms";    

    $result = $wpdb->insert( 
        $table_name, 
        $formDATA, 
        array( 
            '%s', 
            '%s', 
            '%s', 
            '%s', 
            '%s', 
            '%s', 
            '%s', 
            '%s', 
            '%s' 
        ) 
    );  

    if( $result === false )
        echo 'Error';
    else 
        echo 'success';
} 
7
  • is it necessary to write: array('%s', '%s', '%s', '%s','%s', '%s','%s', '%s', '%s') ? Commented Dec 18, 2013 at 6:57
  • Yes, you should.. as that type of data you are inserting and the table's column data type should match. Insert Rows Commented Dec 18, 2013 at 6:59
  • thanks, what other ways we can insert the data in database? i check we can insert using prepare as well which is more easy because there will be large data later. Commented Dec 18, 2013 at 7:05
  • Read here regarding all the methods provided in $wpdb Run any database query --- & --- Accept if it did solve. Commented Dec 18, 2013 at 7:27
  • i am getting following error: Warning: mysql_real_escape_string() expects parameter 1 to be string, array Commented Dec 18, 2013 at 7:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.