创建新用户或更新(如果已存在)

时间:2012-04-26 22:49:23

标签: php flex

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

3 个答案:

答案 0 :(得分: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=?");
      ...
    }
    ...
}

答案 1 :(得分: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
    }
  }
}

答案 2 :(得分:0)

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

它可以让你让数据库做你的“他们已经存在”检查你(使用电子邮件地址或一些等效字段作为主键,如其他答案所示)。