-1

我正在使用 Adob​​e 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);
}
4

3 回答 3

1

您的意思是在一个else块中包含更新用户帐户的代码吗?

public function createUser (User $item)
{
    ...     

    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 (?,?,?,?,?,?,?,?)");
      ...
    }
    else
    {
      //If user does exist, then update record
      $stmt = mysqli_prepare($this->connection,
        "UPDATE users SET latitude=?,longitude=?,datesubmitted=? WHERE id=?");
      ...
    }
    ...
}
于 2012-04-26T23:12:15.447 回答
0

您有很多不需要的代码可以删除。还可以考虑通过数据库不会自动生成的唯一值进行搜索,例如用户的电子邮件地址。

另外,不要担心关闭数据库连接。它会让事情变得简单得多。

我建议您查看一个 PHP 框架来帮助您入门。查看Zend FrameworkCodeIgniter(最小的学习曲线,但没有那么强大)。

这应该让你开始使用你的代码......

<?php
class UserService {
  public $username = "*****";
  public $password = "*****";
  public $server = "localhost";
  public $port = "3306";
  public $databasename = "******";
  public $tablename = "users";
  public $connection;

  public function __construct () {
    $this->connection = mysqli_connect($this->server, $this->username, $this->password, $this   ->databasename, $this->port);

    $this->throwExceptionOnError($this->connection);
  }

  public function createUser($user) {
    $email = mysqli_real_escape_string($user->email);

    //Check to see if the user exists - we need this to be as precise as possible. People     often have the same name. But they won't have the same email.
    $sql = "SELECT * FROM users WHERE email = '{$email}'";
    $res = mysqli_query($sql);
    $cnt = mysqli_num_rows($res);

    if ($cnt > 0) {
      //Update the user or do whatever you need to do
    }
    else {
      //Create the user
    }
  }
}
于 2012-04-26T23:12:53.047 回答
0

我不知道mysqli是否支持这一点,但值得看看这里记录的“INSERT ... ON DUPLICATE KEY UPDATE”语法:http: //dev.mysql.com/doc/refman/5.0/en/insert -on-duplicate.html

它可以让您让数据库为您执行“它们是否已经存在”检查(使用电子邮件地址或某些等效字段作为主键,如其他答案中所建议的那样)。

于 2012-04-26T23:23:15.093 回答