-2
$sql = "INSERT INTO myNotes ('id','date','notes') VALUES ('$id','$date','$notes')";

I want to insert the informations bellow using php into mysql database but at the same time i want to check if the date and id already exists then update the notes

For those who search for the solution I found it

$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';
 };}

1 Answers1

0

Your query should be:

$sql = "INSERT INTO myNotes ('id','date','notes') VALUES ('$id','$date','$notes') ON DUPLICATE KEY UPDATE notes='updatednotes', date='updateddate'";
mehulmpt
  • 15,861
  • 12
  • 48
  • 88