0

This code is supposed to check to see if a user is logged in when posting an input, if so, it then checks to see if the ItemID and User ID already have an entry in the table, and if so, it updates that entry rather than creating a duplicate.

This is the output I need from it:

+--------+-------+----------+
| ItemID | Price | user     |
+--------+-------+----------+
| 1      | 20    | 27       |
+--------+-------+----------+

This is what I'm getting:

+--------+-------+----------+
| ItemID | Price | user     |
+--------+-------+----------+
| 0      | 0     | 27       |
+--------+-------+----------+

Here is the full function if needed : http://pastebin.com/W0UM68UT

if ( is_user_logged_in() ) {

    $hf_user = get_current_user_id();
    $hf_username = $hf_user->user_login;
    global $quanid;
    $inputValue  = isset($_POST[$quanid]);
    global $newdb;
    $newdb = new wpdb( 'user', 'pass', 'db', 'localhost' );
    $retval = $newdb->get_results("SELECT * FROM $table WHERE ItemID = '$quanid' AND user = '$hf_username' ");

if($retval > 0) 
{  
    //If user exists, update
    $newdb->replace($table,array(
    'ItemID' => '$quanid',
    'Price' => $inputValue,
    'user' => $hf_username
        )
    ); 
}
else
{
        global $table;
        global $newdb;
        $newdb->insert( 
            $table,
            array( 
                'ItemID' => $quanid,
                'Price'  => $inputValue,
                'user'   => $hf_username
            )
        );
}



} else {
    global $error;
     $error = "Error: You must be logged in to submit prices";
     return; 
}


}
Kreation
  • 327
  • 3
  • 10

1 Answers1

1

Please don't use globals...

How to avoid using PHP global objects?

Change your SELECT statement to a count for better performance:

SELECT count( 1 ) FROM $table WHERE ItemID = '$quanid' AND user = '$hf_username'

On to your question:

It appears your global $quanid; and isset($_POST[$quanid]); return unexpected values so you should see where they're set. Try using:

var_dump right below these two lines:

global $quanid;
$inputValue  = isset($_POST[$quanid]);
var_dump( $quanid );
var_dump( $inputValue  );
var_dump( $_POST);
die;
Community
  • 1
  • 1
Vladimir Ramik
  • 1,920
  • 2
  • 13
  • 23
  • var_dump is resulting in these two notices: `Notice: Trying to get property of non-object in /directory/plugin.php on line 131 string(2) "10"` `Notice: Undefined variable: inputValue in /directory/plugin.php on line 134 NULL array(0) { }` Line 131: `$hf_username = $hf_user->user_login;` Line 134: `var_dump( $inputValue );` – Kreation Mar 23 '15 at 01:49
  • Appears you're either not testing during a post or your post doesn't have anything. Try to stub it out until you have it working... $quanid = 1; $_POST[ 1 ] = 'abc'; $inputValue = 'abc'; then troubleshoot with var_dump/die mix to pinpoint your issue. – Vladimir Ramik Mar 23 '15 at 01:58