0

ユーザーがこれの 1 つのレコードのみを保持し、いつでも自分の情報を更新できるプロファイル フォームがあります。まず、mysql update を使用して空のテーブルを更新できますか? 2 つの機能を実行するように送信をセットアップしようとしています。レコードが存在するかどうかに応じて、挿入または更新します。

if(isset($_POST['update']) && $profile['user_id'] == ""){
    DB::getInstance()->insert_profile(
        $_POST['user_id'], 
            $_POST['fname'], 
        $_POST['mname'], 
        $_POST['lname'], 
        $_POST['profilepic'], 
        $_POST['location'],
        $_POST['tag'], 
        $_POST['email'],
        $_POST['confemail'],
        $_POST['newsletter'], 
        $_POST['height'], 
        $_POST['build'],
        $_POST['eyecolour'], 
        $_POST['haircolour'], 
        $_POST['race'], 
        $_POST['nationality'], 
        $_POST['languages'],
        $_POST['playingage'], 
        $_POST['accents'], 
        $_POST['aname'], 
        $_POST['aagency'], 
        $_POST['atelephone'],
        $_POST['aemail'], 
        $_POST['aweb'], 
        $_POST['facebook'], 
        $_POST['twitter'], 
        $_POST['spotlight'],    
        $_POST['castingcall']);
}

else if (isset($_POST['update']) && $profile['user_id'] != ""){
hcDB::getInstance()->update_profile(
        $_POST['user_id'], 
        $_POST['fname'], 
        $_POST['mname'], 
        $_POST['lname'], 
        $_POST['profilepic'], 
        $_POST['location'],
        $_POST['tag'], 
        $_POST['email'],
        $_POST['confemail'], 
        $_POST['newsletter'], 
        $_POST['height'], 
        $_POST['build'],
        $_POST['eyecolour'], 
        $_POST['haircolour'], 
        $_POST['race'], 
        $_POST['nationality'], 
        $_POST['languages'],
        $_POST['playingage'], 
        $_POST['accents'], 
        $_POST['aname'], 
        $_POST['aagency'], 
        $_POST['atelephone'],
        $_POST['aemail'], 
        $_POST['aweb'], 
        $_POST['facebook'], 
        $_POST['twitter'], 
        $_POST['spotlight'],    
        $_POST['castingcall']);
}

これは機能せず、エラーも発生しません。どんなアイデアでも大歓迎です。

申し訳ありませんが、私の挿入および更新機能は次のとおりです。

