Page 1 of 1

checkboxes to delete MySQL rows.

Posted: Mon Nov 20, 2006 10:22 am
by boo_lolly
i've got a little search/results page. the results are displayed in an HTML table. what i'd like to do, is add a checkbox to each row in the HTML table, and a button at the very bottom of the table that says 'DELETE'. if the user clicks it, it will delete all the rows in the SQL table that were 'checked', and then display the new results (without the deleted rows). each SQL table row is outputted as an HTML table row. each SQL row has a unique ID. i assume this is the variable to be used in order to execute the function. can i get any suggestions or direction? thanks in advanced.

Posted: Mon Nov 20, 2006 10:57 am
by volka
Make the ID the value of the checkbox.
Append [] to the name of the checkboxes, e.g. name="xyz[]". example:

Code: Select all

<html>
	<head>
		<title>checkbox test</title>
	</head>
	<body>
<?php	
if ( isset($_POST['xyz']) && is_array($_POST['xyz']) ) {
	foreach( $_POST['xyz'] as $xyz ) {
		echo '		checkbox value: ', (int)$xyz, "<br />\n";
	}
}
?>
		<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
			<div>
				1 <input type="checkbox" name="xyz[]" value="1" /><br />
				3 <input type="checkbox" name="xyz[]" value="3" /><br />
				7 <input type="checkbox" name="xyz[]" value="7" /><br />
				8 <input type="checkbox" name="xyz[]" value="8" /><br />
				9 <input type="checkbox" name="xyz[]" value="9" /><br />
				<input type="submit" />
			</div>
		</form>
	</body>
</html>

Posted: Mon Nov 20, 2006 12:09 pm
by boo_lolly
can you explain the if (isset) part?

since i'm trying to delete the rows that are 'checked', inside the foreach loop i would write the SQL command to delete that row, correct?

Posted: Mon Nov 20, 2006 1:04 pm
by volka
boo_lolly wrote:can you explain the if (isset) part?
If all works as expected (remember: client data can always be manipulated) there is an element $_POST['xyz'] and it's an array containg the values of all checked checkboxes (if at least one was checked. If not then there is no $_POST[xyz]).
boo_lolly wrote:since i'm trying to delete the rows that are 'checked', inside the foreach loop i would write the SQL command to delete that row, correct?
deleting records according to the values in the POST array, yes.

Posted: Mon Nov 20, 2006 2:25 pm
by boo_lolly
what if i wanted to have a button that allowed a user to ADD a row of data to my SQL table? i know how to write the input fields and give them all variable names and all that. but i'm a little confused about one part... when the user fills in the input fields then clicks ADD, i want it to redirect them to a page where it shows ALL the sql table rows of data (which i have already made).

how would this page be designed? input fields, a form. should it take them to a page, saying something like "thank you for your request. if you are not redirected in 5 seconds, please click the link below..."

or can it execute the INSERT and redirect them all at the same time?

one more thing. when this new row is entered, there must be a function executed that generates a random sting of characters and numbers. this string is given to the row of data under the 'uniqueID' column. ALSO, at the SAME time, it needs to CREATE TABLE where the name of that sql table is the 'uniqueID'

Posted: Mon Nov 20, 2006 2:27 pm
by volka
boo_lolly wrote:how would this page be designed? input fields, a form. should it take them to a page, saying something like "thank you for your request. if you are not redirected in 5 seconds, please click the link below..."

or can it execute the INSERT and redirect them all at the same time?
It's both possible.
Take a look at how this board handles it. Writing a new post is adding a new record to a database.

Posted: Mon Nov 20, 2006 3:12 pm
by boo_lolly
awesome! so, where exactly does the SQL query go? assuming i wanted the button to work and instantly send them back to the results page (with no middle-man page in between).