$sql = "INSERT INTO myNotes ('id','date','notes') VALUES ('$id','$date','$notes')";
我想使用php将信息插入到mysql数据库中但同时我想检查日期和id是否已经存在然后更新注释
对于那些搜索解决方案的人我找到了
$sql = "SELECT notes FROM myNotes WHERE id='$id' AND date='$date'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$sql = "UPDATE myNotes SET notes=$notes WHERE id='$id' AND date='$date'";
if ($conn->query($sql)) {
echo 'successfully updated';
}
else {
echo 'failed';}
};}
else {
$sql = "INSERT INTO myNotes (id,date,notes) VALUES ('$id','$date','$note')";
if ($conn->query($sql)) {
echo 'added successfully';
}
else {
echo 'failed to add';
};}
答案 0 :(得分:0)
您的查询应该是:
$sql = "INSERT INTO myNotes ('id','date','notes') VALUES ('$id','$date','$notes') ON DUPLICATE KEY UPDATE notes='updatednotes', date='updateddate'";