I have complete it !!!
I have a headache!
<?php
require_once("connection.php");
//verifie the fields on table
$result = $conn->query('DESCRIBE `table`');
$fields = array();
while ( $row = $result->fetch_assoc() ) {
$fields[] = $row['Field'];
}
//var_dump($fields);
// verify type of the fields integer, varchar, text, etc ... result in one code number... the codes are in post... after construct the bind_types array
$bind_types=array();
$query = "SELECT * from table";
if($result = $conn->query($query)){
// Get field information for all columns
while ($column_info = $result->fetch_field()){
if($column_info->type <=3){
// echo $column_info->type ."inteiro" ."<br>";
$bind_type="i";
$bind_types[]=$bind_type;
}
if($column_info->type ==5){
// echo $column_info->type ."double" ."<br>";
$bind_type="d";
$bind_types[]=$bind_type;
}
if($column_info->type >=7 && $column_info->type <=10){
// echo $column_info->type ."text" ."<br>";
$bind_type="s";
$bind_types[]=$bind_type;
}
if($column_info->type >251){
//echo $column_info->type ."text" ."<br>";
$bind_type="s";
$bind_types[]=$bind_type;
}
}
}
//create array with name of the columns in mysql table
$table_column_names=array();
foreach($fields as $key=>$name){
//echo $name."<br>";
$table_column_names[]=$name;
}
$columns=array();//names of columns in mysql table
$values=array();//value data to insert
foreach ($_POST as $variable_name=>$variable_data) {
if(in_array($variable_name,$table_column_names)){
$columns[]=($variable_name);
$values[]=($variable_data);
}
}
$a_bind_params=$values; //value data to insert
$a_param_type=$bind_types;// array of binds to check for example: "iddssddsis"
// bind parameters.
$param_type = '';
$n = count($a_param_type);
for($i = 0; $i < $n; $i++) {
$param_type .= $a_param_type[$i];
}
/* with call_user_func_array, array params must be passed by reference */
$a_params = array();
$a_params[] = & $param_type;
for($i = 0; $i < $n; $i++) {
/* with call_user_func_array, array params must be passed by reference */
$a_params[] = & $a_bind_params[$i];
}
$sql = "INSERT INTO table (" .implode(',',$columns). ") VALUE (" .implode(',',array_fill(0,count($columns),'?')). ")";
$stmt = $conn->prepare($sql);
/* use call_user_func_array, as $stmt->bind_param('s', $param); does not accept params array */
call_user_func_array(array($stmt, 'bind_param'), $a_params);
$stmt->execute();
?>