从数据库检索的Web上的表中删除行 [英] Deleting rows from table on web retrieved from database
本文介绍了从数据库检索的Web上的表中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个问题,从数据库中删除行,我回到我的网站,我使用滴答复选框,当选择倍数时,应删除它们。但它只是没有HAPPENING!没有什么是从数据库中删除!请帮忙!
< form method =action =tester.php>
<?php
include'connect_to_mysql.php';
$ count = 0;
$ count = mysql_num_rows($ result);
$ result = mysql_query(SELECT * FROM booking ORDER BY ID ASC);
echo< table border ='1'>
< tr>
< th> DEL< / th>
< th> ; / th>
< th>电子邮件ID< / th>
th电话号码< / th>
< th>集合地址< / th>
< th>收集日期< / th>
< th>收集时间< / th>
< th> Make& Model< / th>
& / th>
< / tr>;
while($ row = mysql_fetch_array($ result))
{
?>
< td align =centerbgcolor =#FFFFFF>< input name =delete_these []type =checkboxid =checkbox []value =& $ row ['ID'];?>>< / td>
<?php
echo< td> 。 $ row ['name']。 < / td>;
echo< td> 。 $ row ['email']。 < / td>;
echo< td> 。 $ row ['phonenumber']。 < / td>;
echo< td> 。 $ row ['collectionaddress']。 < / td>;
echo< td> 。 $ row ['collectiondate']。 < / td>;
echo< td> 。 $ row ['collectiontime']。 < / td>;
echo< td> 。 $ row ['makemodel']。 < / td>;
echo< td> 。 $ row ['message']。 < / td>;
echo< / tr>;
}
echo< / table>;
?> < br>
< td colspan =5align =centerbgcolor =#FFFFFF>< input name =deletetype =submitid =deletevalue =Delete> < /td>
< / tr>
<?php
//检查删除按钮是否激活,启动
if(isset($ _ GET ['delete'])){
for($ i = 0; $ i <$ count; $ i ++){
$ id =(int)$ _ POST ['delete_these'] [$ i];
$ sql =DELETE FROM booking WHERE ID ='$ id';
print_r($ _ GET ['delete_these []']);
$ sql =DELETE FROM booking WHERE ID IN($ ids);
echo< br /> SQL:$ sql< br />;
$ result = mysql_query($ sql);
}
if($ result){
}
}
mysql_close
?>
< / form>
解决方案
示例代码:
< form method =POSTaction =index.php>
< table>
<?php while($ row = mysql_fetch_array($ result)):?>
< tr>
< td>< input type =checkboxname =delete_these []value =<?php echo $ row ['id'];?& />< / td>
< td><?php echo $ row ['name']; ?>< / td>
< / tr>
<?php endwhile; ?>
< / table>
< input type =submitname =deletevalue =删除所选项/>
< / form>
<?php
$ selected_values = array();
if(isset($ _ POST ['delete'])){
$ selected_values = $ _POST ['delete_these'];
$ ids = implode(',',$ selected_values);
$ query = mysql_query(DELETE FROM booking WHERE id IN($ ids));
// this become - >从预订中删除id(1,2,3,...)
}
?>
而且还可以使用mysqli或PDO, b
I am having issue with deleting rows from a database that I echoed onto my website, I have used tick check boxes and when multiples are selected they should be deleted. But it's just NOT HAPPENING! Nothing is getting deleted from the database! please help!
<form method="" action="tester.php">
<?php
include 'connect_to_mysql.php';
$count=0;
$count=mysql_num_rows($result);
$result = mysql_query("SELECT * FROM booking ORDER BY ID ASC");
echo "<table border='1'>
<tr>
<th>DEL</th>
<th>Name</th>
<th>Email ID</th>
<th>Phone Number</th>
<th>Collection Address</th>
<th>Collection Date</th>
<th>Collection Time</th>
<th>Make & Model</th>
<th>Message</th>
</tr>";
while($row = mysql_fetch_array($result))
{
?>
<td align="center" bgcolor="#FFFFFF"><input name="delete_these[]" type="checkbox" id="checkbox[]" value="<?php echo $row['ID']; ?>"></td>
<?php
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['phonenumber'] . "</td>";
echo "<td>" . $row['collectionaddress'] . "</td>";
echo "<td>" . $row['collectiondate'] . "</td>";
echo "<td>" . $row['collectiontime'] . "</td>";
echo "<td>" . $row['makemodel'] . "</td>";
echo "<td>" . $row['message'] . "</td>";
echo "</tr>";
}
echo "</table>";
?> <br>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?php
// Check if delete button active, start this
if(isset($_GET['delete'])) {
for($i=0;$i<$count;$i++){
$id =(int)$_POST['delete_these'][$i];
$sql = "DELETE FROM booking WHERE ID='$id'";
print_r($_GET['delete_these[]']);
$sql = "DELETE FROM booking WHERE id IN($ids)";
echo "<br />SQL: $sql<br />";
$result = mysql_query($sql);
}
if($result){
}
}
mysql_close();
?>
</form>
解决方案
First off you can just implode()
all the gathered ids from the form and from there build the query.
Sample code:
<form method="POST" action="index.php">
<table>
<?php while($row = mysql_fetch_array($result)): ?>
<tr>
<td><input type="checkbox" name="delete_these[]" value="<?php echo $row['id']; ?>" /></td>
<td><?php echo $row['name']; ?></td>
</tr>
<?php endwhile; ?>
</table>
<input type="submit" name="delete" value="Delete Selected" />
</form>
<?php
$selected_values = array();
if(isset($_POST['delete'])) {
$selected_values = $_POST['delete_these'];
$ids = implode(',', $selected_values);
$query = mysql_query("DELETE FROM booking WHERE id IN($ids)");
// this becomes -> delete from booking where id in (1, 2, 3, ...)
}
?>
and while you still can, use mysqli or PDO, its free anyway
这篇关于从数据库检索的Web上的表中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文