0

I am trying to run the following query to access my WP cf7dbplugin_submits table but it keeps coming up empty. I have added my table into wp-db.php model as such:

Added the contact form table- cf7dbplugin_submits to the following at about line 268:
    var $tables = array('cf7dbplugin_submits','posts', . . .)

At around line 304 add the following:
    /**
     * WordPress cf7dbplugin_submits table
     */
    public $cf7dbplugin_submits;

So the database function should find it (it has when used elsewhere in my code). But the following is consistently coming up empty for any $postid:

function action_getpost($postid) {
  global $wpdb, $out;
  $out['post'] = array();
  $out['uploads'] = array();

    // wp_posts has one row for each submitted form.
    // wp_nf_objectmeta includes a set of rows for each defined form.

    $query =
    "SELECT submit_time, form_name, field_value" .
    "FROM $wpdb->cf7dbplugin_submits " .
    "WHERE field_order = %d AND submit_time = %d ";

    $queryp = $wpdb->prepare($query, array(9999, $postid));
    if (empty($queryp)) {
      $out['errmsg'] = "Internal error: \"$query\" \"$postid\"";
      return;
    }
    $wpdb->show_errors( true );
    $row = $wpdb->get_row($queryp, ARRAY_A);
    if (empty($row)) {
      $out['errmsg'] = "No rows matching: \"$postid\"";
      return;
    }

If I just run the following query in phpMyAdmin, where $postid = 1445125066.4375:

SELECT submit_time, form_name, field_value
FROM wp_cf7dbplugin_submits
WHERE field_order = 9999 AND submit_time = 1445125066.4375 

It produces the needed row

submit_time         form_name       field_value
1445125066.4375     Demographics    usernamehere

So why is the function returning my error of no rows matching 1445125066.4375? Any ideas?

4
  • If $postid is supposed to be a WordPress post ID it isn't ever going to be a decimal. – s_ha_dum Nov 2 '15 at 19:08
  • Thanks, there is no key in the wp_cf7dbplugin_submits table. The closest thing I have to identify posts there is the unix timestamp with milliseconds in 'submit_time'. This with the field order of 9999 will get me the distinct form data I need. The timestamp is what I am using for $postid and it should match 'submit_time'. So, will it not be possible to use this? – Craig Tucker Nov 2 '15 at 19:31
  • Weird variable name, but where is $postid set? – s_ha_dum Nov 2 '15 at 19:41
  • Yes, what I am doing is trying to rework existing open source code that was developed for Ninja Forms integration to work with Contactform 7 instead. In the Ninja Forms version $postid was set to wp_post.ID. Where is it set?: First the user sees all form submissions for a form_name (e.g. Demographics). This part works fine. On clicking submission, $postid in this case is set to submit_time for that submission and the function action_getpost is called with the $postid. My CF7 query yields the same data as the Ninja query did. – Craig Tucker Nov 2 '15 at 21:35
0

You have several things going on there and I may not be able to sort them all out.

  1. Unless $wpdb->cf7dbplugin_submits has been added to the $wpdb object your query won't work. You will need something like {$wpdb->prefix}cf7dbplugin_submits instead but I can't really guess at the right value.
  2. You don't need to swap in 9999. That is a hard-code value. Swapping it in with prepare is a waste of resources.
  3. Prepare accepts 3 different placeholders, one for a string, one for a digit, and one for float. By using %d you are truncating your decimal. You need %f.

Perhaps something like this will work better:

$query =
"SELECT submit_time, form_name, field_value" .
"FROM {$wpdb->prefix}cf7dbplugin_submits " .
"WHERE field_order = 9999 AND submit_time = %f ";

$queryp = $wpdb->prepare($query, $postid);

Item #2 won't cause a failure of the query, but item #1 would, so that is where I am betting this goes wrong. However, item #3 would just as likely break the query as you would not get a match on a decimal value if the decimal input was truncated to an integer.

2
  • Thanks. I made adjustments with 9999. cf7dbplugin_submits was added to $wpdb so that is not an issue. I tried just writing 1445125066.4375 into the query. It passed to get_row with no error. The next part of the code works. So for some reason prepare is not passing $postid to %d in the query. But $postid is being passed to the error code. So, I know I am accessing the table. And I know that $postid is set before reaching the function. prepare is not filling %d. I am not sure why. – Craig Tucker Nov 2 '15 at 23:15
  • @CraigTucker : Ha! Got it. Give me two minutes and check the edit. – s_ha_dum Nov 2 '15 at 23:22
0

As it turns out it was an issue of the decimal point which was pointed by s_ha_dum. But for different reasons. The place holder %d is integer. I need %f for floating point. Now it works. And thanks to s_ha_dum I have more parsimonious code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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