我正在编写一个脚本,其中一个功能应该是删除表中的一行,但是当我尝试删除该行时,它最终删除了错误的行。
我的脚本中的代码如下:
<?php
session_start();
include '../includes/conn.php';
$verified = $_SESSION['verified'];
if($verified !== true){
header("Location: index.php");
}
if(isset($_POST['id'])){
$query = $conn->prepare("DELETE FROM inventory WHERE ID=?");
$query->bind_param('s', $_POST['id']);
$query->execute();
}
if(isset($_POST['desc']) && isset($_POST['mcost']) && isset($_POST['price']) && isset($_POST['rrp']) && isset($_POST['loc'])){
date_default_timezone_set("Europe/London");
$date = (string)date("l jS \of F Y");
$query = $conn->prepare("INSERT INTO inventory (Description, MaterialsPrice, Price, RRP, Location, Date_Created) VALUES (?, ?, ?, ?, ?, ?)");
$query->bind_param('ssssss', $_POST['desc'], $_POST['mcost'], $_POST['price'], $_POST['rrp'], $_POST['loc'], $date);
$query->execute();
header("Location: inventory.php");
}
?>
<html>
<head>
<title>Encanto Jewellery - Inventory</title>
<style type="text/css">
@import url(../includes/tablestyles.css);
</style>
</head>
<body>
<?php
$desca = array();
$mpa = array();
$pa = array();
$rrpa = array();
$loca = array();
$datea = array();
$query = $conn->prepare("SELECT Description, MaterialsPrice, Price, RRP, Location, Date_Created FROM inventory ORDER BY ID desc");
$query->execute();
$query->bind_result($desc, $mp, $p, $rrp, $loc, $date);
while($query->fetch())
{
$desca[] = $desc;
$mpa[] = $mp;
$pa[] = $p;
$rrpa[] = $rrp;
$loca[] = $loc;
$datea[] = $date;
}
?>
<form action="" method="POST" name="insert">
<div class="col" style="border-style: solid solid none solid;">
<div class="row">
<b><p>Description</p></b>
</div>
<?php
for($i = 0; $i < count($desca); $i++)
{
echo '<div class="row"><p>' . $desca[$i] . '</p></div>';
}
?>
<div class="row">
<textarea style="width: 99%;" rows="1" name="desc"></textarea>
</div>
</div>
<div class="col">
<div class="row">
<b><p>Price of Materials</p></b>
</div>
<?php
for($i = 0; $i < count($mpa); $i++)
{
echo '<div class="row"><p>' . $mpa[$i] . '</p></div>';
}
?>
<div class="row">
<textarea style="width: 99%;" rows="1" name="mcost"></textarea>
</div>
</div>
<div class="col">
<div class="row">
<b><p>Price</p></b>
</div>
<?php
for($i = 0; $i < count($pa); $i++)
{
echo '<div class="row"><p>' . $pa[$i] . '</p></div>';
}
?>
<div class="row">
<textarea style="width: 99%;" rows="1" name="price"></textarea>
</div>
</div>
<div class="col">
<div class="row">
<b><p>RRP</p></b>
</div>
<?php
for($i = 0; $i < count($rrpa); $i++)
{
echo '<div class="row"><p>' . $rrpa[$i] . '</p></div>';
}
?>
<div class="row">
<textarea style="width: 99%;" rows="1" name="rrp"></textarea>
</div>
</div>
<div class="col">
<div class="row">
<b><p>Location</p></b>
</div>
<?php
for($i = 0; $i < count($loca); $i++)
{
echo '<div class="row"><p>' . $loca[$i] . '</p></div>';
}
?>
<div class="row">
<textarea style="width: 99%;" rows="1" name="loc"></textarea>
</div>
</div>
<div class="col">
<div class="row">
<b><p>Date Created</p></b>
</div>
<?php
for($i = 0; $i < count($datea); $i++)
{
echo '<div class="row"><p>' . $datea[$i] . '</p></div>';
}
?>
<div class="row">
<input type="submit" value="Insert" style="height: 24px;" />
</div>
</div>
</form>
<div class="col">
<div class="row">
<b><p>Delete</p></b>
</div>
<?php
for($i = 0; $i < count($datea); $i++)
{
?>
<div class="row">
<form action="" method="POST">
<input type="hidden" name="id" value="<?php echo $i + 1; ?>" />
<input type="submit" value="Delete" />
</form>
</div>
<?php } ?>
</div>
</body>
</html>
提前致谢,期待您的回答,
答案 0 :(得分:0)
当您将ID放在表单中时,您有一个循环,您可以在其中添加ID:
<input type="hidden" name="id" value="<?php echo $i + 1; ?>" />
此ID似乎与数据库中的实际ID没有任何关系,它只是基于您的循环计数器。所以,我不希望它删除你要删除的内容。
答案 1 :(得分:0)
您无法像那样计算ID。
您从数据库中选择它会有更多成功(就像您在其他领域所做的那样。
答案 2 :(得分:-1)
ID是整数吗?如果是这样,请使用
$query->bind_param('i', $_POST['id']);
另外,我怀疑你还在删除错误的元素。尝试这些更改:
向查询添加ID并将变量绑定到它:
$ids = array();
// other stuff...
$query = $conn->prepare("SELECT ID, Description, MaterialsPrice, Price, RRP, Location, Date_Created FROM inventory ORDER BY ID desc");
$query->execute();
$query->bind_result($id, $desc, $mp, $p, $rrp, $loc, $date);
while($query->fetch())
{
$ids[] = $id;
$desca[] = $desc;
$mpa[] = $mp;
$pa[] = $p;
$rrpa[] = $rrp;
$loca[] = $loc;
$datea[] = $date;
}
然后当你生成删除元素时,迭代$ ids数组:
<?php
foreach($ids as $delete_id){
?>
<div class="row">
<form action="" method="POST">
<input type="hidden" name="id" value="<?php echo $delete_id; ?>" />
<input type="submit" value="Delete" />
</form>
</div>
<?php } ?>