1

hi i have decided to store few values in a separate table, So i created it and now i used a form to store values but when i process that form i'm getting error Trying to get property of non-object and Call to a member function insert() on a non-object Even though i declared global $wpdb;

Here is the form

<form action="formaction.php" method="post">
    <input type="text" name="name1" value="">
    <input type="text" name="name2" value="">
    <input type="submit" name="submit" value="add">
</form>

formaction.php

if(isset($_POST['name1']))
{
    $db_data1=$_POST['name1'];
}
if(isset($_POST['name2']))
{
    $db_data2=$_POST['name2'];
}
function res()
{
 global $wpdb,$db_data1,$db_data2;
        $my_table_name=$wpdb->prefix."my_table";
        $name1=$db_data1;
        $name2=$db_data2;
        $rows_affected = $wpdb->insert( $my_table_name, array( 'first' => $name1, 'last' => $name2 ) );
}

res();

according to error $wpdb is not an object but i have used global, what is the bug in this code? am i doing correctly ?

1

You're posting directly to formaction.php, as such WordPress is never loaded and so you do not have access to any of its API, including the database API stored in global $wpdb (which is causing the error). I'd recommend keeping everything in WordPress... and a useful method for form handling is using admin-post.php (if you're familiar with WordPress' ajax handling - this is a similar idea).

The idea is that you post to the url

 .../wp-admin/admin-post.php

which you can get via admin_url('admin-post.php');. And you post - along with any data and nonces - a unique value for the action variable. (Let's say wpse111797_form_submitted). When the form is submitted to admin-post.php, WordPress will trigger the hook:

  • admin_post_wpse111797_form_submitted - if you are logged in
  • admin_post_nopriv_wpse111797_form_submitted - if you are logged out

So your form might look like:

<form action="<?php echo admin_url('admin-post.php'); ?>" method="post">
    <input type="hidden" name="action" value="wpse111797_form_submitted">

    <input type="text" name="name1" value="">
    <input type="text" name="name2" value="">

    <input type="submit" name="submit" value="add">

</form>

Please don't forget to uses nonces to verify intent.

You can then hook onto either of the above hooks (or both) depending on whether you want to handle form submissions from logged in users, guests, or both.

function wpse111797_form_handler(){

   global $wpdb;
   $name1 = isset( $_POST['name1'] ) ? $_POST['name1'] : null;
   $name2 = isset( $_POST['name2'] ) ? $_POST['name2'] : null;

   //TODO Check nonces & permissions first!

   $my_table_name=$wpdb->prefix."my_table";

   $rows_affected = $wpdb->insert( $my_table_name, array( 'first' => $name1, 'last' => $name2 ) );

   //Redirect user to a 'success' page, or from back whence they came!
}

//Attach callback to hook (in this case, the hook for logged-in users)
add_action( 'admin_post_wpse111797_form_submitted', 'wpse111797_form_handler' );
  • Thanks for your understandable answer to me. So what ever i do with php i should hook it otherwise it will come out of wordpress right? – sun Aug 27 '13 at 10:55
  • Sorry i have used the form directly, that too causing me a problem.i wanted to add a Add New button in top of table and if user click on Add New this form should appear.What is the best idea to make this inside WordPress itself? – sun Aug 27 '13 at 11:00
  • 1
    I'm afraid I'm not able to go into specifics of your implementation. But the above is a general guide on how to implement form handling on custom admin pages. R.e. the problems you're experiencing, there could be syntax errors - but in principle the code is ok. I suggest turning WP_DEBUG on and finding out where its failing and go on from there. – Stephen Harris Aug 27 '13 at 11:23
  • why i can't able to see admin_post_wpse111797_form_submitted in codex document.is this familiar way to store datas? – sun Aug 27 '13 at 12:36
  • There's nothing on the Codex about this, you'll have to go to source to see what's happening. – Stephen Harris Aug 27 '13 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.