Skip to main content

Simple Function to Insert data into MySql database using PHP


The below simple php function is used to insert data into MySql database with out manually filling the insert mysql query with input filed names.

Function:
function insert_Defined($table, $array,$ins=''){ 
// insert('table', array_map('trim',$_POST));
    global $db;

    $query = "INSERT INTO" . ' ' . $table;
    $fis = array(); $vas = array();
   
    foreach ($array as $field => $val) {
        $fis[] = "`$field`";
        $vas[] = ($val != "NOW()") ? "'" . mysqli_real_escape_string($db,$val) . "'" : "NOW()";
    }
   
    $query .= "(" . implode(",", $fis) . ") VALUES (" . implode(",", $vas) . ")";
    //echo $query;exit;
    Query($query);if(!empty($ins)){$ins_id=mysqli_insert_id($db);return $ins_id;}
}


Form:
<form action="" method="post" name="designation" id="designation">
  <span class="label">Department Name <span class="red">*</span></span>
  <select name="desg_dept_id" id="desg_dept_id" placeholder="Department Name" style="width: 310px" required>
    <option value="">Select Department Name</option>
    <option value="1">Dept 1</option>
    <option value="2">Dept 2</option>
    <option value="3">Dept 3</option>
  </select>
  <br>
  <span class="label">Designation Name <span class="red">*</span></span>
  <input name="desg_name" id="desg_name" type="text" value="" required>
  <br>
  <span class="label">Designation Abbreviation <span class="red">*</span></span>
  <input name="desg_abbreviation" id="desg_abbreviation" type="text" value="" required>
  <br>
  <span class="label">Description</span>
  <textarea name="desg_description" id="desg_description" value="" ></textarea><br>
  <input name="" type="submit" value="Submit" class="button-green">
  <input name="reset" type="reset" value="Cancel" class="button-orange ml10" onClick="history.back()">
</form>


Function Calling for Insertion data into MySql.

insert_Defined('designations', array_map('trim',$_POST));

Note: 
The $_POST is a array. The names of posted form input fields need to match with database "designations" table column names.

Comments

Popular posts from this blog

How to create a Barcode Using PHP Barcode 128 Generator

A barcode is an optical, machine-readable, representation of data, the data usually describes something about the object that carries the barcode.We will use PHP to generate Barcode in this tutorial. In this script, we are using coding which will generate barcodes in barcode format Code 128 . First, we will create index.php which will ask for the user input for which Barcode has to be created PHP Barcode Generator <fieldset><legend>Detail Informations</legend><form action="createbarcode.php" method="post"><b>Enter Your Code </b><input name="barcode" type="text" /><input type="submit" value="Create Barcode" /></form></fieldset> Now we will create createbarcode.php which will call function from Barcode code128 class for creating barcode <? php include('barcode128.php'); // include php barcode 128 class // design our barcode display echo

Document Expired or Webpage has expired on back button

If you have a dinamic website and want to allow your visitors to use the back button after they sent a form with the post method, the best combination I found was: <?php header("Expires: Sat, 01 Jan 2000 00:00:00 GMT"); header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT"); header("Cache-Control: post-check=0, pre-check=0",false); session_cache_limiter("must-revalidate"); // and after you start the sessionsession_start(); ?> I try some combinations using header("Cache-Control: no-cache, must-revalidate"), but when clicking the back button, the last changes in the form back to their previous states. The combination above works fine with IE 6.x. I didn't test this with other browsers. When I try something like session_cache_limiter("nocache, must-revalidate") it doesn't work. The page only updates when I used the browser's refresh button. In dynamic web sites this is not g

How to Enable IMAP PHP in xampp

The imap extension comes as standard with the PHP installation. You just need to enable it in your php.ini   Enable IMAP in XAMPP: You need to configure your php.ini file to enable IMAP extension Search for the line ;extension=php_imap.dll and remove semicolon(;) and restart your xampp. The line should look like as mentioned below. extension=php_imap.dll Note : New php version does not have dll anymore. The default php.ini should already contain a line to load the extension but commented out: ;extension=imap Remove semicolon from above like below extension=imap Enable IMAP in Linux: If you are using LAMP server, First install IMAP using the command on terminal $ sudo apt-get install php5-imap To enable IMAP, run the following command. sudo phpenmod imap Restart apache server with below command sudo service apache2 restart