WordPress.org

Support

Support » How-To and Troubleshooting » Why wpdb->query firing two times?

Why wpdb->query firing two times?

  • Hello All,
    I had added the following code on form submit,
    It will run when form is submit.
    The problem is, in database it is saving but its saving two times.
    I am just starter in wordpress, so if i am missing something then please tell me.

    if(isset($_POST['submitted']) && ($_SERVER['REQUEST_METHOD'] == 'POST')) {
    
    	global $wpdb;
    	$company_name = $wpdb->prefix . "company";
    	$single_table = $wpdb->prefix . "single";
    	$emp_table = $wpdb->prefix . "employee";
    	$idea_table = $wpdb->prefix . "idea";
    
    	if($_POST['team-type'] == "Freelancer"){
    		$wpdb->query( $wpdb->prepare("INSERT INTO $company_name (name, team_size) VALUES(%s, %d)", $_POST['cname'],1));
    		$company_id = $wpdb->get_var( "SELECT MAX(id) FROM wp_company" );
    		echo "<h2>1</h2>";
    		$wpdb->query( $wpdb->prepare("INSERT INTO $single_table (company_id, name, bio,linkedin_url) VALUES (%d, %s, %s, %s)", $company_id, $_POST['freelancer-name'] ,$_POST['free-bio'], $_POST['free-url'] ) );
    	}else{
    		$query = "INSERT INTO $table_name (name, team_size) VALUES('" . $_POST['cname'] . "'," . $_POST['team'] .")";
    		$wpdb->query($query);
    		echo "<h2>2</h2>";
    	}
    	echo "<h2>3</h2>";
    }

    and h2 tag, its showing content only once but in database i have two copy of same data.

  • The topic ‘Why wpdb->query firing two times?’ is closed to new replies.