if (isset($_POST["sendComments"])) {
$whichProjectToSave= mysqli_real_escape_string($db5, $_POST['whichProjectToSave']);
$commentsToPHP= mysqli_real_escape_string($db5, $_POST['commentsToPHP']);
$objectName= mysqli_real_escape_string($db5, $_POST['objectName']);
$sql="INSERT INTO objectsCommentsTable (objectsCommentsText,projectName,objectName) VALUES('$commentsToPHP','$whichProjectToSave','$objectName')";
// $sql="UPDATE objectsCommentsTable SET objectsCommentsText ='$commentsToPHP' WHERE projectName= '$whichProjectToSave' AND objectName = '$objectName' ";
mysqli_query($db5,$sql);
}
如果數據存在,我必須更新。但我只知道如何使用上面的方法。 其實我只能在表中插入數據,然後通過更改代碼進行更新。但我想用下面的代碼中的東西。它如何執行?如何更新數據庫表中是否存在數據
IF EXISTS (SELECT objectsCommentsText FROM objectsCommentsTable WHERE objectName='$objectName' AND projectName = '$projectName')
UPDATE objectsCommentsTable
SET objectsCommentsText ='$commentsToPHP'
WHERE projectName= '$whichProjectToSave' AND objectName = '$objectName'
ELSE
INSERT INTO objectsCommentsTable (objectsCommentsText,projectName,objectName)
VALUES('$commentsToPHP','$whichProjectToSave','$objectName')
之前問的問題,請搜索一下:https://stackoverflow.com/questions/15383852/sql-if-exists-update -else-insert-into – aidinMC