WordPress.org

Ready to get started?Download WordPress

Forums

Custom Form not inserting data to database (1 post)

  1. Jake256
    Member
    Posted 5 months ago #

    I have created two custom template pages(createhome.php & insert.php)

    My createhome.php has a form whose action connects to a file called insert.php

    The insert.php should then insert the input to the database using wpdb->insert()

    However, when I click submit, it takes me to a 404 page not found, and the url is: http://hf.mr21solutions.com/create-home/insert.php

    Nothing is inserted into the database. BUT, when I navigate to the insert page(http://hf.mr21solutions.com/insert/) it inserts blank data to the database!

    To see for yourself, submit the form here:(http://hf.mr21solutions.com/create-home/)

    So, apparently my form is not connecting to my insert.php file on submission. Does this have something to do with my form's action? Or is this something with the permalinks? I just want to be able to save my form's input data into my newly created table in the wp database. If this was regular php/mysql, I wouldn't have a problem in the world.

    Here is my form:
    <h2><center> Enter your home information: </center></h2>

    <form action="insert.php" method="post">
                            Name: <input type="text" name="owner_name"><br />
                            Address: <input type="text" name="address"><br />
                            City: <input type="text" name="city"><br />
                            State: <input type="text" name="state"><br />
                            Zip Code: <input type="text" name="zipcode"><br />
                            Number of Rooms: <input type="text" name="num_rooms"><br />
                            Number of Bedrooms: <input type="text" name="num_bedrooms"><br />
                            Number of Bathrooms: <input type="text" name="num_bathrooms"><br />
                            Square Feet: <input type="text" name="square_feet"><br />
                            Age of Home: <input type="text" name="home_age"><br />
                            			 <input type="hidden" name="id">
                            			 <input type="hidden" name="owner_id">
                            <input type="submit">
                            </form>

    Here is my insert.php code:

    <?php
    if(!empty($_POST)){
    global $wpdb;
    $wpdb->insert(
    'wp_homes',
    array(
    								'owner_name' => '$_POST[owner_name]',
    								'address' => '$_POST[address]',
    								 'city' => '$_POST[city]',
    								'state' => '$_POST[state]',
    								'zipcode' => '$_POST[zipcode]',
    								'num_rooms' => '$_POST[num_rooms]',
    								'num_bedrooms' => '$_POST[num_bedrooms]',
    								'num_bathrooms' => '$_POST[num_bathrooms]',
    								'square_feet' => '$_POST[square_feet]',
    								'home_age' => '$_POST[home_age]'
    								 )
    						);
    
                            echo "1 record added";
    						}
                            ?>

Reply

You must log in to post.

About this Topic