Title: wpdb insert
Last modified: February 18, 2022

---

# wpdb insert

 *  [hsysgrp](https://wordpress.org/support/users/hsysgrp/)
 * (@hsysgrp)
 * [4 years, 3 months ago](https://wordpress.org/support/topic/wpdb-insert-2/)
 * No error, and no insert, either. What is wrong?
 *     ```
       		global $wpdb; 
       	$wpdb->show_errors(true);
       	$query2 = "INSERT INTO AAUW_Members ( ID, Title, FirstName, LastName, Address1, City, State, Zip, HomePhone, CellPhone, Email, 
       			College1, College2, College3, Birth_Day, Birth_Month, Mem_Type, Honorary, Joined_Local,Joined_Natl, Mailings) 
       	  		Values (%d, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"	;	
       	$prepare_query = $wpdb->prepare($query2, $ID, $Title, $FirstName, $LastName, $Address1, $City, $State, $Zip, $HomePhone, $CellPhone, $Email, 
       	$College1, $College2, $College3, $Birth_Day, $Birth_Month, $Mem_Type, $Honorary, $Joined_Local,$Joined_Natl, $Mailings); 
       			$result = $wpdb->query($prepare_query);
   
       		echo "print_r($result)";
       ```
   
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fwpdb-insert-2%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

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

 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [4 years, 3 months ago](https://wordpress.org/support/topic/wpdb-insert-2/#post-15380649)
 * Your last debug-line does not output the value. But I would recommend to replace
   it with:
 * `echo print_r($wpdb);`
 * There you should see the WP Query-object where you will see the generated SQL-
   statement and all errors, if the happend.
 *  Thread Starter [hsysgrp](https://wordpress.org/support/users/hsysgrp/)
 * (@hsysgrp)
 * [4 years, 3 months ago](https://wordpress.org/support/topic/wpdb-insert-2/#post-15380841)
 * Page I need help with is :[https://hsysgrp.com/restore-record-wpdb/](https://hsysgrp.com/restore-record-wpdb/)
   
   added echo print_r($wpdb); with no output.
 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [4 years, 3 months ago](https://wordpress.org/support/topic/wpdb-insert-2/#post-15380862)
 * No output? Then something is wrong in your code. Please post the whole code you
   wrote here.
 *  Thread Starter [hsysgrp](https://wordpress.org/support/users/hsysgrp/)
 * (@hsysgrp)
 * [4 years, 3 months ago](https://wordpress.org/support/topic/wpdb-insert-2/#post-15381069)
 * Am replacing straight sql with wpdb functions. Code works up to the global $wpdb;
   then nothing.`
    $sql = “SELECT ID, Title, FirstName,LastName, Address1, City,
   State, Zip, HomePhone, CellPhone, Email, “; $sql .= ” College1, College2, College3,
   Birth_Day, Birth_Day, Birth_Month, Mem_Type, Honorary, Joined_Local, “; $sql .
   = “Joined_Natl, Mailings “;
 *  $sql .= “FROM AAUW_Archived WHERE ID = ‘$ID’ “;
 *  echo $sql, ‘<br>’;
 *  $retrieve_data = mysqli_query($link, $sql);
    if (mysqli_num_rows($retrieve_data)
   > 0) { while($row = mysqli_fetch_array($retrieve_data)) {
 *  echo “ID: ” . $row[“ID”]. “<br>”;
    echo “Title: ” . $row[“Title”] . “<br>”; 
   echo “FirstName: ” . $row[“FirstName”] . “<br>”; echo “LastName: ” . $row[“LastName”].“
   <br>”; echo “Address1: ” . $row[“Address1”] . “<br>”; echo “City: ” . $row[“City”].“
   <br>”; echo “State: ” . $row[“State”] . “<br>”; echo “Zip: ” . $row[“Zip”] . “
   <br>”; echo “HomePhone: ” . $row[“HomePhone”] . “<br>”; echo “CellPhone: ” . 
   $row[“CellPhone”] . “<br>”; echo “Email: ” . $row[“Email”] . “<br>”; echo “College1:”.
   $row[“College1”] . “<br>”; echo “College2: ” . $row[“College2”] . “<br>”; echo“
   College3: ” . $row[“College3”] . “<br>”; echo “Birth_Day: ” . $row[“Birth_Day”].“
   <br>”; echo “Birth_Month: ” . $row[“Birth_Month”] . “<br>”; echo “Mem_Type: ”.
   $row[“Mem_Type”] . “<br>”; echo “Honorary: ” . $row[“Honorary”] . “<br>”; echo“
   Joined_Local: ” . $row[“Joined_Local”] . “<br>”; echo “Joined_Natl: ” . $row[“
   Joined_Natl”] . “<br>”; echo “Mailings: ” . $row[“Mailings”] . “<br>”;
 *  $ID = $row[‘ID’];
 *  $Title = $row[‘Title’];
    $FirstName = $row[‘FirstName’]; $LastName = $row[‘LastName’];
   $Address1 = $row[‘Address1’]; $City = $row[‘City’]; $State = $row[‘State’]; $
   Zip = $row[‘Zip’]; $HomePhone = $row[‘HomePhone’]; $CellPhone = $row[‘CellPhone’];
   $Email = $row[‘Email’]; $College1 = $row[‘College1’]; $College2 = $row[‘College2’];
   $College3 = $row[‘College3’]; $Birth_Day = $row[‘Birth_Day’]; $Birth_Month = 
   $row[‘Birth_Month’]; $Mem_Type = $row[‘Mem_Type’]; $Honorary = $row[‘Honorary’];
   $Joined_Local = $row[‘Joined_Local’]; $Joined_Natl = $row[‘Joined_Natl’]; $Mailings
   = $row[‘Mailings’];
 *  echo “Variables”;
    } // while mysqli_fetch…retrieve_data } else { // else of
   retrieve_data > 0 echo “0 results”; } } // else of while mysqli_fetch…query //
   Sql to Insert into AAUW_Members using wpdb echo $ID; echo $Title; echo $FirstName;
 *  global $wpdb;
    $wpdb->show_errors(true); $query2 = “INSERT INTO AAUW_Members(
   ID, Title, FirstName, LastName, Address1, City, State, Zip, HomePhone, CellPhone,
   Email, College1, College2, College3, Birth_Day, Birth_Month, Mem_Type, Honorary,
   Joined_Local,Joined_Natl, Mailings) Values (%d, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%
   s,%s,%s,%s,%s,%s,%s,%s,%s,%s)” ; $prepare_query = $wpdb->prepare($query2, $ID,
   $Title, $FirstName, $LastName, $Address1, $City, $State, $Zip, $HomePhone, $CellPhone,
   $Email, $College1, $College2, $College3, $Birth_Day, $Birth_Month, $Mem_Type,
   $Honorary, $Joined_Local, $Joined_Natl, $Mailings); $result = $wpdb->query($prepare_query);
   echo print_r($wpdb);
 *  } // else of count == 0
    } //Close isset search `
 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [4 years, 3 months ago](https://wordpress.org/support/topic/wpdb-insert-2/#post-15381097)
 * Unfortunately, your code is still not recognizable in which context it is executed.
   You see neither a WordPress hook nor a function name. Thus, it is unclear to 
   me in which context this is executed. Therefore, I can not judge why the loading
   of $wpdb does not work for you.
 * Even more: I wonder why you use above mysqli_query() on a seemingly not initialized
   $link.
 * What exactly do you want to achieve here? Is the database table you want to access
   in the WordPress database or is it in an external database?
 *  Thread Starter [hsysgrp](https://wordpress.org/support/users/hsysgrp/)
 * (@hsysgrp)
 * [4 years, 3 months ago](https://wordpress.org/support/topic/wpdb-insert-2/#post-15382317)
 * Thank you for your response. The context is the original template file custom-
   page_RestoreID.php searches for a record in an archive table and inserts that
   record into the members table using “$sqlInsert .= “INSERT INTO AAUW_Members (
   ID, Title, FirstName, LastName, Address1, ….”. I am attempting to convert sql
   statements into $wpdb format for security reasons. The template file custom-page_RestoreID_wpdb.
   page replaces $sqlInsert with “$query2 = “INSERT INTO AAUW_Members ( ID, Title,
   FirstName, LastName, Address1,…. $prepare_query = $wpdb->prepare($query2, $ID,
   $Title, $FirstName, $LastName, $Address1, etc. I know I am missing something,
   as it appears the insert is not executing at all. Self-taught here, appreciate
   all the help.
 *  [Dion](https://wordpress.org/support/users/diondesigns/)
 * (@diondesigns)
 * [4 years, 3 months ago](https://wordpress.org/support/topic/wpdb-insert-2/#post-15383193)
 * If using `print_r($wpdb)` results in no output, then I have to ask: is your code
   loading WordPress?
 * You should dump/echo your `$prepare_query` variable before executing the query.
   Does the query look corrrect?
 * Since your code is already working with `mysqli` functions, perhaps you should
   continue to use them. If your external tables are in the same database as the
   WordPress tables, you can obtain the DB handle from WordPress as follows:
 * `$link = $wpdb->__get('dbh');`
 * In that way you don’t need to open a second connection to the database. The above
   will also work if the external tables are in a separate database and the WordPress
   DB user has access to the database.
 * While bypassing `$wpdb` is perhaps frowned upon by WP purists, it should be stated
   that `$wpdb` is antiquated, bloated, and a CPU/memory pig. And I’m being kind.
   🙂
 *  Thread Starter [hsysgrp](https://wordpress.org/support/users/hsysgrp/)
 * (@hsysgrp)
 * [4 years, 3 months ago](https://wordpress.org/support/topic/wpdb-insert-2/#post-15384887)
 *     ```
       INSERT INTO AAUW_Members (ID, Title, FirstName,LastName,Address1,City, State, Zip) VALUES ('111114', 'Ms.', 'Rosalind', 'Franklin', '10 Birkdale', 'Poughkeepsie', 'NY', '12603')  This sql works in phpmyadmin.
   
   
       $query2 = "Insert into AAUW_Members ( ID, Title, FirstName,LastName, Address1, City, State, Zip) Values (%d,%s,%s,%s,%s,%s,%s,%s)";
   
       $prepare_query = $wpdb->prepare($query2,$ID,$Title,$FirstName,$LastName,$Address1, $City, $State, $Zip);
       $result = $wpdb->query($prepare_query);
       		echo print_r($result);
       PHP Fatal error:  Uncaught Error: Call to a member function prepare() on null in /home4/hsysgrpc/public_html/wp-content/themes/twentytwelve-child/custom-page_RestoreID_wpdb.php:121
       ```
   
 * Don’t know what that means..
 *  [Dion](https://wordpress.org/support/users/diondesigns/)
 * (@diondesigns)
 * [4 years, 3 months ago](https://wordpress.org/support/topic/wpdb-insert-2/#post-15388819)
 * It means your code is not loading WordPress, so the `$wpdb` variable is unavailable.
 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [4 years, 3 months ago](https://wordpress.org/support/topic/wpdb-insert-2/#post-15388839)
 * In this context I would like to know what is the template file _custom-page\_RestoreID.
   php_ you are referring to? Do you use a particular theme in which there is this
   file? Again, the entire source code would be relevant to help you.
 *  [Vijay Hardaha](https://wordpress.org/support/users/vijayhardaha/)
 * (@vijayhardaha)
 * [4 years, 3 months ago](https://wordpress.org/support/topic/wpdb-insert-2/#post-15396155)
 *     ```
       global $wpdb;
       $table = $wpdb->prefix . 'you_table_name_without_prefix';
       $data  = array(
       	'column1' => 'data one',
       	'column2' => 'data two',
       	// And so on.
       );
       $wpdb->insert( $table, $data );
       $insert_id = $wpdb->insert_id;
       if ( empty( $insert_id ) ) {
       	// then handle the error
       }
   
       // else do your job.
       ```
   
 * Your insert query code should be like this in WordPress.

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

The topic ‘wpdb insert’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 11 replies
 * 4 participants
 * Last reply from: [Vijay Hardaha](https://wordpress.org/support/users/vijayhardaha/)
 * Last activity: [4 years, 3 months ago](https://wordpress.org/support/topic/wpdb-insert-2/#post-15396155)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
