我正在使用下面的代码,当 user_id 没有设置为唯一时,它使用相同的 user_id 输入了两次相同的数据。所以我把它设置为唯一的,现在只是得到错误,查询失败。非常感谢所有帮助:(
if (empty($err)) {
$thesis_Name = mysql_real_escape_string($_POST['thesis_Name']);
$abstract = mysql_real_escape_string($_POST['abstract']);
// insert into the database
$the_query ="SELECT * FROM thesis WHERE user_id='$_SESSION[user_id]'";
$testResult = mysql_query($the_query) or die('Error, query failed');
if(mysql_fetch_array($testResult) == NULL){
//insert...
$the_query ="INSERT INTO thesis (`user_id`,`thesis_Name`,`abstract`)
VALUES ($user_id, $thesis_Name, $abstract)";
$result = mysql_query($the_query) or die('Error, query failed') ;
}
else{
//update...
$the_query = "UPDATE thesis
SET thesis_Name='$thesis_Name', abstract='$abstract'
WHERE user_id='$_SESSION[user_id]'";
$result = mysql_query($the_query)or die('Error, query failed');
}
// query is ok?
if (mysql_query($the_query, $link) ){
// redirect to user profile
header('Location: myaccount.php?id=' . $user_id);
}
我也试过
$the_query = "INSERT INTO thesis (`user_id`,`thesis_Name`,`abstract`)
VALUES ($user_id, $thesis_Name, $abstract)
ON DUPLICATE KEY UPDATE thesis_Name=VALUES(thesis_Name), abstract=VALUES(abstract)";