Jump to content

Checkbox delete row command


Gilly79

Recommended Posts

Hello

 

I have a table of returned items from a SQL back end database an i would like to be able to mark them for delete... I have put a checkbox in and it works on a submit button..

 

However when it goes to the php action page i get the error message  DELETE FROM MESSAGES WHERE MessageNo = ''

 

i think its not passing the message_no from the delete step but im not sure...

 

Can anyone help me please???

 

Ive attatched the main file but putting below the peices of code i think are relevant:

 

messages.php

 

    while ($row = @ mysqli_fetch_array($result)) {

    //while($row = mysql_fetch_row($result)) {

echo "<tr>";

echo "<td >".$row["MessageNo"]."</td>";

echo "<td>".$row["MessageFrom"]."</td>";

echo "<td>".$row["MessageTo"]."</td>";

echo "<td>".$row["Subject"]."</td>";

echo "<td>".$row["Message"]."</td>";

echo "<td><input type=\"checkbox\" name=\"reply[]\" value=".$row["MessageNo"]." /></td>";

echo '<td><input type="checkbox" name="delete[]" value="'. $row['MessageNo']. '" />';

echo "</tr>";

 

delete.php script is

 

<?php

 

  // Start the session

  require_once('startsession.php');

 

  // Page requires admin rights

  //require_once('admin.php');

 

  require_once('appvars.php');

  require_once('connectvars.php');

 

// if id provided, then delete that record

$delete = $POST['delete'];

 

 

// create query to delete record

$query = "DELETE FROM MESSAGES WHERE MessageNo  = '$delete' ";

$result = mysqli_query($dbc,$query);

//$result = mysqli_query($query) or die ("Error in query: $query. ".mysqli_error());

  // see if any rows were affected

if (mysqli_affected_rows($dbc) > 0) {

  //If so , return to calling page using header function and HTTP_REFERER

    header("Location: {$_SERVER['HTTP_REFERER']}");   

  }

  else {

    // print error message

    echo "Error in query: $query".mysqli_error();

mysqli_close($dbc);

    exit;

  }

?>

 

 

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/193676-checkbox-delete-row-command/
Share on other sites

hiya

 

There is issue with the form - I just copied over a snipit of the code on messages

 

I have the following on my deletemessages.php action page

 

$delete = $GET['delete'];

 

Because the name of the delete checkbox is delete I thought that this would be what I needed to set the VAR as??

In the form you use name=delete[], which is an array, it makes sense it would be an array as how would you post multiple checkbox values without an array?

so you need to treat $delete as an array.

 

If you only have one checkbox then use name=delete, without the square brackets, and it will be a var instead of an array.

 

 

HTH

Teamatomic

 

I think I get what you are saying... However Im a bit slow

 

So the var that I will be passing to the deletemessage action page, do I need to address that any differently?

 

Or do I put the var in the deletemessage page

 

'$delete' = $GET['MessageNo']; 

 

Really sorry - its just the penny isn't dropping!

Should be some thing like that

<?php
class myclass
{
private $user_id;

public function __construct()
{    
	$this->user_id = $_SESSION['user_id'];
	if(isset($_POST['button']))
	{
		myclass::deleteEntry();
	}
	else
	{
		myclass::viewform(); 
	}
}

private function viewform()
{   
	myclass::includes();
	print "<form enctype=\"multipart/form-data\" method=\"post\" action=\"\">"
	$query = "SELECT * FROM MESSAGES WHERE MessageTo='". $this->user_id . "' "; 
	$result = mysqli_query($dbc,$query) or die ("Error in query: $query. ".mysql_error());  

	if (mysqli_num_rows($result)>0)
	{ 
		print "<table border=1>\n";
		print "<tr>\n";
		print "<td>MessageNo</td>\n";
		print "<td>MessageFrom</td>\n";
		print "<td>MessageTo</td>\n";
		print "<td>Subject</td>\n";
		print "<td>Message</td>\n";
		print "<td>Reply</td>\n";
		print "<td>Delete</td>\n";  
		print "</tr>\n";

		while ($row = @ mysqli_fetch_array($result))
		{
			print "<tr>\n";
			print "<td> . $row[\'MessageNo\'] . </td> \n";
			print "<td> . $row[\'MessageFrom\'] . </td>\n";
			print "<td> . $row[\'MessageTo\'] . </td>\n";
			print "<td> . $row[\'Subject\'] . </td>\n";
			print "<td> . $row[\'Message\'] . </td> \n";
			print "<td><input type=\"checkbox\" name=\"reply[]\" value=\" . $row['MessageNo'] . \" /></td>\n";
			print "<td><input type=\"checkbox\" name=\"delete[]\" value=\" . $row['MessageNo']. \" />\n";
			print "</tr> \n";
		} 

	    print "</table>";
		print "<label>\n";
		print "<p>\n";
		print "<input type=\"submit\" name=\"button\" id=\"button\" value=\"Submit\" />\n";
		print "</label></p>\n";
		print "</form>\n";
	}  
	else
	{  
    		print "No rows found!";  
	}  
}  

private function deleteEntry()
{   
	myclass::includes();
	$delete = $_POST['delete']; 
                $max = count($delete);
	for($i = 0; $i < $max; $i++)
	{
		$query = "DELETE FROM MESSAGES WHERE MessageNo  = '" . $delete[$i] . "'";
		$result = mysqli_query($dbc,$query);
	}

	if(mysqli_affected_rows($dbc) > 0)
	{ 
		header("Location: {$_SERVER['HTTP_REFERER']}");    
	} 
	else
	{ 
		echo "Error in query: $query" . mysqli_error();
		mysqli_close($dbc);
	    exit;
	}
}

private function includes()
{   
	require_once('startsession.php');
	require_once('connectvars.php');
}
}	

new myclass();
?>

First of all you should make sure you're using the correct form method (GET vs POST), and since I don't know which one you're using I'll simply use $_REQUEST in this example.

 

Setup your form as you already have:

<input type="checkbox" name="delete[]" value="..." />

<input type="checkbox" name="delete[]" value="..." />

<input type="checkbox" name="delete[]" value="..." />

<input type="checkbox" name="delete[]" value="..." />

 

PHP part you need:

$array = implode(',', $_REQUEST["delete"]);

$sql = "DELETE FROM messages WHERE MessageNo IN ($array)";

hi dennis - Ive included the :

 

$array = implode(',', $_REQUEST["delete"]);

$sql = "DELETE FROM messages WHERE MessageNo IN ($array)";

 

within my deletemessage page

 

HOWEVER i have amended it to a POST as that is what my form is...

 

But im now getting

 

Error in query: DELETE FROM messages WHERE MessageNo IN ()

 

I musn't be getting the vars through...

 

Ive attatched the full messagesv2 page incase ive got something wrong - it could be around the vars within my table??

 

Really sorry - ive been tearing my hair out nearly all week with this one!

 

[attachment deleted by admin]

Array ( [delete] => Array ( [0] => 8 ) [button] => Submit ) Error in query: DELETE FROM messages WHERE MessageNo IN ()

 

thats the message...

 

Ive changed it to a post as that what my form is too

 

$array = implode(',', $POST["delete"]);

print_r($array);

print_r($_POST);

 

 

check my other code above, it should work.

here is example on what your script should do

<?php
class myclass
{
public function __construct()
{
	if(isset($_POST['submit']))
	{
		myclass::result($_POST['editor']);
	}
	myclass::show();
}

private function show()
{
	print "Which text editor do you use?<br /> \n";
	print "<form method=\"post\" action=\"\"> \n";
	print "<input type=\"checkbox\" name=\"editor[]\" value=\"Notepad\" /> \n";
	print "Notepad<br /> \n";
	print "<input type=\"checkbox\" name=\"editor[]\" value=\"Scite\" /> \n";
	print "Scite<br /> \n";
	print "<input type=\"checkbox\" name=\"editor[]\" value=\"Crimson Editor\" /> \n";
	print "Crimson Editor<br /> \n";
	print "<input type=\"checkbox\" name=\"editor[]\" value=\"Dreamweaver\" /> \n";
	print "Dreamweaver<br /> \n";
	print "<input type=\"checkbox\" name=\"editor[]\" value=\"vim\" /> \n";
	print " vim<br /> \n";
	print "<input type=\"submit\" name=\"submit\" id=\"submit\" value=\"submit\" /> \n";
	print "</form>\n";
}

private function result($editor)
{
	$max = count($editor);
	for($i = 0; $i < $max; $i++)
	{
		print $editor[$i];
		print "<br>\n";
	}
}
}
new myclass();
?>

 

I get $_POST['editor']

and then counting them

and then using loop output all variables that array editor contains

 

hi just thinking about it - is it my sql command

 

$query = "DELETE FROM messages WHERE MessageNo IN ($array)";

 

as  the array is working... the error message shows the message number

 

[delete] => Array ( [0] => 7 [1] => 8 ) (i selected message 7 & 8 to be deleted...)

 

shouldn't the sql have an equals in?

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.