- Get link
- X
- Other Apps
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));
// 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
Post a Comment