我正在制作库存计划,我需要以下方面的帮助: 我有一个表,通过从我的数据库中选择数据并在表中显示它。 我现在需要让用户能够从表单中删除错误/错误的记录(当然,你不希望每次都必须在数据库中去除记录,这将是愚蠢的)
我的代码:
<div id="artikel-container">
<table class="table 1">
<thead>
<title>Inventory Grid.html</title>
<meta charset = "UTF-8" />
<style type = "text/css">
table, td, th {
border: 1px solid black;
}
</style>
</thead>
<tbody>
<?php
$con = mysqli_connect("localhost", "root", "admin", "inventarisdb");
// Check connection
if ( mysqli_connect_errno() ) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query( $con, "SELECT * FROM BCD" );
echo "<table border='0'>
<tr>
<th>Categorie</th>
<th>SerieNummer</th>
<th>MacAdress</th>
<th>ProductCode</th>
<th>Prijs</th>
<th>RekNummer</th>
<th>PaletNummer</th>
<th>Hoeveelheid</th>
<th>Aantekeningen/th>
</tr>";
while( $row = mysqli_fetch_array( $result ) ) {
echo "<tr>";
echo "<td>" . $row['Categorie'] . "</td>";
echo "<td>" . $row['SerieNummer'] . "</td>";
echo "<td>" . $row['MacAdress'] . "</td>";
echo "<td>" . $row['ProductCode'] . "</td>";
echo "<td>" . $row['Prijs'] . "</td>";
echo "<td>" . $row['RekNummer'] . "</td>";
echo "<td>" . $row['PaletNummer'] . "</td>";
echo "<td>" . $row['Hoeveelheid'] . "</td>";
echo "<td>" . $row['Aantekeningen'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</article>
</fieldset>
</tbody>
此代码完美无缺!
我想要实现的是使用checkbos从此表中选择记录,并通过单击页面底部的删除按钮将其删除,如下所示:
<Input type="submit" id="submit" name="submit" value="Verwijderen" />
或至少有这个原则!
(这是我写的第二个应用程序,第一个是网页,我不知道如何研究这个。我一直在寻找不是我需要的问题和帖子:S Hope U可以帮助我)< / p>
答案 0 :(得分:1)
我建议您使用jQuery ajax,这样您就可以逐页删除条目而无需重新加载页面。
首先包括HTML HEADER中的jQuery库和Javascript脚本文件
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="script.js"></script>
的script.js:
$(document).ready(function() {
$(".delete").click(function(event) {
var entryid = $(this).closest('tr').find('.entryid').val();
alert('This entry will be deleted: ' + entryid); // this will alert you the value what you get. After finisgin to get correct value you can delete this
$.ajax({
type: "GET",
url: "delete.php",
data: "id=" + entryid
});
$(this).closest('tr').remove();
});
});
MySQL数据库中的必须是名为“id”
的每个条目的唯一IDGrid.html:
echo "<td>" . $row['Aantekeningen'] . "</td>";
echo '<td><input type="hidden" class="entryid" name="id" value='.$row['id'].' /><a href="#" class="delete">delete</a></td>';
delete.php:
// connect to database
$id = $_GET['id'];
$sql="DELETE FROM BCD WHERE id='$id'";
答案 1 :(得分:0)
首先,您需要将表格包装成表格
<div id="artikel-container">
<form name="form1" method="post" action="...">
<table class="table 1">
...
echo "</table></form>";
mysqli_close($con);
接下来你需要表格上的复选框$ row ['ID']是表格的主键
echo "<td>" . $row['Aantekeningen'] . "</td>";
echo "<td><input type='checkbox' name='del[]' value='".$row['ID'] . "></td>";
echo "</tr>";
在表单的action属性中调用的php文件中:
<?php
if( isset( $_POST['del'] ) {
$del = $_POST['del'];
for(x=0; x<count(del); x++ ) {
$sql = 'DELETE FROM BCD WHERE ID='.$del[$x];
//---execuste the query
}
}
?>