Reputation: 183
I've looked at other questions on this but they won't work for me.
I want to check if a user has already logged in, using the value steamid
. If they have, display their info, if they haven't, make a new account in the database. Here is what I have now:
$dbh = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
if($dbh){
echo "Connected successfully";
}
include ('steamauth/userInfo.php');
$stmt = $dbh->prepare("SELECT steam_id FROM user WHERE steam_id = :steam_id");
$stmt->bindParam(':steam_id', $steamprofile['steamid']);
$stmt->execute();
if($stmt->rowCount() > 0){
echo "User already exists!";
}else{
$sql = "INSERT INTO user (display_name, user_url, steam_id, profil_image)
VALUES ('$steamprofile[personaname]', '$steamprofile[profileurl]', $steamprofile[steamid]), '$steamprofile[avatar]'";
/*
if ($dbh->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $dbh->error;
}
*/
}
As of this code, the only return is "connected successfully". No new data is added to the (empty) user database.
If I uncommented the last block, I get the output:
"Notice: Undefined property: PDO::$error in F:\Bitnami\htdocs\Dreamweaver\freehtml5streets\index.php on line 48 Error: INSERT INTO user (display_name, user_url, steam_id, profil_image) VALUES (//list of corresponding values of signed in user).
Upvotes: 1
Views: 2169
Reputation: 982
The error exception is because pdo error exception returns an array of getMessage, getCode and getFile see below and example of its implementation.
public function __construct() {
try {
$this->mssql = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
} catch (PDOException $e) {
throw new CHttpException(404, 'Connection Error, Contact admin for assistance ' . $e);
/** You can access the error messages using $e->getMessage() for the exception error message. $->getCode() for the error code; $e->getFile(); return the file path **/
}
}
//From the exerpt of your code try this
include ('steamauth/userInfo.php');
if (!empty($steamprofile['steamid'])) {
$stmt = $dbh->prepare("SELECT count(*) from user WHERE steam_id = :steam_id");
$stmt->bindValue(':steam_id', $steamprofile['steamid']);
$stmt->execute();
$count = $stmt->fetchColumn();
}
//Row will return false if there was no value
if ($count == 0) {
//insert new data
$sql = "INSERT INTO user (display_name, user_url, steam_id, profil_image)
VALUES ('$steamprofile[personaname]', '$steamprofile[profileurl]', $steamprofile[steamid]), '$steamprofile[avatar]'";
} else {
//User exist
}
Upvotes: 2
Reputation: 27478
You get the error "Undefined Property" because "error" is not defined as a property of $dbh. You probably meant "errorInfo" see here
By the way building up an SQL statement using raw input leaves you totally vulnerable to an SQL injection attack.
Upvotes: 2