我准备了下面的表格,我需要通过这个表格将数据插入到 mysql 但问题是当我尝试插入 array_filter 时会出现问题。我只需要选择框值数据到 mysql。
示例:当用户选择选择框第 1 行和第 5 行时,只有这两行应该插入到 mysql 中,而不是这 5 行插入空值仍然插入数据。请帮忙
<form action="array-act.php" method="post">
<table width="500" border="0">
<tr>
<td><input name="name[]" value="One" type="text" /></td>
<td><input name="marks[]" type="text" /></td>
<td><input name="grade[]" type="text" /></td>
<td><select name="first[]"><option value="">Select</option><option value="SM">SM</option><option value="QB">QB</option></select></td>
</tr>
<tr>
<td><input name="name[]" value="Two" type="text" /></td>
<td><input name="marks[]" type="text" /></td>
<td><input name="grade[]" type="text" /></td>
<td><select name="first[]"><option value="">Select</option><option value="SM">SM</option><option value="QB">QB</option></select></td>
</tr>
<tr>
<td><input name="name[]" value="Three" type="text" /></td>
<td><input name="marks[]" type="text" /></td>
<td><input name="grade[]" type="text" /></td>
<td><select name="first[]"><option value="">Select</option><option value="SM">SM</option><option value="QB">QB</option></select></td>
</tr>
<tr>
<td><input name="name[]" value="Four" type="text" /></td>
<td><input name="marks[]" type="text" /></td>
<td><input name="grade[]" type="text" /></td>
<td><select name="first[]"><option value="">Select</option><option value="SM">SM</option><option value="QB">QB</option></select></td>
</tr>
<tr>
<td><input name="name[]" value="Five" type="text" /></td>
<td><input name="marks[]" type="text" /></td>
<td><input name="grade[]" type="text" /></td>
<td><select name="first[]"><option value="">Select</option><option value="SM">SM</option><option value="QB">QB</option></select></td>
</tr>
</table>
<input name="Go" type="submit" />
</form>
<?php
$name = $_POST['name'];
$marks = $_POST['marks'];
$grade = $_POST['grade'];
$option = array_values(array_filter($_POST['first']));
$n = count($option);
for ($i = 0; $i < $n; $i++)
{
$query = "INSERT INTO `table` (`name`, `marks`, `grade`, `option`)
VALUES ('$name[$i]', '$marks[$i]', '$grade[$i]', '$option[$i]')";
// Here you must execute your query
$result = mysql_query($query) or die('Failed to connect to server: ' . mysql_error());
}
if($result)
{
echo "Data Inserted";
}
else
{
echo "Data Not Inserted";
}
?>