Simplest PHP code to insert a new record

image

<html>
<head><title>Insert A New Record</title></head>
<body>
Insert A New Record<br>
<form action="" method="get">
 Employee ID
 <input type="text" name="empno"><br>
 Firstname
 <input type="text" name="firstname"><br>
 Lastname
 <input type="text" name="lastname"><br>
 Department Code
 <input type="text" name="workdept"><br>
 <input type="submit" value="Save Record"><br>
</form>
 
<?php
//extract content of the form
    $empno=$_GET['empno'];
    $firstname=$_GET['firstname'];
    $lastname=$_GET['lastname'];
    $workdept=$_GET['workdept'];
    
    if($empno!=NULL){
        //establish database connection
        //to create the bridge
        $db=mysqli_connect("localhost","root","","mycompanyhr");
 
        //connection failed
        if($db==false){//connection error
            echo("Connection failed : ". mysqli_connect_error($db));
            exit();
        }
        //u are connected to db
        else{
            echo("Connected to database <hr>");
        }
        
        //sql command to insert new record
        $sql="INSERT INTO employee 
        (EMPNO, FIRSTNAME, LASTNAME, WORKDEPT)
        values ('$empno','$firstname','$lastname','$workdept')";
        
        //execute the sql command
        $qr=mysqli_query($db, $sql);
        //if there's any SQL error
        if($qr==false){
            echo ("Query cannot be executed!<br>");
            echo ("SQL Error : ".mysqli_error($db));
        }
        //no SQL error
        else{
            echo ("The record for $empno is stored!<br>");
        }    
    }
?>
</body>

Popular Posts

WorldConferences.net