Title: Adding data to custom database
Last modified: August 20, 2016

---

# Adding data to custom database

 *  Resolved [j3ddesign](https://wordpress.org/support/users/j3ddesign/)
 * (@j3ddesign)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/adding-data-to-custom-database/)
 * Hi,
    I’m trying to put together a form to allow admins to add new courses to 
   a list for users to look at. It’s a simple HTML form, pushing data to a php file
   which should in turn add rows to my database.
 * The html form does have to save each result to an array, as there is an option
   to add more “forms”.(perhaps this is what is screwing it up).
 * The problem comes when i actually go to submit my form and i get:
    `Fatal error:
   Call to a member function insert() on a non-object in C:\Users\Jordan\Documents\
   Work\ASTO\Website\www.pcwportsmouth.dev\wp-content\themes\asto\insertcourse.php
   on line 9`
 * setup is as follows:
    Database name “allcourses” rows: courseid(autoincremental),
   coursename, coursevalue, coursecategory
 * addnewcourse.php
 *     ```
       <?php get_header(); ?>
       <div id="addcourse">
       <form action="http://www.pcwportsmouth.dev/wp-content/themes/asto/insertcourse.php" method="post" id="addcourse">
   
       <label id="coursename">Course Name:<input type="text" name="coursename[]" size="30" /></label>
       <label id="coursevalue">Points Value:<input type="text" name="coursevalue[]" size="10"  /></label>
       <label id="coursecategory">Course Category:<select name="coursecategory[]" size="1">
               <option selected>Product Knowledge</option>
               <option>Demonstrate Effectively</option>
               <option>Perfect Your Pitch</option>
               <option>Business Integration and Technical Training</option>
           </select></label>
   
       <p>&nbsp;</p>
       <p>&nbsp;</p>
       </div>
       <input type="submit" name="Submit" id="addcoursesubmit" value="Submit" />
       </form>
       <a href="" onClick="return false;" id="addform">Add Another Course.</a>
   
       <?php get_footer(); ?>
       ```
   
 * insertcourse.php
 *     ```
       <?php
   
       global $wpdb;
   
       $coursename=$_POST['coursename'];
       $coursevalue=$_POST['coursevalue'];
       $coursecategory=$_POST['coursecategory'];
   
       $wpdb->insert(
       		'allcourses',
       		array(
       				'coursename' => '$coursename',
       				'coursevalue' => '$coursevalue',
       				'coursecategory' => '$coursecategory'
       			),
       			array(
       			 '$s',
       			 '$d',
       			 '$s'
       			 )
       );
   
       echo "</br>";
       echo "<a href\"add-new-asto-course\">Add More Courses</a>"
   
       ?>
       ```
   

