14

I am starting out plugin development and have followed the tutorials on the WordPress Codex sites. I am now stuck - I have a database called "wp_imlisteningto", where the wp_ was inserted using:

$table_name = $wpdb->prefix . "imlisteningto";

When the plugin is activated.

The database itself has three columns, set up when the plugin is activated:

$sql = "CREATE TABLE $table_name (
id mediumint(9) AUTO_INCREMENT,
album VARCHAR(50),
artist VARCHAR(50),
PRIMARY  KEY (id)
);";

I am trying to insert data (by creating a new row) into this database from a php form.

Within the WordPress admin, I create a new page which has the very simple form:

<form action="/wp-content/plugins/listeningto/formhtml.php" method="post">
Album: <input type="text" name="album" />
Artist: <input type="text" name="artist" />
<input type="submit">
</form>

Which as you can see calls formhtml.php, which is:

<?php
global $wpdb;

$wpdb->insert( $table_name, array( 'album' => $_POST['album'], 'artist' => $_POST['artist'] ), array( '$s', '$s' ) );
?>

When I submit the form, I get an Error 500.0 when running the plugin in Worpdress on IIS7.0, and a "Page Not Found" when running on another web server which runs apache.

If I change formhtml.php to:

<?php

echo $_POST['album'];
echo $_POST['artist'];

?>

Works fine - I get the album and artist that I put in the form. Obviously something I'm doing wrong when inserting the data (in a new row) into the database.

Any thoughts as to what that might be?

UPDATE

Ok, so if I update formhtml.php with this:

<?php
require_once('../../../wp-config.php');
$table_name = $wpdb->prefix . "imlisteningto";
$wpdb->insert( $table_name, array( 'album' => $_POST['album'], 'artist' => $_POST['artist'] ), array( '$s', '$s' ) );
?>

I no longer get an error message, but data still doesn't get put into the database.

UPDATE 2

This worked for me:

<?php
require_once('../../../wp-config.php');
global $wpdb;
$table_name = $wpdb->prefix . "imlisteningto";
$wpdb->insert( $table_name, array( 'album' => $_POST['album'], 'artist' => $_POST['artist'] ) );
?>

as did this:

<?php
require_once('../../../wp-load.php');
global $wpdb;
$table_name = $wpdb->prefix . "imlisteningto";
$wpdb->insert( $table_name, array( 'album' => $_POST['album'], 'artist' => $_POST['artist'] ) );
?>

So, for some reason $wpdb was not working unless I required either wp-config or wp-load.php. If include wp-load.php, $wpdb gets values and all is well.

1
  • 3
    require_once(ABSPATH . 'wp-settings.php'); should work as well. ABSPATH is defined @wp-load.php Commented Jan 4, 2013 at 16:20

6 Answers 6

5

including

require_once('../../../wp-config.php');

worked for me

Sign up to request clarification or add additional context in comments.

Comments

3

Try this..

<?php
global $wpdb;

$wpdb->insert( $table_name, array( 'album' => "$_POST['album']", 'artist' => "$_POST['artist']" ) );
?>

Ex :

<?php
global $wpdb;

$wpdb->insert($table_name , array('chart_name' => "Line Chart" ,'chart_type' => "trends",'status' => 0));

?>

2 Comments

Hi Ajay thanks for the reply - no, that didn't work either. However, I have updated my post with what worked for me.
@MattyRasker Is it works ? for more info snipt.net/ajaypatel_aj/wordpress-crud-queries
0

You've probably figured this out by now, but no one addressed it here. Your sample code has '$s' in the 3rd parameter (2nd array), but that should be '%s' because it's for value-formatting. The WP Codex says [http://codex.wordpress.org/Class_Reference/wpdb] that this format parameter for $wpdb->insert() is optional.

Comments

0

Create your array as per DB table. Then use WP insert .

 global $wpdb;
 $order = [
            'product_id' =>'1',
            'discount' => '1',
            'total' => '1',
            'status' => '1',
            'payment_method' => '1',
            'payment_reference' => '1',
            'sold_by' => '1'
        ];

$wpdb->insert('DB_TABLE_NAME', $order );

Comments

0
  $global $wpdb;      
  $table_name1 = $wpdb->prefix.'php_crud';  
    
  $data_insert= array(                      
                    'uname1' => $uname1,
                    'uemail1' => $uemail1,
                    'upass1' => $upass1,
                    'language1' => $language1,
                    'gender1' => $gender1,
                    'ustate1' => $ustate1,
                    'file1' => $finalimg
                    );
 $form_insert1=$wpdb->insert($table_name1,$data_insert);
 if($form_insert1)
 {
    echo "<div class='container' style{ color:red;} >Form 
    Submitted Successfully<div>";   
}
else
{
    echo "ERROR";
}

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
-1

I think there are 2 mistakes in you sql string.

Think it should be the $table_name variable should be concatenated

$sql = "CREATE TABLE" . $table_name . "(
id mediumint(9) AUTO_INCREMENT,
album VARCHAR(50),
artist VARCHAR(50),
PRIMARY  KEY (id)
)";

and remove ; on the last line.

2 Comments

this is wrong, you will end up with: CREATE TABLEwp_imlisteningto
Noted, but the creation of the table wasn't the issue, that worked fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.