Reputation: 701
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;
}
}
Upvotes: 1
Views: 117
Reputation: 22972
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);
Upvotes: 2