2012-04-26 27 views
-1

私は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); 
} 
+0

コードに一貫しているのか、時間を無駄にするのですか? 1分ほど前に、あなたはこれをクラス形式で持っていました。これで個々の関数を作成しています。それはどちらになるのですか? – pthurmond

+0

また、なぜ疑問符に変数を設定していないのですか?我々はあなたのためにすべてを書くことは期待できません。 – pthurmond

+1

あなたのデータベーステーブルはどのように見えますか? – pthurmond

答えて

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=?"); 
     ... 
    } 
    ... 
} 
+0

私が見たコード例では、他に必要なコードはありませんでした。私はそれが{}の外にあったものはすべて、それがもう一つのものだと思った。 – user1077544

+0

この例で 'return'ステートメントを(またはまれに' throw')探す。 –

0

あなたには不要なコードがたくさんあります。また、ユーザーの電子メールアドレスなど、データベースによって自動的に生成されない一意の値で検索することも検討してください。

また、db接続を閉じることについても心配しないでください。それは事をもっと簡単にするでしょう。

私は、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 
    } 
    } 
} 
+0

提案はありがたいですが、これは典型的な登録ではありません。 Facebookのアプリで、ユーザーがFacebookにログインしたときに取得したユーザーID、名前、性別、DOBを使用しています。 IDはデータベースによって生成されません。 – user1077544

+0

ああ、メールのことは分かりません。それは結構です。あなたが持っているパラメータを使うだけで、複数のものではなく一意のユーザを見つけることができます。フレームワークは依然として非常に役立つでしょう。私はZendがFacebookで動作するためのいくつかのプラグインがあると信じています。それはあなたのために重い持ち上げを行い、開発をスピードアップし、あなたの欲求不満を解消します。 – pthurmond

0

mysqliのがこれをサポートしている場合、私は知りませんが、それは「DUPLICATE KEY UPDATE INSERT ... ON」を見て価値がある構文などhttp://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

これは、あなたがあなたのためにあなたのために(すでに電子メールアドレスまたは他の回答で示唆されているように、主キーと同等のフィールドを使用して)チェックするDBを取得することができます。

関連する問題