Like Us Like Us Facebook Subscribe Subscribe us YouTube Whatsapp Share us Whatsapp Query Send your queries

Submitting multiple form fields using a PHP function

Submitting multiple form fields using a PHP function

Suppose if you have form with 25 fields and have to save into a database table, then we have to get values of each field, escaping its value to protect SQL Injection , so you have some lengthy code .

For example when submitting a Form :

<?php isset($_POST[‘submit’){

$name =$_POST[‘name’];$mobile =$_POST[‘mobile’];//and so on

}?>

In this Tutorial I am going to tell how it can so easy and short creating a php function.

First create table company using phpmyadmin its so simple, then create a table staffs under database company.

Staff table Fields you can get from form’s name, remember name of field of database and form name should be equal otherwise function save will not work.

 

Full Source Code –

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8">
<!--add here bootstrap cdn link-->
<title>ADD</title>
</head><body>
<?php 
$db= new mysqli('localhost','root','','company');
if(isset($_POST['addstaff'])){
         
         $fields = $_POST;
         unset($fields['addstaff']);
         save($db,$fields);
}
function save($db,$fields,$table){
           $fname="";
           $fvalue="";
           foreach($fields as $name=>$value){
                $value=$db->real_escape_string($value);
                $fvalue.= "'".$value."',";
                $fname.= $name.",";
               //echo $name." => ".$value."<br/>";
           }
           $fname=substr_replace($fname, '', -1);
           $fvalue=substr_replace($fvalue, '', -1);
           $sql="insert into $table($fname) values($fvalue)";
           //echo $sql;

           if($db->query($sql)){
             echo '<div class="alert alert-success">Successfully Saved</div>';  
           }else{
             echo '<div class="alert alert-danger">'.$db->error.'</div>';
           }

  }
?>
  <form id="add" action="" method="post">
	<div class="col-md-12">
     <div class="col-md-12">
       <div class="pull-right"><button type="submit" name="addstaff" class="btn btn-success">Submit</button></div>
     </div>
     <div class="col-md-12">
            </div>
     <div class="col-md-6">
         <div class="col-md-12">
           <label for="">Staff Name *</label>
           <input type="text" name="name" placeholder="Staff Name" class="form-control" required>
         </div>
         <div class="col-md-6">
           <label for="">Contact No. *</label>
           <input type="text" name="mobile" placeholder="10 digit contact number" class="form-control" required>
         </div>
         <div class="col-md-6">
           <label for="">Email ID *</label>
           <input type="email" name="email" placeholder="Email ID" class="form-control" required>
         </div>
         <div class="col-md-6">
           <label for="">Department *</label>
           <select name="department" id="department" class="depart form-control">
             <option value="">Select Department</option>
             <option value="dev">Web / Software Development</option>
             <option value="ani">Animation</option>
             <option value="net">Networking / Hardware</option>
           </select>
         </div>
         <div class="col-md-6">
           <label for="">Employee Type *</label>
           <select name="employee_type" id="employee_type" class="depart form-control">
             <option value="">Select </option>
             <option value="Regular">Regular</option>
             <option value="Freelancer">Freelancer</option>
           </select>
         </div>
         <div class="col-md-6">
           <label for="">Salary *</label>
           <input type="text" name="salary" placeholder="Salary" class="form-control" required>
         </div>
         <div class="col-md-6">
           <label for="">DOB *</label>
           <input type="text" name="dob" placeholder="YYYY-MM-DD" class="datepicker form-control" required>
         </div>
         <div class="col-md-6">
           <label for="">DOJ (Date of Joining)*</label>
           <input type="text" name="doj" placeholder="YYYY-MM-DD" class="datepicker form-control" required>
         </div>

        
     </div> 

     <div class="col-md-6">
        <div class="col-md-6">
           <label for="">Gender *</label>
           <select name="gender" id="gender" class="form-control">
             <option value="">Select </option>
             <option value="Male">Male</option>
             <option value="Female">Female</option>
           </select>
         </div>
         <div class="col-md-6">
           <label for="">Marital Status *</label>
           <select name="marital_status" id="marital_status" class="form-control">
             <option value="">Select </option>
             <option value="Married">Married</option>
             <option value="Unmarried">Unmarried</option>
           </select>
         </div>
         <div class="col-md-12">
           <label for="">Address*</label>
           <textarea name="address" id="address" class="form-control" placeholder="Complete Address" required></textarea>
         </div>
     </div>
     <div class="col-md-12">
       <br/><br/><br/>
     </div>
  </div>
  </form>
</body>
</html>

 

 

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments