Home > OS >  How can I update single field of an item in database using PHP and MySQL
How can I update single field of an item in database using PHP and MySQL

Time:07-30

I am building a to-do list using PHP and MySQL as a beginner project and I am trying to add the ability to post status updates to individual items in the list to a column in the database called 'notes'. I have been unable to figure out how to specify in the update query, the specific item in the list that I am trying to update. The query I have updates the 'notes' column for all of the items in the database. Can someone please help with this?

<?php 
            if (isset($_POST['notes'])) {                                   
                $input = $_POST['notes'];
                $id = $row['id'];

            $sql = "UPDATE tasks SET notes ='$input' WHERE id = ($id)";  
            if ($conn->query($sql) === TRUE) {
                echo "New record created successfully";
              } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
              } } ?>

enter image description here

CodePudding user response:

When you send what to update in the notes column, you have also sent that particular row id, using this id you can update that particular record in the table

For this, you have store this id in the hidden field in your form tag

            $id = $_POST['id'];  **// Changes here** 
  • Related