作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个表单可以将项目上传到我的数据库。在此表单中,我可以选择复选框来为项目添加标签。最好我想将这些标签保存为数据库中的单个字符串,并用逗号分隔。但是,无论我尝试什么,我似乎都只收到错误或只是复选框的最终结果。
<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/>";
返回以下内容:数组 = 数组(6) { [0]=> 字符串(1) "1"[1]=> 字符串(1) "2"[2]=> 字符串(1) "3"[3]=> 字符串( 1) "4"[4]=> 字符串(1) "5"[5]=> 字符串(1) "6"}数组 = 数组(1) { [0]=> 字符串(0) ""}= 字符串(0) ""标签=
我需要这个数组作为用逗号分隔的字符串,以便使我的构建代码的其余部分正常工作,但是我在处理之后做了一些错误的事情捕获数据。
最佳答案
你应该编写这样的 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>
并从 $_POST 请求中获取 $_POST['Tags'] ,其中 $_POST['Tags'] 是标签数组。
关于php - Method Post 形式的复选框,在 PHP 中从数组转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51982789/
我是一名优秀的程序员,十分优秀!