Hi, im trying to build an admin page where the user can add, delete or edit an entry. So far ive had no problem writing the code that adds an entry, however...since each entry has with it seperate delete and edit button i wrote a script that would generate the buttons and appended a number onto the name of the delete button ie...delete0, delete1 and so on. My problem is how can i have the user delete an entry..so if the user clicks on the 7th delete button/entry.. that particular row/entry would be deleted. Ive attached my code and a link the site that im working on
http://www.financialshopper.com/categories.php
and heres the code:
<!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");
$query_delete = "SELECT MAX(C_ID) FROM category";
$result_delete = mysql_query($query_delete);
//If ($delete2 == "delete") {
//echo("entry was deleted");
//}
//IF USER CLICKS ON ADD BUTTON, NEW CUSTOMER ID IS ADDED AND FIELDS ARE THEN SUBMITTED TO DATABASE
If ($add == "add"){
$query_id = "SELECT MAX(C_ID) FROM category";
$result_id = mysql_query($query_id);
for ($i = 0; $i < mysql_num_rows($result_id); $i++) {
$rowarray_id = mysql_fetch_row($result_id); //)
$new_c_id = $rowarray_id[$i] + 1;
//echo($new_c_id);
}
//$c_image = (substr($c_image,-3));
$query_add = "INSERT INTO category (c_id, c_name, c_description, c_image) VALUES ('" . $new_c_id . "', '" . $c_name . "', '" . $c_description . "', '" . $c_image . "')";
$result_add = mysql_query($query_add);
}
$query_cat = "SELECT 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>
<form action="categories.php" method="post" name="categories" id="categories">
<?
for ($i = 0; $i < mysql_num_rows($result_cat); $i++) {
echo("<TR>");
$rowarray = mysql_fetch_row($result_cat);
for ($j=0; $j < mysql_num_fields($result_cat); $j++) {
$image = (substr($rowarray[$j],-3));
//echo ($image);//echo($rowarray[$i]);
if ($image == "gif") {
echo("<TD><img src=" . $rowarray[$j] . " border=0></TD>");
} else {
echo("<TD>" . $rowarray[$j] . "</TD>" );
}
}
//echo("hello");
echo("<TD colspan=2><input type=submit name=delete" . $i . " value=delete width=15><input type=submit name=edit" . $i . " value=edit width=15></TD><TD><img src=images/clear.gif width=1 height=1 border=0></td>");
echo("</TR>");
//$deletenum = "delete" . $i;
$max = count
//If ($delete =="delete"){
//echo("works");
}
}
//echo(delete[$i]);
//}
?>
<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>
need help building an admin page
Moderator: General Moderators
sorry!..here is the code again
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");
$query_delete = "SELECT MAX(C_ID) FROM category";
$result_delete = mysql_query($query_delete);
//If ($delete2 == "delete") {
//echo("entry was deleted");
//}
//IF USER CLICKS ON ADD BUTTON, NEW CUSTOMER ID IS ADDED AND FIELDS ARE THEN SUBMITTED TO DATABASE
If ($add == "add"){
$query_id = "SELECT MAX(C_ID) FROM category";
$result_id = mysql_query($query_id);
for ($i = 0; $i < mysql_num_rows($result_id); $i++) {
$rowarray_id = mysql_fetch_row($result_id); //)
$new_c_id = $rowarray_id[$i] + 1;
//echo($new_c_id);
}
//$c_image = (substr($c_image,-3));
$query_add = "INSERT INTO category (c_id, c_name, c_description, c_image) VALUES ('" . $new_c_id . "', '" . $c_name . "', '" . $c_description . "', '" . $c_image . "')";
$result_add = mysql_query($query_add);
}
$query_cat = "SELECT 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>
<form action="categories.php" method="post" name="categories" id="categories">
<?
for ($i = 0; $i < mysql_num_rows($result_cat); $i++) {
echo("<TR>");
$rowarray = mysql_fetch_row($result_cat);
for ($j=0; $j < mysql_num_fields($result_cat); $j++) {
$image = (substr($rowarray[$j],-3));
//echo ($image);//echo($rowarray[$i]);
if ($image == "gif") {
echo("<TD><img src=" . $rowarray[$j] . " border=0></TD>");
} else {
echo("<TD>" . $rowarray[$j] . "</TD>" );
}
}
//echo("hello");
echo("<TD colspan=2><input type=submit name=delete" . $i . " value=delete width=15><input type=submit name=edit" . $i . " value=edit width=15></TD><TD><img src=images/clear.gif width=1 height=1 border=0></td>");
echo("</TR>");
//$deletenum = "delete" . $i;
$max = count
//If ($delete =="delete"){
//echo("works");
}
}
//echo(delete[$i]);
//}
?>
<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>Code: Select all
# not relevant for youre question, but i did notice this at the end of
# youre script.
# you do not have to close a mysql link for as far as i know
<?
mysql_close($link);
?>thanks for the imput
thankyou very much for the input...especially the idea of putting check boxes next to the rows that need to be deleted..ill try that method and see how it comes out..again, thankyou for your input!
if you want to delete the items via checkbox
i wrote a lil snipplet to do so and did some explaining.. not 100% 'professional' but will give you a good start.
viewtopic.php?t=20198
i wrote a lil snipplet to do so and did some explaining.. not 100% 'professional' but will give you a good start.
viewtopic.php?t=20198