我正在从数据库中检索查询的结果。我已将记录的ID存储到复选框数组中。
我能够检索数据并将其成功存储到复选框数组中。现在我想将复选框的值插入到不同的表中。我运行此脚本,它没有显示任何错误,但数据未插入数据库。
谁能告诉我我做错了吗?我该如何解决?
<?php
$conn = mysqli_connect('localhost:3306', 'root', '', 'mydatabase');
if($conn -> connect_errno){
die('coudn\'t connect to the database' . mysqli_connect_error());
}
else{
$query = "select * from simple";
$result = $conn -> query($query);
echo '<table border="1px"><tr><td>Select</td><td>ID</td><td>Name</td><td>Surname</td></tr>';
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
echo '<tr>';
echo "<td><input type=\"checkbox\" name=\"checkbox[]\" id=\"checkbox[]\" value=\"".$row['id']."\" /></td>";
echo "<td>{$row['id']}</td> "."<td>{$row['Name']}</td>"."<td>{$row['Surname']}</td>";
echo '</tr>';
}
echo '</table>';
echo "Fetched data successfully\n";
$limit = count('$checkbox');
echo $limit;
for($i=0;!$limit > 0;$i++,$limit--){
$res = implode(",", '$checkbox');
$q1 = 'insert into joint_data values("'.$i.'","'.$res.'")';
if($conn -> query($query) == TRUE){
echo "Data Entered successfully\n";
}
echo $i,$res;
}
}
$result->free();
mysqli_close($conn);
?>
最佳答案
$conn = mysqli_connect('localhost:3306', 'root', '', 'mydatabase');
if($conn -> connect_errno){
die('coudn\'t connect to the database' . mysqli_connect_error());
}
else{
if(!isset($_GET['checkbox'])){
$query = "select * from simple";
$result = $conn -> query($query);
echo '<form action="" method="get"><table border="1px"><tr><td>Select</td><td>ID</td><td>Name</td><td>Surname</td></tr>';
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
echo '<tr>';
echo "<td><input type=\"checkbox\" name=\"checkbox[]\" value=\"".$row['id']."\" /></td>";
echo "<td>{$row['id']}</td> "."<td>{$row['Name']}</td>"."<td>{$row['Surname']}</td>";
echo '</tr>';
}
echo '<td><input type="submit" value="submit" name="submit" /></td></table></form>';
echo "Fetched data successfully\n";
}else{
$data = $_GET['checkbox'];
$query = "select * from simple";
$result = $conn -> query($query);
$i=1;
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
if (in_array($row['id'], $data)) {
$q1 = 'insert into joint_data values("'.$i++.'","'.$row['id'].'")';
if($conn -> query($query) == TRUE){
echo "Data Entered successfully\n";
}
echo $i,$row['id'];
}
}
}
$result->free();
mysqli_close($conn);
}
关于php - 当面颊值也已从数据库中检索时,如何将复选框数组的值插入数据库?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28211987/