方法post表单中的checkbox,在php中从数组转换为字符串

41zrol4v  于 2021-06-20  发布在  Mysql
关注(0)|答案(3)|浏览(244)

我有一个表格上传项目到我的数据库。在这个表单中,我选择了一些复选框来为项目提供标记。最好是将这些标记保存为数据库中的单个字符串,用逗号分隔。然而,无论我尝试什么,我似乎只得到一个错误或只是我的复选框的最终结果。

<form action="storeupdate.php" enctype="multipart/form-data" method="post">
<input type='checkbox' name='Tags' value='Milk' >Milk<br/>
<input type='checkbox' name='Tags' value='Sugar' >Sugar<br/>
<input type='checkbox' name='Tags' value='Cream' >Cream<br/>
<input type='checkbox' name='Tags' value='Chocolate' >Chocolate<br/>
<input type="reset"> <input type="submit" name="Submit" value="Submit"><br/>
</form>

storeupdate.php:

$Tags = mysqli_real_escape_string($conn, $_REQUEST['Tags']);

我尝试过foreach循环,但它们似乎没有给我任何结果。当我回显$tags时,我只得到已提交的“最高”结果。
如果我甚至不知道如何获取单个值,我甚至不能在将它们放入数据库之前开始编写一个脚本来将它们串在一起。

更新

既然我们已经确定我在表单中做错了,我仍然不知道如何捕获和转换这个数组。

$Tags[] = mysqli_real_escape_string($conn, $_POST['Tags[]']);
$tagstring = implode(",",$Tags);
echo "<br/> $_REQUEST = ";
var_dump($_POST['Tags']);
echo "<br/> $Tags = ";
var_dump($Tags);
echo "<br/> $tagstring = ";
var_dump($tagstring);
echo "<br/>";
echo "Tags= " . $tagstring . "<br/>";

返回以下内容:array=array(6){[0]=>string(1)“1”[1]=>string(1)“2”[2]=>string(1)“3”[3]=>string(1)“4”[4]=>string(1)“5”[5]=>string(1)“6”}array=array(1){[0]=>string(0)”}=string(0)”“标记=
我需要这个数组作为一个用逗号分隔的字符串,以便使其余的构建代码正常工作,但是在捕获数据之后,我在处理中做了一些错误的事情。

nwlqm0z1

nwlqm0z11#

<form action="storeupdate.php" method="post">             //enctype="multipart/form-data" The "multipart/form-data" is not required unless you are uploading a file. otherwise no need to set it. It has a default value of "application/x-www-form-urlencoded"
    <input type='checkbox' name='tags[]' value='Milk' >Milk<br/>             //Try to use small letters, hyphen and underscore when setting the name attribute of your form.
    <input type='checkbox' name='tags[]' value='Sugar' >Sugar<br/>
    <input type='checkbox' name='tags[]' value='Cream' >Cream<br/>
    <input type='checkbox' name='tags[]' value='Chocolate' >Chocolate<br/>
    <input type="reset"> <input type="submit" name="submit" value="Submit"><br/>
</form>

现在您的数据将以$\u post['tags']形式显示。

u2nhd7ah

u2nhd7ah2#

您应该编写这样的html:

<form action="storeupdate.php" enctype="multipart/form-data" method="post">
<input type='checkbox' name='Tags[]' value='Milk' >Milk<br/>
<input type='checkbox' name='Tags[]' value='Sugar' >Sugar<br/>
<input type='checkbox' name='Tags[]' value='Cream' >Cream<br/>
<input type='checkbox' name='Tags[]' value='Chocolate' >Chocolate<br/>
<input type="reset"> <input type="submit" name="Submit" value="Submit"><br/>
</form>

并从$\u post请求中获取$\u post['tags'],其中$\u post['tags']是您的标签数组。

3xiyfsfu

3xiyfsfu3#

如果你想拥有 Tags 您需要在html中将此参数声明为数组:

<input type='checkbox' name='tags[]' value='Milk' >Milk<br/>
<input type='checkbox' name='tags[]' value='Sugar' >Sugar<br/>
<input type='checkbox' name='tags[]' value='Cream' >Cream<br/>
<input type='checkbox' name='tags[]' value='Chocolate' >Chocolate<br/>

现在 $_REQUEST['tags'] 将包含一个数组。

相关问题