我编写了一个函数来填充数据库中的内容,但显示一些错误,我使用下面的函数来填充数据
function fillData($con,$dbName,$tableName,$arr){
$columns = implode(", ",array_keys($arr));
$escaped_values = array_map('mysql_real_escape_string', array_values($arr));
$values = implode(", ", $escaped_values);
$sql = "INSERT INTO ". $tableName ." ($columns) VALUES ($values)";
$myfilling=mysqli_query($con,$sql);
if($myfilling){
echo "\n data succsess add to table";
}else{
echo "\n Error creating table: " . mysqli_error($con);
}
mysqli_close($con);
}
但返回错误
Error creating table: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'M, 1392/12/28, 12:30:08, 3.99 M, 10 B, 10%, 493, 17.63, 17.60, ┘à╪ش╪د╪▓' at line 1
我的代码中有什么问题取决于 $values 字符串类型
最佳答案
您需要在值两边加上单引号。您可以在内爆时实现此目的,如下所示:
$values = "'" . implode("', '", $escaped_values) . "'";
关于php - 如何用php向mysql数据库填充数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22597196/