Got the lower portion here sorted, but now there's an issue when it inserts the record. I have the NULL
value for file
formatted as %s
for string, but it's not inserting NULL
it's inserting [BLOB - 0B]
. The column format in the mySQL table is longblob
. Any ideas?
this is my first time using $wpdb->insert
and it looks like I've missed something.
Here's the loop I'm trying to use. There are currently 2 timestamps in the array.
for ( $i = 0; $i < count($timestamps); $i++ ) {
$working_time = $timestamps[$i];
$working_form = $formnames[$i];
$status_data = array(
'submit_time' => $working_time,
'form_name' => $working_form,
'field_name' => 'lead_status',
'field_value' => 'new',
'field_order' => 10001,
'file' => NULL
);
$status_data_types = array(
'%f',
'%s',
'%s',
'%s',
'%d',
'%s'
);
$result = $wpdb->get_results("SELECT field_value FROM ".$leadtable." WHERE submit_time = ".$working_time);
if(!$result) {
$insert = $wpdb->insert($leadtable, $status_data, $status_data_types);
if( !$insert ) {
echo 'didn\'t work';
}
}
}
I know that $working_time
and $working_form
are both set properly. $working_time
is a long float like 1387175380.9600
and $working_form
is a string.
Nothing is being returned, even by the if( !$insert )
check so I'm guessing it's erring somewhere before that point. I know that if( !$result )
will return true because that data does not exist yet.
Found the issue. The get_results query was failing due to a missed period. My HS English teacher was right... a missed period can be a HUGE problem!
Use $wpdb->insert() . $wpdb->insert('wp_submitted_form', array( 'name' => 'Kumkum', 'email' => '[email protected]', 'phone' => '3456734567', // ... and so on )); Addition from @mastrianni: $wpdb->insert sanitizes your data for you, unlike $wpdb->query which requires you to sanitize your query with $wpdb->prepare .
If you want to get the last inserted row ID from the WordPress database. You can use the $wpdb->insert() it does the insert. $lastid = $wpdb->insert_id; You can find more information about how to do things the WordPress way can be found in the WordPress codex.
Both the $wpdb->insert() and the $wpdb->update() methods perform all the necessary sanitization for writing to the database.
The $wpdb object can be used to read data from any table in the WordPress database, not just those created by WordPress itself.
If you want to get the last error and last query you can use this properties of $wpdb object:
$wpdb->last_error
will show you the last error, if you got one.
$wpdb->last_query
will assist you with showing the last query (where the error occurred)
I hope this will help you out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With