Solved

Using wpdb to insert not working

Posted on 2015-02-16
6
145 Views
Last Modified: 2015-02-24
See attached.

This is a simple test program to try to insert a row into a table using wpdb class. Does not work. Nothing inserted.

Image shows table structure in database.

What's wrong?

Thanks
test-dbinsert.php
captcha-str.jpg
0
Comment
Question by:Richard Korts
  • 3
  • 3
6 Comments
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40613050
What happens if you turn on WP_DEBUG?  What errors are reported.
0
 

Author Comment

by:Richard Korts
ID: 40613210
Note that the program is NOT a WordPress program; it's php OUTSIDE of WordPress.

I put in the debug; probably didn't do it right. Here is the WHOLE program

<?php
$code=rand(1000,9999);
$skey = intval($_GET['skey']);
require( 'wp-load.php' );
define('WP-DEBUG', true);
global $wpdb;
$wpdb->insert(
      'captcha',
      array(
            'skey' => $skey,
            'code' => $code
      ),
      array(
            '%d',
            '%d'
      )
);
?>

Ran it again; does nothing. No debug statements, nothing.

Thanks
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40613231
I put in the debug; probably didn't do it right.

Yeah, nope.  WP_DEBUG goes in wp-config.php so it needs to run inside.

Why use $wpdb if you are running completely outside the framework? For database authentication?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Richard Korts
ID: 40613242
Jason,

Because I tried the normal connect to the database with normal SQL & it failed too.

I echoed the Insert statement, (normal SQL), I copied it & used it in phpMyadmin & it inserted fine.

FYI, this is just my Plan B way of working since $_SESSION variables do not work, even outside WordPress. I have a whole nother question on that in the php category, No Expert yet has solved that either.
0
 

Author Comment

by:Richard Korts
ID: 40613256
Jason,

I envision a solution to this where I put in a NEW database totally devoted just to this ONE process, keeps all WordPress influences out of it & do it that way.

What a pain but I have spent the ENTIRE day on this ONE problem to no avail.
0
 
LVL 70

Accepted Solution

by:
Jason C. Levine earned 500 total points
ID: 40613476
Because I tried the normal connect to the database with normal SQL & it failed too...Keeps all WordPress influences out of it & do it that way

There's absolutely no reason why a normal PHP call to mysqli() with correct authentication details wouldn't work.  Using $wpdb should also work assuming your load works properly.

For error checking, do this:

require( 'wp-load.php' );
global $wpdb;
$wpdb->show_errors();
$wpdb->insert(
      'captcha',
      array(
            'skey' => $skey,
            'code' => $code
      ),
      array(
            '%d',
            '%d'
      )
);

Open in new window


and hopefully we get something out of it.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Blog posts not showing up on Blog page 7 67
"Go Fund Me" type plugin 5 24
Remove Black Diamond with Question Mark from Wordpress Custom Meta 6 96
Two wordpress questions 3 61
I want to start by talking about the use of plug-ins for WordPress. I started a web-site for a company I was working for a few years ago; I had extremely basic knowledge of HTML. I am a Graphic Designer by trade so I invited the opportunity as a cha…
So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
The purpose of this video is to demonstrate how to insert an Iframe into WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Open Page or Post…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question