Home > Back-end >  Form submission failed to update database record
Form submission failed to update database record

Time:12-29

Hi I am trying to update data in a database from a form but when I click save the data is not updated. It should be updating data in the database, which is not working. I already tried a few tutorial but still not working. Here is my code:

 <?php
$conn = mysqli_connect ('localhost','root','','bookstore_db');
 $currentuser = $_SESSION['username'];   
 $select = "SELECT * FROM user WHERE user_name='$currentuser'";
 $run = mysqli_query($conn,$select);
 $row = mysqli_fetch_array($run);
     $user_name = $row['user_name'];
     $user_email = $row['user_email'];
     $user_password = $row['user_password'];
?>
 <div >
   <!-- left column -->
   <div >
     <div >
       <div >
         <img src="image/image.jpg" id="image">
         <input type="file" id="file">
         <label for="file" id="uploadBtn">Choose Photo</label>
       </div>
       <script src="app.js"></script>
     </div>
   </div>
   

   <!-- edit form column -->
   <div >
     <h3><b>Personal info</b></h3>
     
     <form  role="form">
       <div >
         <label >Username:</label>
         <div >
           <input  type="text" value="<?php echo $row['user_name']; ?>">
         </div>
       </div>
       <div >
         <label >Email:</label>
         <div >
           <input  type="text" value="<?php echo $row['user_email']; ?>">
         </div>
       </div>
       <div >
         <label >Password:</label>
         <div >
           <input  type="text" value="<?php echo $row['user_password']; ?>">
         </div>
       </div>
       <center><input type="submit" name="submit"  value="Save Changes"></center> 
   </div>
     </form>
   </div>
</div>
</div>
<hr>

<?php
$conn = mysqli_connect ('localhost','root','','bookstore_db');

if (isset($_POST['submit'])){
 
 $euser_name = $_POST['user_name'];
 $euser_email = $_POST['user_email'];
 $euser_password = $_POST['user_password'];
 
 $update = "UPDATE user SET user_name='$euser_name',user_email='$euser_email',user_password='$euser_password' WHERE user_name='$currentuser'";
 
 $run_update = mysqli_query($conn,$update);
 if($run_update === true)
 {
     echo '<script type="text/javascript"> alert("Data Updated") </script>';
 }
     else
 {
     echo '<script type="text/javascript"> alert("Data Not Updated") </script>';
 }
}



?>

What could the missing code be, or did I overlook something here?

CodePudding user response:

There might be multiple issues. I'm here to point out one of them.

Your <form> element has no method attribute. By default, forms are submitted with the GET method, but your PHP are reading results from the $_POST superglobal variable that should be submitted by POST method. So when your form is submitted, you will get only an empty $_POST array. Thus the database update will make the user fields all empty.

To fix this, simply change this line:

<form  role="form">

to this:

<form  role="form" method="POST">

P.S. Your way to do query opens to SQL injection attacks. Instead of using mysqli_query on a constructed string, using a combination of mysqli_prepare, mysqli_stmt_bind_param and mysqli_stmt_execute would be much safer.

CodePudding user response:

Yes, you missed something, your input doesn't have a name, as you do on the submit input. so the PHP won't recognize your POST

=> name="user_name"

<input  name="user_name" type="text" value="<?php echo $row['user_name']; ?>">
  •  Tags:  
  • php
  • Related