0

我有一些基本代码,它从表单中收集 chkbox 和单选按钮是/否答案等并存储在 mysql 中,但一些 mysql 提交工作(即在 sql 列中放置是或否)但有些只是在 sql 列中显示为'array' - 这是代码示例。$repeat 显示为“数组”,其中 $inter 显示为是或否。mysql 中的列在结构上是相同的,表单 HTML 是相同的语法,$_POST 数组是逗号分隔的,因为我也通过电子邮件发送值并且电子邮件正确显示答案 - 这很奇怪

<?php
$repeat = "";
foreach ($_POST['repeat'] as $key => $value) {
$repeat.= ",$value ";
}
$repeat = substr($repeat,1);

$inter = "";
foreach ($_POST['intermitant'] as $key => $value) {
$inter.= ",$value ";
}
$inter = substr($inter,1);

//other posts all looped though etc
//then the sql - log on etc

$sql="INSERT INTO faults (repeat_visit,intermitant) VALUES ('$repeat','$inter')";

这是 HTML - 我可以看到它没有什么奇怪的,我得到了你上面描述的内容 - 但是为什么一个工作而另一个不使用相同的代码,相同的方法应用于复选框和一些其他的 wiork 很好的 put 'array ' 在 sql 中

<label>Repeat Visit:</label>
<input type="radio" name="repeat[]" value="Yes" />Yes
<input type="radio" name="repeat[]" value="No" />No

<label>Intermitant Fault:</label>
<input type="radio" name="intermitant[]" value="Yes" />Yes
<input type="radio" name="intermitant[]" value="No" />No   
4

1 回答 1

0

As popnoodles stated, seeing the form's HTML would help, but this looks as if each element in $_POST['repeat'] & $_POST['intermitant'] is actually an array, and so the $value of the foreach() is returning "Array"

Check that the checkbox array is only 1 dimensional, and if it is not, does it have to be multi-dimensional?

If so, perhaps a recursive function or nested loop to implode the values is required

$repeat = "";
foreach ($_POST['repeat'] as $k => $v)
{
    foreach ($v as $kInner => $vInner)
    {
         $repeat .= ",$vInner ";
    }
}
于 2013-02-15T04:59:45.533 回答