i have use function for inserting data to my sql.
the data is
function mysql_insert($table, $arr){
if($arr!=''){
$values = array_map('mysql_real_escape_string', array_values($arr));
$keys = array_keys($arr);
$q=('INSERT INTO `'.$table.'` (`'.implode('`,`', $keys).'`) VALUES (\''.implode('\',\'', $values).'\')');
//$res = mysql_query($q)OR die(mysql_error());
return $q;
}else{
return false;
}
data and query is came from :
if($crud=='insert'){
$field= array( 'col1' => 'apel',
'c0l2' => 'box1',
'col3' => 200,//integer Quantity
);
$data=mysql_insert('wh',$field);}echo json_encode($data);
and result is
= “INSERT INTO wh (col1,c0l2,col3) VALUES (‘apel’,’box1′,’200′)”
that col3 have value as string. i need that col3 as integer.
what wrong with this code?
This is happening because the command
implode('\',\'', $values)is telling it to turn the array$valuesinto a string, with single quotes and commas between each element. If you want to not put quotes around numbers, you’ll need to iterate over $values with a more specific function.However, it doesn’t actually matter. If you give MySQL a string containing a number for a numeric field, it will just convert it to a number of the appropriate type. You can leave it as-is and everything should work.