Jump to content

Checkboxes for deleting lists of items from MySQL databases


jacomus

Recommended Posts

I have tried about a dozen different tutorials and have not been able to figure out why none of the examples seem to be working? One thing I have figured is that the examples all seem to be incomplete, there are undeclared variables within the code (I find $i is a common one). I am fairly new to PHP, but is it possible that the syntax is different for later versions of PHP when it comes to this? I am a little surprised I have not been able to find anything that works on the version that I have (PHP Version 4.4.7).

 

Right now I would say I am completely lost, essentially what I have been trying to do is this

 

1) Creating a list in PHP populated from a MySQL database, and the checkbox code placed within so that it will appear beside every item that appears.

 

2) PHP code that will allow the user to check off and delete items in a similar way to an Email deletion system.

 

Most of the examples I have seen have been relatively simple, but none seem to work when I test the actual examples out.

Ok, I'm not sure how basic I should make this, but here goes:

 

Firstly, you need to create a connection to your database. There are lots of tutorials on this - try http://www.w3schools.com/PHP/php_mysql_connect.asp

 

You then need to create a SELECT query to get the data you need from the database.

 

You then need to loop through all the results from your SELECT query

 

Let's get you to that point first.

Try to do the above - if you don't know how to create a query, just search for "mysql SELECT query" and for loops, "php loop".

 

Give it a go and let me know how you do.

Alright, I do have all that stuff in there. It works up to the point where clicking the checkboxes and the delete button do not function. Here is what I do have so far for my sample code:

 

<?php
$localhost = '';
$username = '';
$password = '';
$stuff = $HTTP_POST_VARS['stuff'];
$del_id = $checkbox[$i];
$sql = "DELETE FROM stuff WHERE items;";

?>

<form action="testitembase2.php" method=post>
</tr>
<tr>
<td>stuff</td>
<td align="Center"><input type="text" name="stuff" size="30" maxlength="30"></td>
</tr>
<tr>
	<td colspan="2" align=center"><input type="submit" value="Submit Order"></td>
</tr>
</table>
</form>
<?php
mysql_connect ($localhost, $username, $password);
mysql_select_db (itemmanagement);

mysql_query ("INSERT INTO stuff(items) VALUES 
('$stuff');
");



$stuff2 = mysql_query ( "select * from stuff;");
echo "<table border='1' align='left'><tr>";	
	echo "<td bgcolor=#cccccc><B>items stuff:</b><BR></td></tr\n>";
	while($row = mysql_fetch_array( $stuff2 )){
	echo "<tr>";
	echo "<td>{$row['items']}";
?>
<bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['items']; ?>"><?
	echo '<br /></td>';
	echo "</tr\n>";
	}
	echo "</tr\n>";
	?><tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr><?php
if($delete){
$stuff3 = mysql_query ($sql);
$count = mysql_num_rows($stuff3);
for($i=0;$i<$count;$i++){

$result = mysql_query("DELETE FROM stuff WHERE items;");

}

if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=testitembase2.php\">";



}}
?>

 

Here's working code. Only the names have been changed to protect the innocent. And it's not complete - it still needs some validation. Create a page called test.php, copy and past this, and enter your server/pass info.

 

<?php
$server = "xxxx";
$user = "xxxx";
$pswd = "xxxx";
$db = "xxxx";
$dbConn = mysql_connect("$server", "$user", "$pswd") or die("Could not connect".mysql_error());
mysql_select_db("$db") or die("Couldn't open $db. MySQL Error: ".mysql_error());
$tableName = "mytable";

//Delete Start
if(isset($_POST['delcheck'])) {
$del = $_POST['delcheck'];

foreach ($del as $temp) {
	$querydelete = "DELETE from $tableName WHERE pid = '$temp'";
	//mysql_query($querydelete) or die(mysql_error());	
	print "$querydelete<br />";
}
}
//Delete End
?>
<html>
<head>
</head>
<body>

<form action="test.php" method="post" name="Delete">
<table border="0" cellpadding="10" cellspacing="10">
<tr>
<th class="head">Last</th>
<th class="head">First</th>
<th class="head">Delete</th>
</tr>

