I have a table which stores the categories of a list of items(e.g Clothes,Electronics, etc).i wrote a form which lists all the categories in the database and gives the user the option to delete them as he wishes.
Problem:
When i try to delete the first record shown, the last record displayed gets deleted instead of the first.The problem does not occur when i try deleting the other records displayed(e.g 2nd,3rd,4th, etc)
Example:
+---------------------+
| Category |
-----------------------
| Clothes | Delete | <---Tried to delete this
-----------------------
| Computers| Delete |
-----------------------
| Games | Delete | <--This gets deleted
-----------------------
My Form/Processing Code:
<?php
if(!isset($_SESSION))
{
session_start();
}
if($_SESSION['auth']!=="yes")
{
echo"You are not authorised to view this page. Please login <a href='login.php'>here</a>";
exit();
}
?>
<?php
if($_POST['delcatsubmit']=="Delete")
{
include("cxn.inc");
var_dump($_POST);
$id=$_SESSION['BizID'];
$catid=$_POST['delcatid'];
$delcat=$cxn->prepare("DELETE FROM `testdb`.`itemcat` WHERE `BusinessID`=:id AND `ID`=:catid");
$delcat->bindValue(":id",$id);
$delcat->bindValue(":catid",$catid);
$delcat->execute();
echo"Category Deleted";
}
?>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?php
include("cxn.inc");
$id=$_SESSION['BizID'];
$viewcat=$cxn->prepare("SELECT * FROM `testdb`.`itemcat` WHERE `BusinessID`= :id");
$viewcat->bindValue(":id",$id);
$viewcat->execute();
echo'<form name="delcategory" id="delcategory" action="delcategory.php" method="POST" >';
echo"<table border='1'>";
echo"<tr>";
echo"<td colspan='2'>";
echo"Category";
echo"</td>";
echo"</tr>";
while($getcat=$viewcat->fetch(PDO::FETCH_ASSOC))
{
echo"<tr>";
echo"<td>";
$cat=$getcat['ItemCat'];
$delcatid=$getcat['ID'];
echo"$cat";
echo"</td>";
echo"<td>";
echo"<input type='hidden' name='delcatid' id='delcatid' value='$delcatid' />";
echo"<input type='submit' name='delcatsubmit' id='delcatsubmit' value='Delete' />";
echo"</td>";
echo"</tr>";
}
echo"</form>";
echo"</table>";
?>
</body>
</html>
Results of VAR_DUMP($_POST)
array(2) { ["delcatid"]=> string(1) "3" ["delcatsubmit"]=> string(6) "Delete" } Category Deleted
Questions:
1.Why is the id of the last item displayed being deleted/returned when i try to delete the first item?
2.I get the message:
PHP Notice: Undefined index: delcatsubmit
when i delete a category.I gether that this is due to $_POST['delcatsubmit'] being undefined the first time the form is loaded.Is there any way to correct/remove the notice short of suppressing it with @ ?
I appreciate any inputs and suggestions
Thanks!
P.S. I'm using MYSQL and UNIFORM SERVER
Solved it.
i was using the same name attribute on every row in the form, so they are being overridden and it's using the last one.
What i did to solve it was wrap a new form around every iteration of the submit button as it was being looped.Hope this helps anyone else who encounters the same problem.
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?php
include("cxn.inc");
$id=$_SESSION['BizID'];
$viewcat=$cxn->prepare("SELECT * FROM `testdb`.`itemcat` WHERE `BusinessID`= :id");
$viewcat->bindValue(":id",$id);
$viewcat->execute();
echo"<table border='1'>";
echo"<tr>";
echo"<td colspan='2'>";
echo"Category";
echo"</td>";
echo"</tr>";
while($getcat=$viewcat->fetch(PDO::FETCH_ASSOC))
{
echo"<tr>";
echo"<td>";
$cat=$getcat['ItemCat'];
$delcatid=$getcat['ID'];
echo"$cat";
echo"</td>";
echo"<td>";
echo"$delcatid";
echo"</td>";
echo"<td>";
echo'<form name="delcategory" id="delcategory" action="delcategory.php" method="POST" >';
echo"<input type='hidden' name='delcatid' id='delcatid' value='$delcatid' />";
echo"<input type='submit' name='delcatsubmit' id='delcatsubmit' value='Delete' />";
echo"</form>";
echo"</td>";
echo"</tr>";
}
echo"</table>";
?>
</body>
</html>