public function update_profile (
$userID,$fname, $mname, $lname, $profilepic, $location, $tag, $email, $confemail, $newsletter,
$height, $build, $eyecolour, $haircolour, $race, $nationality, $languages,
$playingage, $accents, $aname, $aagency, $atelephone, $aemail, $aweb,
$facebook, $twitter, $spotlight, $castingcall){
                $fname = $this->real_escape_string($fname);
                $mname = $this->real_escape_string($mname);
                $lname = $this->real_escape_string($lname);
                $profilepic = $this->real_escape_string($profilepic);
                $location = $this->real_escape_string($location);
                $tag = $this->real_escape_string($tag);
                $email = $this->real_escape_string($email);
                $confemail = $this->real_escape_string($confemail);
                $newsletter = $this->real_escape_string($newsletter);
                $height = $this->real_escape_string($height);
                $build = $this->real_escape_string($build);
                $eyecolour = $this->real_escape_string($eyecolour);
                $haircolour = $this->real_escape_string($haircolour);
                $race = $this->real_escape_string($race);
                $nationality = $this->real_escape_string($nationality);
                $languages = $this->real_escape_string($languages);
                $playingage = $this->real_escape_string($playingage);
                $accents = $this->real_escape_string($accents);
                $aname = $this->real_escape_string($aname);
                $aagency = $this->real_escape_string($aagency);
                $atelephone = $this->real_escape_string($atelephone);
                $aemail = $this->real_escape_string($aemail);
                $aweb = $this->real_escape_string($aweb);
                $facebook = $this->real_escape_string($facebook);
                $twitter = $this->real_escape_string($twitter);
                $spotlight = $this->real_escape_string($spotlight);
                $castingcall = $this->real_escape_string($castingcall);

                $this->query("UPDATE profile SET 
                    fname = '" . $fname . "', mname = '" . $mname . "', 
                    lname = '" . $lname . "', profilepic = '" . $profilepic . "',
                    location = '" . $location . "', tag = '" . $tag . "', 
                    email = '" . $email . "', confemail = '" . $confemail . "', newsletter = '" . $newsletter . "',
                    height = '" . $height . "', build = '" . $build . "', 
                    eyecolour = '" . $eyecolour . "', haircolour = '" . $haircolour . "',
                    race = '" . $race . "', nationality = '" . $nationality . "', 
                    languages = '" . $languages . "', playingage = '" . $playingage . "',
                    accents = '" . $accents . "', aname = '" . $aname . "', 
                    aagency = '" . $aagency . "', atelephone = '" . $atelephone . "', 
                    aemail = '" . $aemail . "', aweb = '" . $aweb . "', 
                    facebook = '" . $facebook . "', twitter = '" . $twitter . "',
                    spotlight = '" . $spotlight . "', castingcall = '" . $castingcall. "', WHERE user_id = " . $userID or die(mysql_error()));
            }



public function insert_profile(
$fname, $mname, $lname, $profilepic, $location, $tag, $email, $confemail, $newsletter,
$height, $build, $eyecolour, $haircolour, $race, $nationality, $languages,
$playingage, $accents, $aname, $aagency, $atelephone, $aemail, $aweb,
$facebook, $twitter, $spotlight, $castingcall){
                $fname = $this->real_escape_string($fname);
                $mname = $this->real_escape_string($mname);
                $lname = $this->real_escape_string($lname);
                $profilepic = $this->real_escape_string($profilepic);
                $location = $this->real_escape_string($location);
                $tag = $this->real_escape_string($tag);
                $email = $this->real_escape_string($email);
                $confemail = $this->real_escape_string($confemail);
                $newsletter = $this->real_escape_string($newsletter);
                $height = $this->real_escape_string($height);
                $build = $this->real_escape_string($build);
                $eyecolour = $this->real_escape_string($eyecolour);
                $haircolour = $this->real_escape_string($haircolour);
                $race = $this->real_escape_string($race);
                $nationality = $this->real_escape_string($nationality);
                $languages = $this->real_escape_string($languages);
                $playingage = $this->real_escape_string($playingage);
                $accents = $this->real_escape_string($accents);
                $aname = $this->real_escape_string($aname);
                $aagency = $this->real_escape_string($aagency);
                $atelephone = $this->real_escape_string($atelephone);
                $aemail = $this->real_escape_string($aemail);
                $aweb = $this->real_escape_string($aweb);
                $facebook = $this->real_escape_string($facebook);
                $twitter = $this->real_escape_string($twitter);
                $spotlight = $this->real_escape_string($spotlight);
                $castingcall = $this->real_escape_string($castingcall);

                $this->query("INSERT INTO profile(
                    `fname`, `mname`, `lname`, `profilepic`, `location`, 
                    `tag`, `email`, `confemail`, `newsletter`, `height`, `build`, 
                    `eyecolour`, `haircolour`, `race`, `nationality`, 
                    `languages`, `playingage`, `accents`, `aname`, 
                    `aagency`, `atelephone`, `aemail`, `aweb`, 
                    `facebook`, `twitter`, `spotlight`, `castingcall`
                    )" . " VALUES 
                    '" . $fname . "', 
                    '" . $mname . "', 
                    '" . $lname . "', 
                    '" . $profilepic . "',
                    '" . $location . "', 
                    '" . $tag . "', 
                    '" . $email . "',
                    '" . $confemail . "', 
                    '" . $newsletter . "',
                    '" . $height . "', 
                    '" . $build . "', 
                    '" . $eyecolour . "', 
                    '" . $haircolour . "',
                    '" . $race . "',
                    '" . $nationality . "', 
                    '" . $languages . "', 
                    '" . $playingage . "',
                    '" . $accents . "', 
                    '" . $aname . "', 
                    '" . $aagency . "',
                    '" . $atelephone . "', 
                    '" . $aemail . "',
                    '" . $aweb . "', 
                    '" . $facebook . "', 
                    '" . $twitter . "',
                    '" . $spotlight . "',
                    '" . $castingcall. "')");
            }

そして私の形…

<form id="profile" action="{$HTTP_HOST}admin/profile.php" method="post" autocomplete="off">

そしてボタン:

<button class="submit" type="submit" value="update" name="update">Update</button>

ありがとう。

4

2 に答える 2

0

いいえ、空のテーブルを更新することはできません。更新するレコードが必要です。

基本的な mysql クエリに別のレベルの抽象化があるため、挿入/更新関数が何をするかを投稿しない限り、私たちは本当にあなたを助けることができません.

とにかく、レコードが存在するかどうかを確認し、存在する場合はレコードを更新し、挿入されていない場合はレコードを更新します。

于 2012-10-21T13:22:46.980 に答える
0

どこ$profile['user_id']から来たの?事前にこの値を持っている場合は、ユーザーが存在するかどうかを既に知っている必要があるため、それに応じて挿入または更新を呼び出します。

$profile['user_id']正しく定義されている可能性があり、そのようなユーザーがテーブルに存在しないと思われる場合は、アプリケーションの他の場所に論理的な問題がある可能性があります。簡単なクエリを使用して、そのユーザーが存在するかどうかをいつでも確認できます。

"SELECT user_id 
    FROM your_user_table 
    WHERE user_id = '" . (int)$profile['user_id'] . "'"
于 2012-10-21T13:48:32.753 に答える