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;
}
}
-
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.Polynomial– Polynomial2011年12月05日 14:05:59 +00:00Commented 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.phpCarsten– Carsten2011年12月05日 14:08:50 +00:00Commented Dec 5, 2011 at 14:08
-
I am sorry am little ignorant about this..can you please guide me?Robbie Dc– Robbie Dc2011年12月05日 14:09:06 +00:00Commented Dec 5, 2011 at 14:09
1 Answer 1
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);
answered Dec 5, 2011 at 14:06
Carlos Campderrós
23.2k12 gold badges54 silver badges57 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default