将数组值发送到 mysql 数据库表 [英] Sending array values to a mysql database table

查看:58
本文介绍了将数组值发送到 mysql 数据库表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 HTML 代码集命名的 PHP 文件.分配了文本框的值,并根据我的需要进行打印.

I have a PHP file named with Set of HTML codes. The values for the textboxes are assigned and they prints as I needed.

<?
echo "<html>";
echo "<body>";
for($i=0; $i<5;$i++){

echo "<input name='C[]' value='$Texting[$i]' style='background-color:#D0A9F5;'></input>";

}
echo '<input type="submit" value="Save The Table" name="G"></input>'
echo "</body>";
echo "</html>";

?>

我需要将文本框的值存储在一个数组中并将其存储到另一个 PHP 文件中,以便将这些值发送到数据库.这是其他 PHP 文件的一部分.

And I need to store the values of the textboxes in an array and its to another PHP file in order to send the values to the database. This is a part of other PHP file.

<?
if(!empty($_POST['G'])){

for($i=0;$i<sizeof($_POST['C']);$i++){

echo var_dump($_POST['C']);

$Query="INSERT INTO Questions(Questions_ID,Question_Name)          VALUES($i+1,'$_POST[C][$i]')";

if(!mysqli_query($con,$Query)){

die("<script>alert( \"Error: ". mysqli_error($con)."\");window.location.href='FormCreationTrial.php';</script>");

}}

}

?>

这是数组的 var_dump.array(3) { [0]=> string(1) "A" [1]=> string(5) "Male2" [2]=> string(7) "Female2" }注意:第 19 行 C:\xampp\htdocs\PHIS\FinalSubmissionOfTheFormPHP.php 中的数组到字符串转换.当我试图一个一个地发送数组时,它给了我一个错误,如重复条目 'Array[0]"对于键Question_Name_UNIQUE".

This is the var_dump of the array. array(3) { [0]=> string(1) "A" [1]=> string(5) "Male2" [2]=> string(7) "Female2" } Notice: Array to string conversion in C:\xampp\htdocs\PHIS\FinalSubmissionOfTheFormPHP.php on line 19. When I'm trying to send the array one by one it gives me an error like ' Duplicate entry 'Array[0]' for key 'Question_Name_UNIQUE'.

这完美地打印了接收到的数组的大小.我需要将这些数据发送到一个名为问题"的表中.数组的键应该是名为 column ID 的表的主键,数组的值应该去表的 questionType 列.我怎样才能做到这一点?有人可以给我一个提示.

And This prints the size of the received array perfectly. I need to send those data to a table called 'questions. Keys of the array should be the primary key of the table named column ID and values of the array should go to questionType column of the table. How can I do this? could someone please give me a hint.

推荐答案

$C = $_POST["C"];

在你的 php 文件中试试这个来获取 post 输入字段的内容,然后你可以从这里打开这个数组

try this in your php file to get contents of the post input field and from here you could just open this array

foreach($C as $value)
{
     //your database code here if mysql 
     //$value is your value

}

如果你愿意,我更愿意

var_dump($C);

这将向您显示存储在数组中的所有内容,然后您可以根据该值存储值

this would show you all the contents getting stored inside your array and then you can store the value according to that

这篇关于将数组值发送到 mysql 数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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