我已经制作了一个可以删除项目的页面。它似乎工作得很好,但是当我按下删除按钮时,它总是删除该用户上传的第一个项目而不是我选择的那个...这是代码:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Produktai</title>
<link href="CSS/stilius.css" rel="stylesheet" type="text/css"/>
<link href="CSS/menu.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<?php require("includes/validate.php");?>
<?php require("includes/stilius.php");?>
<?php
$rec_limit = 4;
require("includes/connection_to_db.php");
$sql = "SELECT COUNT(P_ID) FROM prekės";
$retval = mysql_query($sql);
if(! $retval)
{ die('13 line. Nepavyko gauti duomenų: ' . mysql_error()); }
$row = mysql_fetch_array($retval, MYSQL_NUM);
$rec_count = $row[0];
if( isset($_GET{'page'} ) )
{
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page;
}
else
{
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$sql = "SELECT prekės.* , CONCAT(vartotojai.name) as v_name
FROM prekės
LEFT JOIN vartotojai
ON vartotojai.V_ID=prekės.V_ID
ORDER BY prekės.date
DESC LIMIT $offset, $rec_limit";
$retval = mysql_query($sql);
if(! $retval )
{
die('38 line. Nepavyko gauti duomenų: ' . mysql_error());
}
?>
<?php
if(isset($_POST['deleteItem']))
{
$P_ID = $_POST['P_ID'];
if(mysql_query("DELETE
FROM prekės
WHERE P_ID = '$P_ID'"))
{
header("Location: products.php");
}
else {echo "Prekės nepavyko ištrinti: " .mysql_error();}
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<?php while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) : ?>
<?php $image = '<td><img src="data:image/jpeg;base64,'.base64_encode($row['image']).'" name="pix" width="270" height="200"/></td>' ?>
<table class="two">
<th><?php $pav = $row['name']; echo "<a href=\"$_SERVER[PHP_SELF]?product=$pav\">$pav</a>" ?></th>
<th>Prekės savininkas: <?php echo $row['v_name']?></th>
<?php if($row['V_ID'] != $_SESSION['user_id']) : ?>
<th><select name="mano_prekės" value="<?php echo $m_product;?>">
<option value="">Pasiūlykite savo prekę</option>
<option disabled>---------------------------------</option>
<?php require ("includes/exchange.php") ?>
</select>
<input type="button" name="exchange" id="exchange" value="Mainyti">
</th>
<?php elseif($row['V_ID'] = $_SESSION['user_id']) : ?>
<th>
<input name="P_ID" type="hidden" id="P_ID" value=<?php echo $row['P_ID']?>>
<input type="button" value="Redaguoti" id="edit_item">
<input type="submit" name="deleteItem" id="deleteItem" value="Ištrinti" onclick="return confirm('Ar tikrai norite ištrinti šią prekę?')"></th>
</th>
<?php endif; ?>
<tr>
<?php echo $image?>
<td><textarea disabled style="resize:none; background-color:white" name="about" rows="12" cols="65"><?php echo $row['specs']?></textarea><td>
</table>
<?php endwhile; ?>
</form>
<?php
if( $page > 0 )
{
$last = $page - 2;
echo "<a href=\"$_SERVER[PHP_SELF]?page=$last\" style=float:left>Atgal</a>";
echo "<a href=\"$_SERVER[PHP_SELF]?page=$page\" style=float:right>Pirmyn</a>";
}
else if( $page == 0 )
{
echo "<a href=\"$_SERVER[PHP_SELF]?page=$page\" style=float:right>Pirmyn</a>";
}
else if( $left_rec < $rec_limit )
{
$last = $page - 2;
echo "<a href=\"$_SERVER[PHP_SELF]?page=$last\" style=float:left>Atgal</a>";
}
mysql_close();
?>
</body>
</html>
如果你看到我弄错了,请告诉我,因为这让我很头疼..
答案 0 :(得分:0)
问题是您的表单包含所有项目,因此在提交时会发送最后一个ID(这是第一个上传的项目,因为您按降序排序)。
您需要以自己的形式包装每一组输入。
答案 1 :(得分:0)
您可以在单个表单中使用P_ID
来尝试两种解决方案
解决方案-1:每个项目的单独表单。 (每次)
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table class="two">
<th><?php $pav = $row['name']; echo "<a href=\"$_SERVER[PHP_SELF]?product=$pav\">$pav</a>" ?></th>
<th>Prekės savininkas: <?php echo $row['v_name']?></th>
<?php if($row['V_ID'] != $_SESSION['user_id']) : ?>
<th><select name="mano_prekės" value="<?php echo $m_product;?>">
<option value="">Pasiūlykite savo prekę</option>
<option disabled>---------------------------------</option>
<?php require ("includes/exchange.php") ?>
</select>
<input type="button" name="exchange" id="exchange" value="Mainyti">
</th>
<?php elseif($row['V_ID'] = $_SESSION['user_id']) : ?>
<th>
<input name="P_ID" type="hidden" id="P_ID" value=<?php echo $row['P_ID']?>>
<input type="button" value="Redaguoti" id="edit_item">
<input type="submit" name="deleteItem" id="deleteItem" value="Ištrinti" onclick="return confirm('Ar tikrai norite ištrinti šią prekę?')"></th>
</th>
<?php endif; ?>
<tr>
<?php echo $image?>
<td><textarea disabled style="resize:none; background-color:white" name="about" rows="12" cols="65"><?php echo $row['specs']?></textarea><td>
</table>
</form>
解决方案2:将删除视为简单按钮并在其上应用onclick事件。
<input type="button" name="deleteItem" id="deleteItem" value="Ištrinti" onclick="if(confirm('Ar tikrai norite ištrinti šia preke?')){window.location.href='<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>?deleteItem=true&P_ID=<?php echo $row['P_ID']?>';}">