Solved

Using wpdb to insert not working

Posted on 2015-02-16
6
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

How to create your own WordPress Theme Before I begin with the theme design tutorial, I would like to give you the basics of what is required to make a WordPress Theme. The most basic WordPress theme only requires 2 files: a style.css file and an…
WordPress can be pretty daunting, especially for a beginner, so I thought it might be a good idea to write an article to show how easy it is to get started in WordPress and to design a custom theme.  The first step is to check with your hosting comp…
The purpose of this video is to demonstrate how to automatically show related posts at the bottom of a blog post in WordPress. This will be demonstrated using a Windows 8 PC. Plugin “Yet Another Related Posts Plugin” will be used. Go to your…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…

635 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