简体   繁体   English

使用PHP将JS数组插入MySQL的最简单方法

[英]Easiest way to insert a JS array into MySQL using PHP

UPDATE!! 更新! My code is actually valid to run. 我的代码实际上可以运行。 The original question is below. 原始问题如下。 If you are still curious please read the question and then back to read my update. 如果您仍然好奇,请阅读问题,然后返回阅读我的更新。

My bad, the db actually can get updated successfully after the code runs. 不好的是,代码运行后,db实际上可以成功更新。 I accidentally set the datatype in the table as INT but my initial intention, varchar. 我不小心将表中的数据类型设置为INT,但最初的意图是varchar。

But from HenryDev's answer I managed to find the acquired array by post method in php becomes STRING type but array type. 但是从HenryDev的答案中,我设法通过php中的post方法找到了获得的数组,成为STRING类型,但变为数组类型。 I used 我用了

$cameArray = $_POST['cameArray'];
echo gettype($cameArray);

to output "string" on my screen, so implode would not work oh that(see the discussions on HenryDev's answer). 在我的屏幕上输出“字符串”,这样爆裂就不起作用了(请参阅关于HenryDev答案的讨论)。

However his answer will work as a charm if you set up an array in PHP and implode it, it will give you back a "string type" of your array! 但是,如果您使用PHP设置一个数组并将其内爆,那么他的回答将很吸引人,它将为您提供数组的“字符串类型”!

UPDATE!! 更新! My code is actually valid to run. 我的代码实际上可以运行。 The original question is below. 原始问题如下。 If you are still curious please read the question and then back to read my update. 如果您仍然好奇,请阅读问题,然后返回阅读我的更新。

Here is my original question 这是我最初的问题

So I have a very basic question but the tutorials and answers I found online are kinda complicated for my situation. 所以我有一个非常基本的问题,但是我在网上找到的教程和答案对我的情况来说有点复杂。

I have a JS array like 我有一个像

var sentArray = ['1','2','3'];

I want to insert this array into the database as one field of a table. 我想将此数组作为表的一个字段插入数据库中。 I use ajax to send this array to PHP and then execute the query in PHP. 我使用ajax将这个数组发送到PHP,然后在PHP中执行查询。 For instance, 例如,

$cameArray = $_POST['cameArray']; //then $cameArray is sentArray

mysqli_query($con, "INSERT INTO thisTable (arrayField) VALUES ($cameArray)");

And then I want the value in the database shown as "1, 2, 3", but the problem is the value in the database is simply shown as a single "2". 然后我希望数据库中的值显示为“ 1、2、3”,但是问题是数据库中的值只是显示为单个“ 2”。 The type in table is varchar. 表中的类型为varchar。

No any JSON object involved just pure text. 没有任何JSON对象只涉及纯文本。 How could I do that? 我该怎么办? Thanks! 谢谢!

Here's a quick solution. 这是一个快速的解决方案。 If you want to store the array as an string simply do this in your PHP code. 如果要将数组存储为字符串,只需在PHP代码中执行此操作即可。

$array = array('1', '2', '3'); // Your array  
$cameArray = implode(",", $array);

echo $cameArray; // "1,2,3"    

Now you can store the variable $cameArray in your Data Base. 现在,您可以将变量$cameArray存储在$cameArray中。 Hope it helps! 希望能帮助到你!

My bad, the db actually can get updated successfully after the code runs. 不好的是,代码运行后,db实际上可以成功更新。 I accidentally set the datatype in the table as INT but my initial intention, varchar. 我不小心将表中的数据类型设置为INT,但最初的意图是varchar。

But from HenryDev's answer I managed to find the acquired array by post method in php becomes STRING type but array type. 但是从HenryDev的答案中,我设法通过php中的post方法找到了获得的数组,成为STRING类型,但变为数组类型。 I used 我用了

$cameArray = $_POST['cameArray'];
echo gettype($cameArray);

to output "string" on my screen, so implode would not work oh that(see the discussions on HenryDev's answer). 在我的屏幕上输出“字符串”,这样爆裂就不起作用了(请参阅关于HenryDev答案的讨论)。

However his answer will work as a charm if you set up an array in PHP and implode it, it will give you back a "string type" of your array! 但是,如果您使用PHP设置一个数组并将其内爆,那么他的回答将很吸引人,它将为您提供数组的“字符串类型”!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM