• This a fragment from a program that accepts an ID number and then posts it to a record.
    Worked fine in SQL but clearly the $wpdb version is missing something..
    echo gets AAUW_New_Members 99999 Array Array

    $table_name = 'AAUW_New_Members';
    	$MemberID = '99999';
    	$data_update = array('MemberID'=>'$MemberID');
    	$data_where = array('LastName' => 'Curie');
    		echo $table_name;
    		echo $MemberID;
    		echo $data_update;
    		echo $data_where;
    		
      	$wpdb->update($table_name, $data_update, $data_where);
    		exit( var_dump( $wpdb->last_query ) );

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • The only thing that stands out is you are not using quotes corectly on this line:

    $data_update = array('MemberID'=>'$MemberID');

    It should be without the quotes. Single quotes like you have don’t allow for variable to be substibuted (double quotes will), so it the variable should not be wrapped in quotes at all to keep it easier:

    $data_update = array('MemberID'=>$MemberID);

    MK

    (@mkarimzada)

    This is correct. The var_dump should return string type. If you take a look at wpdb::update method, it calls/returns wpdb::prepare which sanitize the SQL query for safe execution.

    To properly check the execution:

    $result = $wpdb->update($table_name, $data_update, $data_where);
    
    if ($result === false) // Failed
    if ($result === true) // Updated
    

    I hope this helps.

    Thread Starter hsysgrp

    (@hsysgrp)

    Driving me berserk.

    $sql  = "UPDATE AAUW_New_Members SET MemberID = '$searchn' WHERE FirstName = '$firstName' AND   LastName = '$lastName' ";
    

    if(mysqli_query($link,$sql)) {
    echo ” New Record Inserted” ;
    } else {
    {
    echo “Error: No record inserted” ;
    }`
    array(1) { [“MemberID”]=> string(7) “7777777” }
    array(2) { [“FirstName”]=> string(5) “Marie” [“LastName”]=> string(5) “Curie” }

    	$table_name = 'AAUW_New_Members';
    	echo $table_name;
    	echo "<br>";
    	$MemberID = $searchn;
    	echo $MemberID;
    	"<br>";
    	
    	$data_update = array("MemberID"=>$searchn);
    	$data_where = array("FirstName" => $firstName,
    						"LastName" => $lastName
    					   );
    	 var_dump($data_update ) ;	
    	echo "<br>";
    	 var_dump($data_where ) ;
    	
    		echo $MemberID;
    		echo "<br>";
    		echo $data_update;
    	echo "<br>";
    		echo $data_where;
    	echo "<br>";
    	
     	$wpdb->update($table_name, $data_update, $data_where);

    MemberID does not update.

    Thread Starter hsysgrp

    (@hsysgrp)

    The error I get is Call to a member function update() on null in /home4/hsysgrpc/public_html/wp-content/themes/twentytwelve-child/custom-page_InputMemberID-wpdb.php:51
    Stack trace:
    I have tried without success
    require_once(‘../wp-load.php’); // relative path
    global $wpdb;

    $wpdb->show_errors(true); also

    $updated = $wpdb->update($tablename,
    array(‘MemberID’ => $searchn
    ),
    array(‘LastName’ =>$searchq
    )
    );
    echo print_r($updated);

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘$wpdb->update’ is closed to new replies.