• Anonymous User

    (@anonymized-3905352)


    Hi Everyone,

    I’m learning how to insert data form a custom form into my own mysql database with WP. I’m getting an [object Object] error when I press “submit”. I’m not sure what I’m doing wrong. Any suggestions?

    (Tested it on two pages)
    http://carlots.net/blog/cars/
    http://carlots.net/blog/cars2/

    Function I added to my themes function.

    function addCustomer(){
    
    	global $wpdb;
    
    	$name = $_POST['name'];
    	$phone = $_POST['phone'];
    	$email = $_POST['email'];
    	$address = $_POST['address'];
    
    	if($wpdb->insert('customers',array(
    		'name'=>$name,
    		'email'=>$email,
    		'address'=>$address,
    		'phone'=>$phone
    		))===FALSE){
    
    		echo "Error";
    
    	}
    	else {
    			echo "Customer '".$name. "' successfully added, row ID is ".$wpdb->insert_id;
    
    		}
    	die();
    }
    add_action('wp_ajax_addCustomer', 'addCustomer');
    add_action('wp_ajax_nopriv_addCustomer', 'addCustomer');

    Syntax for cars2

    <?php
    /*
    Template Name: Cars 2
    */
    
    get_header(); ?>
    
                    <div id="primary">
                            <div id="content" role="main">
    
                                    <?php while ( have_posts() ) : the_post(); ?>
    
                                            <?php get_template_part( 'content', 'single' ); ?>
    
                                            <?php
    
                                            if (is_user_logged_in()):?>
    
                                                    <form type="post" action="" id="newCustomerForm">
    
                                                            <label for="name">Name:</label>
                                                            <input name="name" type="text" />
                                                            <br/>
                                                            <label for="email">Email:</label>
                                                            <input name="email" type="text" />
                                                            <br/>
                                                            <label for="phone">Phone:</label>
                                                            <input name="phone" type="text" />
                                                            <br/>
                                                            <label for="address">Address:</label>
                                                            <input name="address" type="text" />
                                                            <br/>
                                                            <input type="hidden" name="action" value="addCustomer"/>
                                                            <input type="submit">
                                                    </form>
                                                    <br/><br/>
                                                    <div id="feedback"></div>
                                                    <br/><br/>
    
                                            <?php
                                                    global $wpdb;
                                                    $customers = $wpdb->get_results("SELECT id, year, make, model, transmission, mileage, state, city, price AS Price
                                                    FROM cars_database d
                                                    WHERE (d.state = 'rhode island')
                                                    GROUP BY (d.make)
    												ORDER BY (d.id);");
    
                                                    echo "<table>";
                                                    foreach($customers as $customer){
                                                            echo "<tr>";
                                                            echo "<td>".$customer->id."</td>";
                                                            echo "<td>".$customer->year."</td>";
                                                            echo "<td>".$customer->make."</td>";
                                                            echo "<td>".$customer->model."</td>";
    														echo "<td>".$customer->transmission."</td>";
    														echo "<td>".$customer->mileage."</td>";
    														echo "<td>".$customer->state."</td>";
    														echo "<td>".$customer->city."</td>";
    														echo "<td>".$customer->price."</td>";
                                                            echo "</tr>";
                                                    }
                                                    echo "</table>";
                                            else:
                                                    echo "Sorry, only registered users can view this information";
                                            endif;                                         
    
                                            ?>
    
                                            <script type="text/javascript">
                                                    jQuery('#newCustomerForm').submit(ajaxSubmit);
    
                                                    function ajaxSubmit(){
    
                                                            var newCustomerForm = jQuery(this).serialize();
    
                                                            jQuery.ajax({
                                                                    type:"POST",
                                                                    url: "/wp-admin/admin-ajax.php",
                                                                    data: newCustomerForm,
                                                                    success:function(data){
                                                            jQuery("#feedback").html(data);
                                                                    },
                                                        error: function(errorThrown){
                                                            alert(errorThrown);
                                                        }
                                                            });
    
                                                            return false;
                                                    }
                                            </script>
    
                                    <?php endwhile; // end of the loop. ?>
    
                            </div><!-- #content -->
                    </div><!-- #primary -->
    
    <?php get_footer(); ?>

    Syntax for cars/

    <?php
    /*
    Template Name: Cars
    */
    /**
     * The Template for displaying all single posts.
     *
     * @package WordPress
     * @subpackage Twenty_Eleven
     * @since Twenty Eleven 1.0
     */
    
    get_header(); ?>
    
    		<div id="primary">
    			<div id="content" role="main">
    
    				<?php while ( have_posts() ) : the_post(); ?>
    
    					<nav id="nav-single">
    						<h3 class="assistive-text"><?php _e( 'Post navigation', 'twentyeleven' ); ?></h3>
    						<span class="nav-previous"><?php previous_post_link( '%link', __( '<span class="meta-nav">←</span> Previous', 'twentyeleven' ) ); ?></span>
    						<span class="nav-next"><?php next_post_link( '%link', __( 'Next <span class="meta-nav">→</span>', 'twentyeleven' ) ); ?></span>
    					</nav><!-- #nav-single -->
    
    					<?php get_template_part( 'content', 'single' ); ?>
    					<?php echo "This is our custom template!";?>
    
    					<?php	
    
    					/* global $wpdb;
    					$cars = $wpdb->get_results("SELECT year, make, model, transmission, mileage, state, city, price AS Price
                        FROM cars_database d
                        WHERE (d.state = 'rhode island')
                        AND (d.city='providence')
                        GROUP BY (d.model)");
                        // print_r($cars);
    					// echo "<tr> <th>Year</th> <th>Make</th> <th>Model</th> <th>Transmission</th> <th>Mileage</th> <th>State</th> <th>City</th> <th>Price</th>";
    					 echo "This is the results for Rhode Island. How would I get the results for others states using this template?";
    					 echo "<table>";
                         foreach($cars as $car){
                         echo "<tr>";
                         echo "<td>".$car->year."</td>";
                         echo "<td>".$car->make."</td>";
                         echo "<td>".$car->model."</td>";
    					 echo "<td>".$car->transmission."</td>";
    					 echo "<td>".$car->mileage."</td>";
    					 echo "<td>".$car->state."</td>";
    					 echo "<td>".$car->city."</td>";
    					 echo "<td>".$car->Price."</td>";
                         echo "</tr>";
                         }
                        echo "</table>"; */
    
    					if (is_user_logged_in()):
    
                        global $wpdb;
                        $cars = $wpdb->get_results("SELECT id, year, make, model, transmission, mileage, state, city, price AS Price
                        FROM cars_database d
                        WHERE (d.state = 'rhode island')
                        GROUP BY (d.make);");
    
                        // echo "This is the results for Rhode Island. How would I get the results for others states using this template?";
                        echo "<table>";
                        foreach($cars as $car){
                        echo "<tr>";
                        echo "<td>".$car->id."</td>";
                        echo "<td>".$car->year."</td>";
                        echo "<td>".$car->make."</td>";
                        echo "<td>".$car->model."</td>";
    
    					echo "<td>".$car->transmission."</td>";
                        echo "<td>".$car->mileage."</td>";
                        echo "<td>".$car->state."</td>";
                        echo "<td>".$car->city."</td>";
    
    				    echo "<td>".$car->Price."</td>";
                        // echo "<td>".$car->time."</td>";
                        echo "</tr>";
                        }
                        echo "</table>";
                        else:
                        echo "Sorry, only registered users can view this information";
                        endif; 
    
    					echo '<form type="post" action="" id="newCustomerForm">
    
                        <label for="name">Name:</label>
                        <input name="name" type="text" />
                        <br/>
                        <label for="email">Email:</label>
                        <input name="email" type="text" />
                        <br/>
                        <label for="phone">Phone:</label>
                        <input name="phone" type="text" />
                        <br/>
                        <label for="address">Address:</label>
                        <input name="address" type="text" />
                        <br/>
                        <input type="hidden" name="action" value="addCustomer"/>
                        <input type="submit">
                        </form>
                        <br/><br/>
                        <div id="feedback"></div>
                        <br/><br/>'
    
                        ?>
    
    					                                        <script type="text/javascript">
                                                    jQuery('#newCustomerForm').submit(ajaxSubmit);
    
                                                    function ajaxSubmit(){
    
                                                            var newCustomerForm = jQuery(this).serialize();
    
                                                            jQuery.ajax({
                                                                    type:"POST",
                                                                    url: "/wp-admin/admin-ajax.php",
                                                                    data: newCustomerForm,
                                                                    success:function(data){
                                                            jQuery("#feedback").html(data);
                                                                    },
                                                        error: function(errorThrown){
                                                            alert(errorThrown);
                                                        }
                                                            });
    
                                                            return false;
                                                    }
                                            </script>
    
    					<?php comments_template( '', true ); ?>
    
    				<?php endwhile; // end of the loop. ?>
    
    			</div><!-- #content -->
    		</div><!-- #primary -->
    
    <?php get_footer(); ?>

The topic ‘Error when inserting data into database?’ is closed to new replies.