<?php
$selectmain = "SELECT pid, first, last FROM $tableName WHERE pid IS NOT NULL ";
$result = mysql_query($selectmain) or trigger_error("SQL", E_USER_ERROR);
$sqlprint = $selectmain;
while($row = mysql_fetch_row($result))	{
$pid	= $row[0];	//assign a var to each element in the array
$first	= $row[1];	//assign a var to each element in the array
$last 	= $row[2];	//assign a var to each element in the array

print "
<tr>
	<td valign=\"top\" nowrap>$last  </td>
	<td valign=\"top\" nowrap>$first  </td>
	<td valign=\"top\" nowrap><input type=\"checkbox\" name=\"delcheck[]\" value=\"$pid\"></td>
</tr>
";
}
?>

<tr><td colspan="3"><input type="submit" value="Delete" name="btnDelete" onClick="return confirm('Are you sure you want to delete this?')" ></td></tr>
</table>
</form>

</body>
</html>
<?php
mysql_close($dbConn);
?>

Hi benphp, I certainly appreciate the effort, but I have been running into lots of problems with that code. It is as though my version of php lacks A LOT of features even though being a fairly recent version.

 

I created a database and such for this, but I have found that I am getting a parse error on every line. I worked down the code replacing several of the lines, (IE. $db = "xxxx" had to be changed to: $db = $HTTP_POST_VARS['xxxx'];). I got to line 11 (if(isset($_POST['delcheck'])) {) which is where I am stuck.

 

Here's working code. Only the names have been changed to protect the innocent. And it's not complete - it still needs some validation. Create a page called test.php, copy and past this, and enter your server/pass info.

 

<?php
$server = "xxxx";
$user = "xxxx";
$pswd = "xxxx";
$db = "xxxx";
$dbConn = mysql_connect("$server", "$user", "$pswd") or die("Could not connect".mysql_error());
mysql_select_db("$db") or die("Couldn't open $db. MySQL Error: ".mysql_error());
$tableName = "mytable";

//Delete Start
if(isset($_POST['delcheck'])) {
$del = $_POST['delcheck'];

foreach ($del as $temp) {
	$querydelete = "DELETE from $tableName WHERE pid = '$temp'";
	//mysql_query($querydelete) or die(mysql_error());	
	print "$querydelete<br />";
}
}
//Delete End
?>
<html>
<head>
</head>
<body>

<form action="test.php" method="post" name="Delete">
<table border="0" cellpadding="10" cellspacing="10">
<tr>
<th class="head">Last</th>
<th class="head">First</th>
<th class="head">Delete</th>
</tr>

<?php
$selectmain = "SELECT pid, first, last FROM $tableName WHERE pid IS NOT NULL ";
$result = mysql_query($selectmain) or trigger_error("SQL", E_USER_ERROR);
$sqlprint = $selectmain;
while($row = mysql_fetch_row($result))	{
$pid	= $row[0];	//assign a var to each element in the array
$first	= $row[1];	//assign a var to each element in the array
$last 	= $row[2];	//assign a var to each element in the array

print "
<tr>
	<td valign=\"top\" nowrap>$last  </td>
	<td valign=\"top\" nowrap>$first  </td>
	<td valign=\"top\" nowrap><input type=\"checkbox\" name=\"delcheck[]\" value=\"$pid\"></td>
</tr>
";
}
?>

<tr><td colspan="3"><input type="submit" value="Delete" name="btnDelete" onClick="return confirm('Are you sure you want to delete this?')" ></td></tr>
</table>
</form>

</body>
</html>
<?php
mysql_close($dbConn);
?>

 

It's a single PHP page, the submit button leads to entering info from a text box into the database which will then display on the page. testitembase2.php is the only page I am using currently for the purposes of this testing (I am working on a much larger project, but this particular bit is just a test thing).

 

The $delete thing, I am not sure, I was incorporating some example code from various tutorial websites. I added in

$delete = $HTTP_POST_VARS['delete'];

to no avail. I am not really sure what to do here.

 

When you click on submit, the form is trying to process the page testitembase2.php.

Is that the name of the page this code is on?

 

Where do you set the value of $delete?

 

if($delete){
$stuff3 = mysql_query ($sql);

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.