implode()数组以将记录插入到MySQL数据库中 [英] implode() array to insert record into mySql database

查看:281
本文介绍了implode()数组以将记录插入到MySQL数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP数组中只有一行,我想通过将键和值内插到字符串中并在我的Insert语句中使用这些字符串,将该行插入到mySQL数据库中,如下所示:

I have a single row in a PHP array and I would like to insert that row into mySQL database by imploding the keys and values into a string and using those strings in my Insert statement as follows:

$fields = implode(",", array_keys($_POST));
$newdata = implode(",", $_POST);

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

$result = mysqli_query($dbc, $query);

我能够创建字符串,并且它们似乎格式正确,但是未插入行.似乎是一种简单的方法,但不确定我缺少什么.

I am able to create the strings, and they appear to be in proper form ,however the row is not being inserted. Seems like a simple approach but not sure what I'm missing.

推荐答案

正如@Barmar指出的那样,问题在于您的引号位于变量的外部.

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

我认为,相比于Barmar发布的方法,这可能是更容易遵循/更清洁的解决方法:

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

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

这篇关于implode()数组以将记录插入到MySQL数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