Kreation
Kreation

Reputation: 327

Wordpress Form php code submitting blank values

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; 
}


}

Upvotes: 0

Views: 38

Answers (1)

Vladimir Ramik
Vladimir Ramik

Reputation: 1930

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;

Upvotes: 1

Related Questions