gpt4 book ai didi

php - 在 SQL 中将多个复选框值作为单个条目插入

转载 作者:行者123 更新时间:2023-11-29 06:04:04 25 4
gpt4 key购买 nike

我有不同的复选框,如下面的代码所述,这段代码在我这边工作正常,因为它连接所有选定的值并作为“PHP、.NET、JavaScript”等插入数据库,但我想要的是,如果我选择了 3 个复选框,它应该添加三个单独的条目 1. PHP 2. JavaScript,3. Java。任何想法或概念将不胜感激。

    <html>  
<body>
<form action="" method="post" enctype="multipart/form-data">
<div style="width:200px;border-radius:6px;margin:0px auto">
<table border="1">
<tr>
<td colspan="2">Select Technolgy:</td>
</tr>
<tr>
<td>PHP</td>
<td><input type="checkbox" name="techno[]" value="PHP"></td>
</tr>
<tr>
<td>.Net</td>
<td><input type="checkbox" name="techno[]" value=".Net"></td>
</tr>
<tr>
<td>Java</td>
<td><input type="checkbox" name="techno[]" value="Java"></td>
</tr>
<tr>
<td>Javascript</td>
<td><input type="checkbox" name="techno[]" value="javascript"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="submit" name="sub"></td>
</tr>
</table>
</div>
</form>
<?php
if(isset($_POST['sub']))
{
$host="localhost";//host name
$username="root"; //database username
$word="";//database word
$db_name="dbtask";//database name
$tbl_name="new"; //table name
$con=mysqli_connect("$host", "$username", "$word","$db_name")or die("cannot connect");//connection string
$checkbox1=$_POST['techno'];
$chk="";
foreach($checkbox1 as $chk1)
{
$chk .= $chk1.",";
}
$in_ch=mysqli_query($con,"insert into new(time) values ('$chk')");
if($in_ch==1)
{
echo'<script>alert("Inserted Successfully")</script>';
}
else
{
echo'<script>alert("Failed To Insert")</script>';
}
}
?>
</body>
</html>

最佳答案

foreach($checkbox1 as $key => $value)
{
$in_ch=mysqli_query($con,"insert into new(time) values ('$value')");

}

希望对您有所帮助!

关于php - 在 SQL 中将多个复选框值作为单个条目插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42923658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com