如何用php填充mysql数据库中的数据

时间:2014-03-23 21:18:22

标签: php mysql mysqli

我写了一个函数来填充我的数据库中的内容,但显示一些错误,我使用下面的函数来填充数据

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字符串类型

1 个答案:

答案 0 :(得分:0)

您需要在值周围加上单引号。爆炸时可以实现这一点:

$values  = "'" . implode("', '", $escaped_values) .  "'";