0

我有一个个人资料表格,用户只保留一份记录,并且可以随时更新他们的信息。首先,我可以使用 mysql update 来更新一个空表吗?我正在尝试设置提交以执行两个功能。插入或更新,取决于记录是否存在:

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

从哪里来$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 回答
0

不,您不能更新空表,您必须有一条要更新的记录。

您对基本 mysql 查询有另一个抽象级别,因此除非您发布插入/更新函数的功能,否则我们无法真正帮助您。

无论如何只需检查记录是否存在,如果存在,则更新记录,如果不插入。

于 2012-10-21T13:22:46.980 回答