Solved

Using wpdb to insert not working

Posted on 2015-02-16
6
105 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
Comment Utility
What happens if you turn on WP_DEBUG?  What errors are reported.
0
 

Author Comment

by:Richard Korts
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Richard Korts
Comment Utility
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
Comment Utility
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
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Do you think that WordPress is just for blogs?  Think again!  WordPress is really a fantastic all around platform that you can use to develop websites on.  Integrated into its basic functionality is the ability to create pages using your choice of a…
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
The purpose of this video is to demonstrate how to create a Printer Friendly PDF on a WordPress Page. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome Screenshot” Google Chrome Extension, and SmallPDF.com Log…
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…

772 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now