Viewing 14 replies - 1 through 14 (of 14 total)

 *  [Andrew Bartel](https://wordpress.org/support/users/andrew-bartel/)
 * (@andrew-bartel)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/adding-data-to-custom-database/#post-3095639)
 * At a quick glance, I’d say it’s a non object because insertcourse.php isn’t being
   loaded as a template file, meaning, the rest of WordPress isn’t loaded, so php
   has no idea what $wpdb is. Try posting to the same page, i.e. <form action=””
   > and then running a logic check at the top of the template:
 *     ```
       If($_POST['Submit']) {
       // run validation if you're not doing it in js
   
       global $wpdb;
   
       $coursename=$_POST['coursename'];
       $coursevalue=$_POST['coursevalue'];
       $coursecategory=$_POST['coursecategory'];
   
       $wpdb->insert(
       		'allcourses',
       		array(
       				'coursename' => '$coursename',
       				'coursevalue' => '$coursevalue',
       				'coursecategory' => '$coursecategory'
       			),
       			array(
       			 '$s',
       			 '$d',
       			 '$s'
       			 )
       );
   
       }
       ```
   
 *  Thread Starter [j3ddesign](https://wordpress.org/support/users/j3ddesign/)
 * (@j3ddesign)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/adding-data-to-custom-database/#post-3095655)
 * Thanks for the reply.
    Adapted code to what you suggested, however now it just
   refreshes the page and does not submit.
 *  [Andrew Bartel](https://wordpress.org/support/users/andrew-bartel/)
 * (@andrew-bartel)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/adding-data-to-custom-database/#post-3095661)
 * Did you try a:
 *     ```
       print_r($_POST);
       die();
       ```
   
 * to see what’s actually coming through?
 * Load your form, put the print_r and die in at the top, save, and then submit 
   the form. If everything is coming through right, add a logic check at the top
   for submit and run your validation and database interaction in there.
 * Just to confirm, your form is calling is the following correct?
    `<form action
   ="" method="post" id="addcourse">`
 *  Thread Starter [j3ddesign](https://wordpress.org/support/users/j3ddesign/)
 * (@j3ddesign)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/adding-data-to-custom-database/#post-3095678)
 * the form call is correct, did double check! 🙂
 * after doing a print_r i get:
    `Array ( [0] => Magic Trackpad ) [coursevalue] 
   => Array ( [0] => 300 ) [coursecategory] => Array ( [0] => Product Knowledge )[
   Submit] => Submit )`
 * thanks for sticking with me on this. been a long day and its the one bit that
   will make all the work ive done sit together nicely 😛 (just so happens to be
   the bit i cant get right!
 *  [Andrew Bartel](https://wordpress.org/support/users/andrew-bartel/)
 * (@andrew-bartel)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/adding-data-to-custom-database/#post-3095683)
 * Try an echo `$_POST['Submit']`. That’s not giving you anything?
 * Why are you using arrays in the form for the names? name=”coursename[]” vs name
   =”coursename”. That would cause null values for what you’re trying to set: ‘coursename’
   => ‘$coursename’ as $coursename would be array rather than a value in of itself.
 *  Thread Starter [j3ddesign](https://wordpress.org/support/users/j3ddesign/)
 * (@j3ddesign)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/adding-data-to-custom-database/#post-3095689)
 * I had the names as arrays in the form as i had been looking around the web to
   try and solve this before i posted and that seemed to be what others had. I have
   however removed this and below is what i have in my add new course page now. `
   echo $_POST['Submit']` Seemed to do nothing what so ever
 *     ```
       <form action="" method="post" id="addcourse">
   
       <label id="coursename">Course Name:<input type="text" name="coursename" size="30" /></label>
       <label id="coursevalue">Points Value:<input type="text" name="coursevalue" size="10"  /></label>
       <label id="coursecategory">Course Category:<select name="coursecategory" size="1">
               <option selected>Product Knowledge</option>
               <option>Demonstrate Effectively</option>
               <option>Perfect Your Pitch</option>
               <option>Business Integration and Technical Training</option>
           </select></label>
   
       <p>&nbsp;</p>
       <p>&nbsp;</p>
       </div>
       <input type="submit" name="Submit" id="addcoursesubmit" value="Submit" />
       </form>
       <a href="" onClick="return false;" id="addform">Add Another Course.</a>
       <?php
   
       If($_POST['Submit']) {
       // run validation if you're not doing it in js
   
       global $wpdb;
   
       $coursename=$_POST['coursename'];
       $coursevalue=$_POST['coursevalue'];
       $coursecategory=$_POST['coursecategory'];
   
       $wpdb->insert(
       		'allcourses',
       		array(
       				'coursename' => '$coursename',
       				'coursevalue' => '$coursevalue',
       				'coursecategory' => '$coursecategory'
       			),
       			array(
       			 '$s',
       			 '$d',
       			 '$s'
       			 )
       );
   
       }
       echo $_POST['Submit'];
       ?>
       ```
   
 *  [Andrew Bartel](https://wordpress.org/support/users/andrew-bartel/)
 * (@andrew-bartel)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/adding-data-to-custom-database/#post-3095695)
 * Ok, put this in and let me know what is says:
 *     ```
       <?php
   
       If($_POST['Submit']) {
       // run validation if you're not doing it in js
       die('successfully posted');
       global $wpdb;
   
       $coursename=$_POST['coursename'];
       $coursevalue=$_POST['coursevalue'];
       $coursecategory=$_POST['coursecategory'];
   
       $wpdb->insert(
       		'allcourses',
       		array(
       				'coursename' => '$coursename',
       				'coursevalue' => '$coursevalue',
       				'coursecategory' => '$coursecategory'
       			),
       			array(
       			 '$s',
       			 '$d',
       			 '$s'
       			 )
       );
   
       }
       else // else we didn't submit the form, so display the form
       {
       die('if statement failed');
       ?><form action="" method="post" id="addcourse">
   
       <label id="coursename">Course Name:<input type="text" name="coursename" size="30" /></label>
       <label id="coursevalue">Points Value:<input type="text" name="coursevalue" size="10"  /></label>
       <label id="coursecategory">Course Category:<select name="coursecategory" size="1">
               <option selected>Product Knowledge</option>
               <option>Demonstrate Effectively</option>
               <option>Perfect Your Pitch</option>
               <option>Business Integration and Technical Training</option>
           </select></label>
   
       <p>&nbsp;</p>
       <p>&nbsp;</p>
       </div>
       <input type="submit" name="Submit" id="addcoursesubmit" value="Submit" />
       </form>
       <a href="" onClick="return false;" id="addform">Add Another Course.</a>
       <?php
       } // end else no post['submit']
       ?>
       ```
   
 *  Thread Starter [j3ddesign](https://wordpress.org/support/users/j3ddesign/)
 * (@j3ddesign)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/adding-data-to-custom-database/#post-3095700)
 * it says “successfully posted” but after checking phpmyadmin, its not been added:/
   
   just a thought really, i have nothing in the “validation” section… im assuming
   thats what im missing, but this is more beyond me than i thought it was already
   😛
 * basically, the coursename is a simple string, points value is a 2-3 digit int
   and course category again is a string. not sure how to validate that.
 * I am ever so greatful for your help by the way. So many times ive posted here
   and they get ignored 🙂
 *  [Andrew Bartel](https://wordpress.org/support/users/andrew-bartel/)
 * (@andrew-bartel)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/adding-data-to-custom-database/#post-3095704)
 * Sorry, that was just to make sure we were passing the logic check. Ok, now try
   this:
 *     ```
       <?php
   
       If($_POST['Submit']) {
       // run validation if you're not doing it in js
       global $wpdb;
   
       $coursename=$_POST['coursename'];
       $coursevalue=$_POST['coursevalue'];
       $coursecategory=$_POST['coursecategory'];
   
       if($wpdb->insert(
       		'allcourses',
       		array(
       				'coursename' => '$coursename',
       				'coursevalue' => '$coursevalue',
       				'coursecategory' => '$coursecategory'
       			)
       ) == false) wp_die('Database Insertion failed'); else echo 'Database insertion successful<p />';
   
       ?>
       <a href="" onClick="return false;" id="addform">Add Another Course.</a>
       <?php
       }
       else // else we didn't submit the form, so display the form
       {
       ?><form action="" method="post" id="addcourse">
   
       <label id="coursename">Course Name:<input type="text" name="coursename" size="30" /></label>
       <label id="coursevalue">Points Value:<input type="text" name="coursevalue" size="10"  /></label>
       <label id="coursecategory">Course Category:<select name="coursecategory" size="1">
               <option selected>Product Knowledge</option>
               <option>Demonstrate Effectively</option>
               <option>Perfect Your Pitch</option>
               <option>Business Integration and Technical Training</option>
           </select></label>
   
       <p> </p>
       <p> </p>
       </div>
       <input type="submit" name="Submit" id="addcoursesubmit" value="Submit" />
       </form>
       <a href="" onClick="return false;" id="addform">Add Another Course.</a>
       <?php
       } // end else no post['submit']
       ?>
       ```
   
 *  Thread Starter [j3ddesign](https://wordpress.org/support/users/j3ddesign/)
 * (@j3ddesign)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/adding-data-to-custom-database/#post-3095705)
 * we are so close! it inserted a value… but that value was “$coursename” “0” “$
   coursecategory”
 * i so need to go to bed (UK) but i want this done so i can show my boss. priorities!
 *  [Andrew Bartel](https://wordpress.org/support/users/andrew-bartel/)
 * (@andrew-bartel)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/adding-data-to-custom-database/#post-3095707)
 * Right, you’ve got them in quotes. Missed that.
 * Can you confirm that the column name in the database is indeed coursevalue?
 *     ```
       <?php
   
       If($_POST['Submit']) {
       // run validation if you're not doing it in js
       global $wpdb;
   
       $coursename=$_POST['coursename'];
       $coursevalue=$_POST['coursevalue'];
       $coursecategory=$_POST['coursecategory'];
   
       if($wpdb->insert(
       		'allcourses',
       		array(
       				'coursename' => $coursename,
       				'coursevalue' => $coursevalue,
       				'coursecategory' => $coursecategory
       			)
       ) == false) wp_die('Database Insertion failed'); else echo 'Database insertion successful<p />';
   
       ?>
       <a href="" onClick="return false;" id="addform">Add Another Course.</a>
       <?php
       }
       else // else we didn't submit the form, so display the form
       {
       ?><form action="" method="post" id="addcourse">
   
       <label id="coursename">Course Name:<input type="text" name="coursename" size="30" /></label>
       <label id="coursevalue">Points Value:<input type="text" name="coursevalue" size="10"  /></label>
       <label id="coursecategory">Course Category:<select name="coursecategory" size="1">
               <option selected>Product Knowledge</option>
               <option>Demonstrate Effectively</option>
               <option>Perfect Your Pitch</option>
               <option>Business Integration and Technical Training</option>
           </select></label>
   
       <p> </p>
       <p> </p>
       </div>
       <input type="submit" name="Submit" id="addcoursesubmit" value="Submit" />
       </form>
       <a href="" onClick="return false;" id="addform">Add Another Course.</a>
       <?php
       } // end else no post['submit']
       ?>
       ```
   
 *  Thread Starter [j3ddesign](https://wordpress.org/support/users/j3ddesign/)
 * (@j3ddesign)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/adding-data-to-custom-database/#post-3095709)
 * You sir, are a life-saver. i can now go to bed knowing that atleast this section
   is done.
 * Thank you so much, its working now 🙂
 * the only thing that isnt is the link back after completion but im sure ill find
   out when i look into why.
 * Again, thank you. 🙂
 *  [Andrew Bartel](https://wordpress.org/support/users/andrew-bartel/)
 * (@andrew-bartel)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/adding-data-to-custom-database/#post-3095725)
 * You’re welcome, glad it’s working. Please mark as resolved!
 *  Thread Starter [j3ddesign](https://wordpress.org/support/users/j3ddesign/)
 * (@j3ddesign)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/adding-data-to-custom-database/#post-3095737)
 * topic is resolved, thanks for assisting

Viewing 14 replies - 1 through 14 (of 14 total)

The topic ‘Adding data to custom database’ is closed to new replies.

## Tags

 * [array](https://wordpress.org/support/topic-tag/array/)
 * [database](https://wordpress.org/support/topic-tag/database/)
 * [form](https://wordpress.org/support/topic-tag/form/)
 * [html](https://wordpress.org/support/topic-tag/html/)
 * [sql](https://wordpress.org/support/topic-tag/sql/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 14 replies
 * 2 participants
 * Last reply from: [j3ddesign](https://wordpress.org/support/users/j3ddesign/)
 * Last activity: [13 years, 7 months ago](https://wordpress.org/support/topic/adding-data-to-custom-database/#post-3095737)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
