phpmysql-insert-id

question about execute PHP file, from other php file on the same folder


i download 2 php file, one is index. php for input form, and other is insert.php. i have put them on the same folder. when i click submit it execute insert.php file it work fine and it insert form data into my database. but the question is: how it is called? no code line on the index.php calling the insert.php file.

here is a code of index.php

<!DOCTYPE html>
<html lang="en">
   <head>
      <title>GFG- Store Data</title>
   </head>
   <body>
      <center>
         <h1>Storing Form data in Database</h1>
         <form action="insert.php" method="post">          
<p>
               <label for="firstName">Name:</label>
               <input type="text" name="name" id="name">
            </p>             
<p>
               <label for="lastName">Branch:</label>
               <input type="text" name="branch" id="branch">
            </p>             
<p>
               <label for="Gender">Roll Number:</label>
               <input type="text" name="roll_no" id="roll_no">
            </p>
  
            <input type="submit" value="Submit">
         </form>
      </center>
   </body>
</html>

here is the code of insert.php

<!DOCTYPE html>
<html>
 
<head>
    <title>TEST</title>
</head>
 
<body>
    <center>
        <?php
 
        $conn = mysqli_connect("localhost", "root", "", "test");
         
        // Check connection
        if($conn === false){
            die("ERROR: Could not connect. "
                . mysqli_connect_error());
        }
         
        // Taking all 3 values from the form data(input)
        $name =  $_REQUEST['name'];
        $branch = $_REQUEST['branch'];
        $roll_no =  $_REQUEST['roll_no'];
                 
        // Performing insert query execution
        // here our table name is college
        $sql = "INSERT INTO student  VALUES ('$name','$branch','$roll_no')";
         
        if(mysqli_query($conn, $sql)){
            echo "<h3>data stored in a database successfully."
                . " Please browse your localhost php my admin"
                . " to view the updated data</h3>";
          } 
        else{
            echo "ERROR: Hush! Sorry $sql. "
                . mysqli_error($conn);
        }
         
        // Close connection
        mysqli_close($conn);
        ?>
    </center>
</body>
 
</html>


Solution

  • insert.php is calling after clicking on submit button by "action" attribute which you have used in tag.

    <form action="insert.php" method="post">
    

    You can set another path as well as if you want to keep insert.php on any other folder location.