Hi Kender,
Your functions was a great help for me!
I have added an improvement to the sql_insert function that makes it easier to add the types to bindparam, since within the same foreach we can evaluate the field type and add the value to the array $ typeDef
foreach ($data AS $k=>$value) {
if($key == '') {
$key = $k;
$val = '?';
} else {
$key .= ', '.$k;
$val .= ', ?';
}
$typeDef[]= is_int($value)==true ? 'i' : 's';
}
Finally the script was like this
function sql_insert ($data, $typeDef, $table) {
$mysqli = mysqli_con();
$keys = array_keys($data);
$vals = array_values($data);
$key = '';
$val = '';
$type = '';
foreach ($data AS $k=>$value) {
if($key == '') {
$key = $k;
$val = '?';
} else {
$key .= ', '.$k;
$val .= ', ?';
}
$typeDef[]= is_int($value)==true ? 'i' : 's';
}
if ($stmt = $mysqli->prepare("INSERT INTO $table ($key) values ($val)")) {
for($i = 0; $i < count($typeDef); $i++) {
/* Set params value for binding */
$params[$keys[$i]] = $typeDef[$i];
}
/* Bind our params */
bindParameters($stmt, $params);
for($i = 0; $i < count($typeDef); $i++) {
/* Set params for query */
$params[$keys[$i]] = $vals[$i];
}
/* Execute the prepared Statement */
if(!$stmt->execute() ) { printf("Execute Statement Error: %s\n", $stmt->error); exit; }
/* Echo results */
$insert = "Success";
/* Close the statement */
$stmt->close();
} else {
/ Error /
$insert = "Failure";
}
return $insert;
}
?>
Thanks a lot for your help!