need help building an admin page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aj2000
Forum Newbie
Posts: 11
Joined: Sun May 30, 2004 10:12 pm

need help building an admin page

Post by aj2000 »

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>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

aj2000
Forum Newbie
Posts: 11
Joined: Sun May 30, 2004 10:12 pm

Post by aj2000 »

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>
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post by Paddy »

This is quite jumbled. Could I offer a different a different solution?

Have three different pages. One for add, one for edit, and one for delete.

This way the delete one could have check boxes so you can delete many items at once.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

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); 
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

ol4pro's right, mysql implicitly closes the connection.
aj2000
Forum Newbie
Posts: 11
Joined: Sun May 30, 2004 10:12 pm

thanks for the imput

Post by aj2000 »

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!
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

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
Post Reply