gpt4 book ai didi

php - 将复选框值插入数据库

转载 作者:行者123 更新时间:2023-11-29 23:46:01 24 4
gpt4 key购买 nike

目前我正在用 php 做一个项目,用于上传 CSV 文件并将数据插入数据库。在插入之前,我需要在复选框中显示数据并插入选中的值。

使用此代码,上传后会显示带有复选框的数据,但单击复选框时,值不会插入到数据库中。

if(isset($_POST['upload']))
{
if (is_uploaded_file($_FILES['filename']['tmp_name']))
{
echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
}

$handle = fopen($_FILES['filename']['tmp_name'], "r");
echo("<table border='1'>");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
echo("<tr>\r\n");
foreach ($data as $index=>$val)
{
echo("\t<td><input type='checkbox' name='chk1[]' value='$val'>$val</td>\r\n");
}
echo("</tr>\r\n");
}
echo("</table>");
echo("<td><input type='submit' name='insert' id='insert' value='Submit' /></td>");
fclose($handle);
}

为了将所选数据插入数据库,我使用此代码。

if(isset($_POST['insert']))
{
for($i=0;$i < sizeof($checkbox1);$i++)
{
$query = "INSERT INTO uploadmail (name,email) VALUES ('$name','".$checkbox1[$i]."')";
echo $query;
$result = mysql_query($query);
}
}

当我点击“提交”按钮时点击复选框后,它没有执行任何操作。所以请给我任何建议。

提前致谢。

最佳答案

Your table closed before the form submit. And please maintain the table structure if you are using form in a table. Close all input fields in table's td followed by "tr". And here its understood that you assigned $checkbox1 = $_POST['chk1'];

关于php - 将复选框值插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25947235/

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