Insert and DELETE with Multiple Checkboxes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tamyrahlmack
Forum Newbie
Posts: 4
Joined: Wed Jul 25, 2007 7:25 am

Insert and DELETE with Multiple Checkboxes

Post by tamyrahlmack »

Hello, I currently have a page with a repeated region that has information from a table with a checkbox proceed each line.
My objective is for the admin (on the frontend) to be able to check multiple boxes and when they press the submit button, the information that is stored in the database should be copied to a new database and deleted from the existing database.
What I currently have is this:

Code: Select all

mysql_select_db ($database, $Trial);
$query = "SELECT * FROM contactinfo";
$Record = mysql_query($query, $Trial) or die(mysql_error());
$row = mysql_fetch_assoc($Record);
totalrows = mysql_num_rows($Record);
<form id= "form" name="form" method="post" action="">
<?php do { ?>
<input type = "checkbox" name= "checkbox" id= "checkbox" />
<?php echo $row['ID']; ?>, <?php echo $row['FNAME'], ?>
<?php echo $row['address']; ?>
<?php } while ($row = mysql_fetch_assoc($Record) ?>
<input type="submit" name="button" id="button" value="submit"/>
[/i]
I am not sure how to check whether the checkbox is checked and then where to insert the code (before or after the submit button). I have a basic algorithm but I have no ide how to implement it in PHP. I also know what the SQL statement would be. I have searched and read plenty of books but I just don't know how to put the info together.

Any help would be greatly appreciated
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

On your HTML you should have this in each checkbox (or similar):

Code: Select all

<input type="checkbox" value="selected">
Then, You'll need to call the checkboxes using POST.

eg.

Code: Select all

$check1 = $_POST['check1'];

//Then check to see if they where checked:
if ($check1 == "selected"){
echo "Checkbox 1 says: I WAS SELECTED";
}
else {
echo "Checkbox 1 says: I WAS NOT SELECTED";
}
When it comes time to Move database entries from one database to another, you will have to SELECT all the data you wish to be moved, store it in variables, then close the original database connection, and connect to the other database, and INSERT the data back in, assuming the tables etc are already created.
Last edited by iknownothing on Wed Aug 01, 2007 11:01 am, edited 1 time in total.
tamyrahlmack
Forum Newbie
Posts: 4
Joined: Wed Jul 25, 2007 7:25 am

Post by tamyrahlmack »

Thanks for your reply.
Should this go before or after the submit button?
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

tamyrahlmack wrote:Thanks for your reply.
Should this go before or after the submit button?
if it is on the same page I suggest identifying whether or not it has been submitted:

Code: Select all

if(isset($_POST['yesihavebeensubmitted'])){  // 'yesihavebeensubmitted' being the name of a form element

// PHP STUFF GOES HERE

}
If you do that, it can go before or after the submit button.
tamyrahlmack
Forum Newbie
Posts: 4
Joined: Wed Jul 25, 2007 7:25 am

Post by tamyrahlmack »

Okay so now I have a couple questions...

Does this need to go into a loop so that it can check for all the checked data?
There could be 1 listing or 1000 so I would need to check for all of the checked columns.

Would it be okay for me to replace the

Code: Select all

echo "checkbox 1 is selceted"
with my query state like

Code: Select all

$check1 = $_POST['check'];
if($check1 == "selected")
{
 implement this query (INSERT INTO table SELECT * FROM table1 WHERE ID = 'ID')
implement this query (DELETE FROM table WHERE ID = 'ID')
}
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

Yes, except the Statements aren't written correctly...

Code: Select all

//  CONNECT TO OLD DATABASE


// GET STUFF FROM OLD DATABASE
$result = mysql_query("SELECT * FROM old_table");

while($row = mysql_fetch_array($result))  // THIS WILL LOOP TO OBTAIN ALL ENTRIES REQUIRED
  {
  $variable_1[] =  $row['tablerow1'];  //  THIS SHOULD BE AN ARRAY TO MAKE IT EASIER TO MOVE MASS DATA, BUT I AM UNSURE OF HOW TO DO THIS, I SUGGEST GOOGLING: "SELECT into an array php"
  }

mysql_query("DELETE FROM old_table WHERE tablerow1='$variable_1'");

//  DISCONNECT FROM DATABASE
//  CONNECT TO NEW DATABASE

mysql_query("INSERT INTO new_table (tablerow1) VALUES ('$variable_1')");
Something like that...
Last edited by iknownothing on Wed Aug 01, 2007 11:27 am, edited 1 time in total.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

I would rather use:

Code: Select all

echo "<input ='checkbox' name='regions[".$id."]'>"
for generating the FORM.

Then, I would use:

Code: Select all

$regions = $_REUQEST['regions'];

foreach ($regions as $id => $checked)
{
  if ($checked == 'on')
  {
      // move $id to the database and delete from current
  }
}
There are 10 types of people in this world, those who understand binary and those who don't
tamyrahlmack
Forum Newbie
Posts: 4
Joined: Wed Jul 25, 2007 7:25 am

Post by tamyrahlmack »

I'm hoping that at any one time, the ADMIN could transfer all of the files in or database or just one. Do you think that this could prove to be a problem?
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

If it gets to be a large database, and for some odd reason, the server crashes mid-transfer, you could lose the lot, the server would most likely have a backup, but who knows how long the restoration stage could take.

The transfers might prove quite slow when it gets large too.
Post Reply