Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using wpdb to insert not working

Posted on 2015-02-16
6
Medium Priority
?
349 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Wordpress Horizontal Drop-Down Menu In this tutorial I will show you had to add a WordPress horizontal navigation menu to your theme. I have searched and searched for a good tutorial on creating a WordPress nav menu without adding a plug-in or us…
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 set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…

783 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