插入其他更新

时间:2012-04-27 19:11:44

标签: php mysql

我希望表单更新,如果它们是特定user_ID的行,但如果不是则插入。我尝试使用插入...重复密钥更新,但失败了,所以这是我的下一次尝试。 user_ID不是唯一索引,但我的表中的“ID”是一个自动增量字段。 User_ID只是会话所基于的索引。 这是我现在使用的代码:

if (empty($err)) {

        $thesis_Name = mysql_real_escape_string($_POST['thesis_Name']);
        $abstract = mysql_real_escape_string($_POST['abstract']);


$query="UPDATE thesis SET (`thesis_Name`='$thesis_Name',
`abstract`='$abstract') WHERE id='$_SESSION[user_id]'
IF ROW_COUNT()=0
REPLACE INTO  thesis (theis_Name,abstract)VALUES ('$thesis_Name', '$abstract')
";

mysql_query($query) or die();
// query is ok?

if (mysql_query($the_query, $link) ){

 // redirect to user profile
   header('Location: myaccount.php?id=' . $user_id);
   }

在按提交时,我得到一个空白页。

3 个答案:

答案 0 :(得分:2)

您是否尝试过“替换入...”而不是“插入...”

答案 1 :(得分:0)

REPLACE就是你想要的。您需要在UNIQUE KEY上定义id(如果它不是PRIMARY KEY,这是唯一的定义)。

答案 2 :(得分:0)

REPLACE INTO就是答案。如果您仍希望使用代码,则在查询中缺少分号:

$query="UPDATE thesis SET (`thesis_Name`='$thesis_Name',
`abstract`='$abstract') WHERE id='$_SESSION[user_id]';
IF ROW_COUNT()=0
INSERT INTO  thesis (theis_Name,abstract)VALUES ($thesis_Name, $abstract)
";