Forum Moderators: coopster

Message Too Old, No Replies

help with multiple delete checkbox array w/ PEAR

deleting information from mysql database through a checkbox array

         

svmf20

4:43 am on Aug 10, 2004 (gmt 0)

10+ Year Member



I have a current product list and I want to make it so that the user can delete multiple rows at once or delete nothing at all. I also need help with the input text updates based off of the name and price arrays.

Here is the code for the page:

<?php

require 'db_connect.php';

// require database connection
// which also contains the check_login.php
// script. We have $logged_in for use.

if ($logged_in == 0) {
die('Sorry you are not logged in, this area is restricted to registered members. <a href="index.php">Click here</a> to log in.');
}

// ****MAIN PART*****

if (isset($_POST['submit'])) { // if form has been submitted
$total = $db_object->query("SELECT * FROM products");
$n = $total->fetchRow();

// i was experimenting with different things, please ignore the previous 2 lines

foreach ($delete as $id) {
$del = $db_object->query("DELETE FROM products WHERE id = '$id'");
}

if (!empty($name)) {
$name = $n["name"];
while (list($id) = each($name)) {
$nameup = $db_object->query("UPDATE name FROM products SET name = '".$_POST['name']."' WHERE id = '$id'");
}
}

if (!empty($price)) {
$price = $n["price"];
while (list($id) = each($price)) {
$priceup = $db_object->query("UPDATE price FROM products SET price = '".$_POST['price']."' WHERE id = '$id'");
}
}

if (!empty($_POST['name_add']) &&!empty($_POST['price_add'])) {
$add = $db_object->query("INSERT INTO products (name, price) VALUES ('".$_POST['name_add']."', '".$_POST['price_add']."')");
}

header("Location: product_list.php");

} else {// if form hasn't been submitted

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Product List</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<style type="text/css">
body {
scrollbar-face-color: #ffffff;
scrollbar-highlight-color: #000000;
scrollbar-3dlight-color: #000000;
scrollbar-darkshadow-color: #000000;
scrollbar-shadow-color: #000000;
scrollbar-arrow-color: #000000;
scrollbar-track-color: #ffffff;
}
input.m {
background-color: #ffffff;
font-family: Tahoma;
font-size: 10pt;
color: #000077;
border-style: solid;
border-color: #000077;
}
font {
font-size: 12pt;
color: #000077
}
font.int {
font-size: 14pt;
color: #000000
}
font.nav {
font-size: 11pt;
color: #000000
}
font.copy {
font-size: 9pt;
color: #000000
}
font.spacer {
font-size: 4pt;
}
font.desc {
font-size: 11pt;
color: #000077
}
a {
text-decoration: none;
color: #000077;
}
a.copy {
text-decoration: none;
color: #000000;
}
a:hover {
text-decoration: underline;
color: #000077;
}
a:hover.copy {
text-decoration: underline;
color: #000000;
}
a:hover.nav {
text-decoration: none;
color: #000000;
}
</style>

</head>

<body bgcolor="#ffffff" bottommargin="0" leftmargin="0" rightmargin="0" topmargin="0">

<table border="0" align="left" cellspacing="0" cellpadding="0">
<tr>
<td colspan="7"><img src="top.gif" alt="ACP - Admin Control Panel" border="0"></td>
</tr>
<tr>
<td><img src="spacer1.gif" alt="" border="0"></td>
<td><a href="product_list.php"><img src="products.gif" alt="Product List" border="0"></a></td>
<td><img src="spacer2.gif" alt="" border="0"></td>
<td><a href="event_list.php"><img src="events.gif" alt="Event List" border="0"></a></td>
<td><img src="spacer3.gif" alt="" border="0"></td>
<td><a href="logout.php"><img src="logout.gif" alt="Logout" border="0"></a></td>
<td><img src="spacer4.gif" alt="" border="0"></td>
</tr>
</table>
<br><br><br><br><br><br><br><br><br><br><br><br>
<font face="tahoma" class="int">Product List</font><br><br>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<table cellspacing="0" cellpadding="2" border="1" bordercolor="#000000" align="center">
<tr>
<td align="center"><font face="tahoma">&nbsp;&nbsp;delete&nbsp;&nbsp;</font></td>
<td align="center"><font face="tahoma">&nbsp;&nbsp;product&nbsp;&nbsp;</font></td>
<td align="center"><font face="tahoma">&nbsp;&nbsp;price&nbsp;&nbsp;</font></td>
</tr>

<?php

// *****FORM ARRAYS FOR CHECKBOXES AND TEXTBOXES*****

$total = $db_object->query("SELECT * FROM products");
$num = $total->numRows();
if($num > 0) {
while($n = $total->fetchRow()) {
$id = $n["id"];
$name = $n["name"];
$price = $n["price"];
echo ("
<tr>
<td align=\"center\"><input type=\"checkbox\" name=\"delete[]\" value=\"$id\"></td>
<td><input type=\"text\" name=\"name[]\" value=\"$name\" class=\"m\" size=\"30\"></td>
<td><input type=\"text\" name=\"price[]\" value=\"$price\" class=\"m\" size=\"5\"></td>
</tr>");
}
}

?>

<tr>
<td>&nbsp;</td>
<td><input type="text" name="name_add" class="m" size="30"></td>
<td><input type="text" name="price_add" class="m" size="5"></td>
</tr>
<tr>
<td colspan="3" align="right"><input type="submit" name="submit" value="Update"></td>
</tr>
</table>
</form>
<br><br>

<?php

}

$db_object->disconnect();
// when you are done.

?>
</body>
</html>

I have been trying to get this to work for the past 30 hours and no progress. I need some help/suggestions please. I am relatively new to php and server side scripts.

THANKS!

RonPK

4:56 pm on Aug 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello & welcome svmf20,

> Here is the code for the page:

Please have a look at this page with posting guidelines [webmasterworld.com].

foreach ($delete as $id) {
$del = $db_object->query("DELETE FROM products WHERE id = '$id'");
}

This seems to be the essential part. Does $delete exist? Maybe you have to use $_POST['delete'] , depending on the configuration of PHP version.

Some hints for debugging:

1. put

error_reporting(E_ALL);
at the beginning of your script. All warnings and notices will be shown, including notices about non-existing variables.

2. if $delete exists, you can have a look at its contents with this snippet:

echo '<pre>'; 
print_r($delete);
echo '</pre>';

svmf20

5:46 pm on Aug 10, 2004 (gmt 0)

10+ Year Member



oh ok... thx for the post guidlines link.

so does the $delete variable reset when the form is submitted?

svmf20

6:50 pm on Aug 10, 2004 (gmt 0)

10+ Year Member



THANK YOU so much!

all i had to se was to put in the $_POST['delete'] and it found the delete array from before it was submitted.