WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Adding data to custom database (15 posts)

  1. j3ddesign
    Member
    Posted 1 year ago #

    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>"
    
    ?>
  2. Andrew Bartel
    Member
    Posted 1 year ago #

    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'
    			 )
    );
    
    }
  3. j3ddesign
    Member
    Posted 1 year ago #

    Thanks for the reply.
    Adapted code to what you suggested, however now it just refreshes the page and does not submit.

  4. Andrew Bartel
    Member
    Posted 1 year ago #

    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">

  5. j3ddesign
    Member
    Posted 1 year ago #

    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 :P (just so happens to be the bit i cant get right!

  6. Andrew Bartel
    Member
    Posted 1 year ago #

    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.

  7. j3ddesign
    Member
    Posted 1 year ago #

    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'];
    ?>
  8. Andrew Bartel
    Member
    Posted 1 year ago #

    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']
    ?>
  9. j3ddesign
    Member
    Posted 1 year ago #

    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 :P

    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 :)

  10. Andrew Bartel
    Member
    Posted 1 year ago #

    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']
    ?>
  11. j3ddesign
    Member
    Posted 1 year ago #

    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!

  12. Andrew Bartel
    Member
    Posted 1 year ago #

    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']
    ?>
  13. j3ddesign
    Member
    Posted 1 year ago #

    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. :)

  14. Andrew Bartel
    Member
    Posted 1 year ago #

    You're welcome, glad it's working. Please mark as resolved!

  15. j3ddesign
    Member
    Posted 1 year ago #

    topic is resolved, thanks for assisting

Topic Closed

This topic has been closed to new replies.

About this Topic