0
我正在檢查記錄是否存在,如果尚未存在則創建新記錄,否則進行更新。 SQL在PHPMyAdmin中工作正常,但不能通過PHP頁面。嘗試更新時使用PHP時出現重複輸入錯誤
$check_for_id = mysql_query("SELECT id FROM Members WHERE ID = '$id'");
if(mysql_num_rows($check_for_id) == 0) {
// Not found, add a new record
$sql="INSERT INTO Members (ID, Title, FirstName, LastName) VALUES ('$id', '$title', '$firstname', '$lastname')";
$response=" Record Added";
} else {
// Member is already in the database, so let's just update the info
$sql="UPDATE Members
SET ID='$id', Title='$title', FirstName='$firstname', LastName='$lastname'
WHERE ID='$id'";
$response=" Record Updated";
}
if (!mysqli_query($dbconnect,$sql)) {
die('Error: ' . mysqli_error($dbconnect));
}
echo mysqli_affected_rows($dbconnect) . $response;