1

I use this function to check whether the record exists in my database and act accordingly. There is some error in the second part when the data is not matched: it is supposed to insert a new record, but it does not insert new data, and it returns empty data. Where am I going wrong?

 class User {

function checkUser($uid, $oauth_provider, $username,$email,$twitter_otoken,$twitter_otoken_secret) 
{

     // Define database connection constants
define('DB_HOST', 'localhost');
define('DB_USER', '*********');
define('DB_PASSWORD', '********');
define('DB_NAME', '******');
    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

    $query ="SELECT * FROM si_table WHERE oauth_uid = '$uid' and oauth_provider = '$oauth_provider'";
    $data=mysqli_query($dbc,$query);
    $result = mysqli_fetch_array($data);
    if (!empty($result)) {

        # User is already present
    } else {

        #user not present. Insert a new Record

     $query2 ="INSERT INTO si_table (oauth_provider, oauth_uid, user_name,email_id,twitter_oauth_token,twitter_oauth_token_secret) VALUES ('$oauth_provider',$uid,'$username','$email')";
        mysqli_query($dbc,$query2);
        $query1 ="SELECT * FROM si_table WHERE oauth_uid = '$uid' and oauth_provider = '$oauth_provider'";
        $data1=mysqli_query($dbc,$query1);
        $row = mysqli_fetch_array($data1);

        return $row;

    }
    return $result;
}



}
3
  • Why are you using mysqli and concatenated query strings? Please strongly consider using parameterized queries - it is a small investment that will save you the horror of SQL injections. Commented Dec 5, 2011 at 14:05
  • More importantly, what's going on with the INSERT statement? That one looks wrong. Better check for errors: de3.php.net/manual/en/mysqli.error.php Commented Dec 5, 2011 at 14:08
  • I am sorry am little ignorant about this..can you please guide me? Commented Dec 5, 2011 at 14:09

1 Answer 1

2

You have an error in your insert query: you are providing just 4 values for 6 fields.

Do some error checking on the return value of mysqli_query($dbc, $query2);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.