PHP updating data in MySQL database

      No Comments on PHP updating data in MySQL database

PHP updating data in MySQL database -update command

Learn about SQL update command. Please refer to the post.

PHP updating data in MySQL database

Creating a form

  1. Form is enclosed in <form> </form> tags.
  2. Specify the method attribute as GET or POST depending on the data being sent to the php script
  3. Set the action attribute with the URL of the php script eg:request-handler.php
  4. NOTE: Each input/select/textarea field in the form should contain name attribute. Without name attribute the value submitted will not be sent to the script.
  5. A sample form

    <form method="POST" action="request-handler.php">
      <label>Enter your email:</label>
      <input type="email" name="email" placeholder="Enter Email" required/>
      <label>Enter new password:</label>
      <input type="password" name="password" placeholder="New Password" required/>
      <input type="submit" value="login"/>
    </form>
    

    Demo database:

    PHP updating data in MySQL database

    PHP updating data in MySQL database – request-handler.php

    Here we will use email value entered by the user to match it to the row where the column containing password.

    <?php
      if(isset($_POST['submit'])) //avoid error:undefined index
      {
          $email=$_POST['email']; //getting values from the form
          $password=$_POST['password']; 
      }
      // establishing a database connection
    
      define("HOST", "localhost");     // The host you want to connect to.
      define("USER", "user_db");    // The database username. 
      define("PASSWORD", "123$%bb^7aqq!2");    // The database password. 
      define("DATABASE", "sample_db");    // The database name.
      $connection = mysqli_connect(HOST, USER, PASSWORD, DATABASE);
    
      // Check connection
    
      if (mysqli_connect_errno())
       {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
       }
      // checking for empty variables
    
      if($email="" ||$password="")
      {
        echo "Please enter a valid value to each input";
      }
      else
      {
        //building a query
        $sql="UPDATE `student` SET `stdpassword`='$password' WHERE `emailid`='$email'";
        //running query
        $run=mysqli_query($connection,$sql);
        //success message
        if($run)
        { 
            echo "Database Updated successfully";
         }  
     } 
    ?>
    

    The update query

    This query updates the new information to the database. where `emailid`='$email'is used to match the row to the email of the user.This updates the required row.

     $sql="UPDATE `student` SET `stdpassword`='$password' WHERE `emailid`='$email'";
    

    How it is done:

    1. User fills the form with his/her email and the new password.
      PHP updating data in MySQL database
    2. On clicking the submit button ->request-handler.php script is called.
    3. It runs the update statement on the database.
       $sql="UPDATE `student` SET `stdpassword`='iamablog' WHERE `emailid`='scanfcode@scanfcode.com'";
    4. Rows are affectedPHP updating data in MySQL database
    5. Resulting database is:
      PHP updating data in MySQL database

Leave a Reply

Your email address will not be published. Required fields are marked *