• 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 to see the link]

Viewing 11 replies - 1 through 11 (of 11 total)
  • 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

    (@hsysgrp)

    Page I need help with is :https://hsysgrp.com/restore-record-wpdb/
    added echo print_r($wpdb); with no output.

    No output? Then something is wrong in your code. Please post the whole code you wrote here.

    Thread Starter hsysgrp

    (@hsysgrp)

    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
    `

    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

    (@hsysgrp)

    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

    (@diondesigns)

    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

    (@hsysgrp)

    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

    (@diondesigns)

    It means your code is not loading WordPress, so the $wpdb variable is unavailable.

    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.

    
    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.