Home > OS >  Hello I am trying to edit/update data from my database(sql on localhost)
Hello I am trying to edit/update data from my database(sql on localhost)

Time:10-22

Hello I am trying to edit/update data from my database(SQL on localhost) but when I click on update it updates the whole data in the table which is named by "list" code of editing data.php

<html>
    <head>
        <title>My first PHP Website</title>
    </head>

    <body>
    
    <h2 align="center">My list</h2>
    <table border="1px" width="100%">
        <tr>
            <th>Id</th>
            <th>YourName</th>
            <th>Email</th>
            <th>Dish</th>
            <th>People</th>
            <th>Reg_Date</th>
            <th>Edit</th>
            <th>Delete</th>
        </tr>
        <?php
//Database Connection        
           $con =mysqli_connect ('localhost' , 'root' , '' , 'table');
             if(!$con){
      echo "DataBase error";
   }
   else{
      print "DataBase Connected";
   } 
   echo "<br>";
   error_reporting();

//For select from database

            $sql = "Select * from list";
            $result = $con->query($sql);
// Fetch from database
         while($row = $result->fetch_assoc())
         {

            echo "<tr>
                <td> ".$row['id']."</td>
                <td> ".$row['YourName']."</td>
                <td> ".$row['Email']."</td>
                <td> ".$row['Dish']."</td>
                <td> ".$row['People']."</td>
                <td> ".$row['reg_date']."</td>
                <td><a href = 'edit.php?rn=$row[id]&fn=$row[YourName]&ln=$row[Email]&em=$row[Dish]&dm=$row[People]&rg=$row[reg_date]'>Edit</td>
                </tr>
            ";
         }


        ?>
    </table>
    </body>
</html>

in the table every row get its edit option when we click on it. another page open and code is here.

 <body>
        <h2>Edit Page</h2>
        <a href="Logoutforuserdata.php">Click here to go logout</a><br/><br/>
        <form action="edit.php" method="POST">
            id: <input type="number" name="id"> <br/>
            YourName: <input type="text" name="YourName"> <br/> 
            Email: <input type="email" name="Email"> <br/> 
            Dish: <input type="text" name="Dish"> <br/> 
            People: <input type="text" name="People"> <br/> <br/>

            <input type="submit" value="Update the list"/>
        </form>
      
   </body>


<?php
 if($_SERVER['REQUEST_METHOD'] == "POST")
    {

      $YourName = ($_POST["YourName"]);
      $Email = ($_POST["Email"]);
      $Dish = ($_POST["Dish"]);
      $People = ($_POST["People"]);

     $con =mysqli_connect ('localhost' , 'root' , '' , 'table');
   if(!$con){
      echo "DataBase error";
   }
   else{
      print "DataBase Connected";
   } 
   echo "<br>";
$sql1 = "UPDATE list SET YourName='$YourName' , Email='$Email' , Dish='$Dish' ,People='$People'";

if ($con->query($sql1) === TRUE) {
  echo "Record updated successfully";
} else {
  echo "Error updating record: " . $con->error;
}
    $con->close();
 }
?> 

When I click on the button which updates the list all the data in the list is updated with the given one. I want that data on only one line. Please help me with it.

CodePudding user response:

You forgot to add the WHERE condition. Specify which row is to be updated. See the updated code:

$sql1 = "UPDATE list SET YourName='$YourName' , Email='$Email' , Dish='$Dish' ,People='$People' WHERE id='$id'";

CodePudding user response:

You need to add a WHERE constraint to your query:

$id = ($_POST["id"]);

$sql1 = "UPDATE list SET YourName='$YourName' , Email='$Email' , Dish='$Dish' ,People='$People' WHERE id = '$id'";
  •  Tags:  
  • php
  • Related