checkboxes to delete MySQL rows.
Moderator: General Moderators
checkboxes to delete MySQL rows.
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.
Make the ID the value of the checkbox.
Append [] to the name of the checkboxes, e.g. name="xyz[]". example:
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>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:can you explain the if (isset) part?
deleting records according to the values in the POST array, yes.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?
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'
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'
Last edited by boo_lolly on Mon Nov 20, 2006 2:33 pm, edited 1 time in total.
It's both possible.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?
Take a look at how this board handles it. Writing a new post is adding a new record to a database.