未插入DB-PHP的选中值
因此,我试图从复选框中插入选中的值,但不起作用。我相信我的问题是,他们没有在一开始就正确设置。我试图使用循环,但我相信我做得不对:P 我的“isset”: 以及插入过程:未插入DB-PHP的选中值,php,checkbox,insert,key-value,isset,Php,Checkbox,Insert,Key Value,Isset,因此,我试图从复选框中插入选中的值,但不起作用。我相信我的问题是,他们没有在一开始就正确设置。我试图使用循环,但我相信我做得不对:P 我的“isset”: 以及插入过程: //Insert for Checkboxes// ///////////////////////// $query2 = " INSERT INTO coffee_ckbx ( white, green, red,
//Insert for Checkboxes//
/////////////////////////
$query2 = "
INSERT INTO coffee_ckbx (
white,
green,
red,
blue
) VALUES (
:white,
:green,
:red,
:blue
)
";
foreach($_POST['ckboxes'] as $check){array($check);}
$query_params2 = array(
':white' => $check['1'],
':green' => $check['2'],
':red' => $check['4'],
':blue' => $check['5']
);
try
{
// Execute the query to create the user
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
$stmt2 = $db->prepare($query2);
$result2 = $stmt2->execute($query_params2);
}
//kept the catch out and other code due to lengthiness.
这是我的html:
<li>
<label for='ck-boxes'>Select from the check boxes: </label>
<input type="hidden" name="ckboxes" value="0" />
<input type='checkbox' id='' name='ckboxes' value='white'/>
<input type='checkbox' id='' name='ckboxes' value='green'/>
<input type='checkbox' id='' name='ckboxes' value='red'/>
<input type='checkbox' id='' name='ckboxes' value='blue'/>
</li>
从复选框中选择:
有什么想法吗?
提前感谢:)尝试在名称末尾添加
[]
,这样会告诉PHP这是一个数组
<input type="hidden" name="ckboxes[]" value="0" />
<input type='checkbox' id='' name='ckboxes[]' value='white'/>
<input type='checkbox' id='' name='ckboxes[]' value='green'/>
<input type='checkbox' id='' name='ckboxes[]' value='red'/>
<input type='checkbox' id='' name='ckboxes[]' value='blue'/>
也不要引用数字,引号内的内容被视为
string
因此,
$check['1']
应该是$check[1]
作为示例。是实际的表单
中的输入吗?是的,它们在表单中。在这里你可以看到我的代码,也许这会有所帮助。这对我来说非常好。只需删除isset($CKBOX),然后放入$CKBOX=$\u POST['CKBOX']在您的“isset
”中输入code>。谢谢,它现在可以工作了。虽然我不明白你的意思,所以如果我使用相同的代码就不会了。我的代码:我有另一个问题,我没有;我无法解决。为了插入任何数据,我必须选中所有复选框,如果不选中,则未选中的复选框将出现“未定义偏移”错误。
<input type="hidden" name="ckboxes[]" value="0" />
<input type='checkbox' id='' name='ckboxes[]' value='white'/>
<input type='checkbox' id='' name='ckboxes[]' value='green'/>
<input type='checkbox' id='' name='ckboxes[]' value='red'/>
<input type='checkbox' id='' name='ckboxes[]' value='blue'/>