Reputation: 2615
I'm trying to insert a new table row into a custom wpdb
table. The data is coming from a form posted to a script.
I'm declaring global $wpdb
at the start of the script, and inserting into the db, but for some reason it doesn't like the query function, which WordPress recommends to use in the codex.
// declare global wpdb
global $wpdb;
// set variables for posting to db
$name = $_POST['name'];
$email = $_POST['email'];
// post to the db
$wpdb->query( $wpdb->prepare(
"
INSERT INTO $wpdb->entries
( name, email )
VALUES ( %s, %s )
",
$name,
$email
) );
Any ideas as to why it's bugging out?
Upvotes: 0
Views: 9241
Reputation: 1
I have solved my error using
include_once($_SERVER['DOCUMENT_ROOT'].'/wordpress/wp-config.php' );
This code actually I have submitted form data in my custom database table using Ajax but I have got a fatal error.. Now the fatal error is solved.
Upvotes: 0
Reputation: 475
<?php
/**
* Created by PhpStorm.
* User: manisha
* Date: 05-11-2015
* Time: 15:30
*/
include_once($_SERVER['DOCUMENT_ROOT'].'/wordpress/wp-config.php' );
$email = $_REQUEST['email'];
global $wpdb;
$result=$wpdb->query(
$wpdb->prepare(
"
DELETE FROM wp_addemail
WHERE email = %s
",
$email
)
);
this things is worked for me
Upvotes: 0
Reputation:
It is a wordpress feature, so the global $wpdb
can be called only if it has been defined previously.
This means, inside the /wp-content/... folder if you use a script with wp specific features it won't work until it is compiled with wordpress core.
So, for example, a theme template or plugin files can work just by calling $wpdb
, but your standalone script won't, even if it is inside the plugin or theme directory.
Simple solution is to include /wp-config.php file before running that script. Just write:
include_once($_SERVER['DOCUMENT_ROOT'].'/wordpress/wp-config.php' );
before calling $wpdb
. It works for me.
Upvotes: 10