implode() array to insert record into mySql database

对着背影说爱祢 提交于 2019-12-06 15:52:22
Jordan

As @Barmar has pointed out, the problem is your quotes are on the outside of your variable.

I think this may be an easier to follow/cleaner way of fixing this however than the method Barmar posted:

$newdata = "'" . implode("','", $_POST) . "'";

You need to quote each value, not the entire list of values:

$fields = implode(",", array_keys($_POST));
$newdata = implode(",", array_map(function($x) use ($dbc) {
    return "'" . $dbc->real_escape_string($x) . "'";
}, $_POST));

$query = (
"INSERT INTO Food_entered ($fields)
VALUES ($newdata)");

$result = mysqli_query($dbc, $query);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!