if you view the source..you will see each delete button has a unique name..ie delete1, delete2..etc...this is done by appending the rows incremental value. Also, each form passes a value..can be Delete=0, 1, 2 etc...when the page loads and you click on a delete button..it always passes a zero..i dont know why its doing this
to understand try clicking on a delete button at http://www.financialshopper.com/categories_test.php
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Supreme Liquors</title>
<script language="javascript">
function Category_Check(c_name, c_description, c_image) {
if (c_name == "") {
alert("Please enter a catogory title")
return false;
}
if (c_description == "") {
alert("Please enter a description")
return false;
}
if (c_image == "") {
alert("Please upload an image")
return false;
}
return true;
}
</script>
</head>
<body>
<?php
//INITIAL DB CONNECTION
$link = mysql_connect("mysql", "anujjain", "A4494J");
mysql_select_db("supremeliquors");
//IF DELETE EXISTS THEN OUTPUT THE VARIABLE
if(isset($delete)) {
echo($delete);
//$query_delete = "DELETE FROM category where c_id = '" . $delete . "'";
//$result_delete = mysql_query($query_delete);
}
// RETRIEVE THE ID, IMAGE AND CATEGORY FROM DB
$query_cat = "SELECT c_id, c_image, c_name FROM category";
$result_cat = mysql_query($query_cat);
?>
<TABLE width="740" cellpadding="0" cellspacing="0" border="1">
<TR><TD colspan="5" align="center">Logo</TD></TR>
<?
//GENERATE A TALBE WITH ID, IMAGE, CATEGORY AND DELETE BUTTON FOR EACH RECORD
for ($i = 0; $i < mysql_num_rows($result_cat); $i++) {
echo("<TR>");
$rowarray = mysql_fetch_row($result_cat);
for ($j=1; $j < mysql_num_fields($result_cat); $j++) {
$image = (substr($rowarray[$j],-3));
if ($image == "gif") {
echo("<TD><img src=" . $rowarray[$j] . " border=0></TD>");
} else {
echo("<TD>" . $rowarray[$j] . "</TD>" );
}
}
//GENERATES A FORM FOR EACH DELETE BUTTON AND PASSES DELETE VARIABLE
echo "<form action=categories_test.php?delete=" . $i . " method=POST><TD colspan=2><input type=submit name=delete" . $i . " value=delete></TD><TD><img src=images/clear.gif width=1 height=1 border=0></td>";
echo("</TR>");
}
?>
<form action="categories.php" method="post" name="categories" id="categories">
<TR><td width="75">Category Title</td><TD colspan="3"><input type="text" name="c_name" size="25"></TD><TD><img src="images/clear.gif" alt="" width="1" height="1" border="0"></td></TR>
<TR><td width="75">Description</td><TD colspan="3"><textarea cols="25" rows="4" name="c_description"></textarea></TD><TD><img src="images/clear.gif" alt="" width="1" height="1" border="0"></td></TR>
<TR><td width="75">Category Picture</td><TD colspan="3"><input type="file" name="c_image" size="50" accept="image/gif"></TD><TD><input type="submit" name="add" value="add" width="5" height="5" onclick="return Category_Check(c_name.value, c_description.value, c_image.value);"></td></TR>
<input type="hidden" name="upload" value="upload">
</FORM>
</TABLE>
<?
mysql_close($link);
?>
</body>
</html>