我正在使用 Adobe Flex 通过 PHP 成功地将用户提交到我的数据库中。现在我需要修改我的 PHP 以首先查看用户是否在数据库中,如果是,则更新一些字段,但如果它们是新的,那么我们需要完全创建用户。我已经评论了肯定已经用于创建不存在的用户的代码部分。我不确定我要去哪里错了。我已经删除了关于连接到数据库的部分;效果很好。
public function createUser (User $item)
{
//Checking to see if user exists
$checkdb = mysqli_prepare($this->connection, "SELECT firstname FROM users WHERE id =?");
mysqli_stmt_bind_param($checkdb, "s", $item->id);
mysqli_stmt_execute($checkdb);
mysqli_stmt_fetch($checkdb);
if ($checkdb == 0)
{
// If user exists, create record. This code definitely works.
$stmt = mysqli_prepare($this->connection,
"INSERT INTO users (
id,firstname,lastname,gender,dob,latitude,longitude,datesubmitted)
VALUES (?,?,?,?,?,?,?,?)");
$this->throwExceptionOnError();
mysqli_bind_param($stmt, 'sssssdds', $item->id, $item->firstname,
$item->lastname, $item->gender, $item->dob, $item->latitude,
$item->longitude, $item->datesubmitted);
$this->throwExceptionOnError();
mysqli_stmt_execute($stmt);
$this->throwExceptionOnError();
mysqli_stmt_free_result($stmt);
mysqli_close($this->connection);
}
//If user does exist, then update record
$stmt = mysqli_prepare($this->connection,
"UPDATE users SET latitude=?,longitude=?,datesubmitted=? WHERE id=?");
$this->throwExceptionOnError();
mysqli_bind_param($stmt, 'dds', $item->latitude,
$item->longitude, $item->datesubmitted);
$this->throwExceptionOnError();
mysqli_stmt_execute($stmt);
$this->throwExceptionOnError();
mysqli_stmt_free_result($stmt);
mysqli_close($this->connection);
}