Page 1 of 1

Insert and DELETE with Multiple Checkboxes

Posted: Wed Aug 01, 2007 10:51 am
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

Posted: Wed Aug 01, 2007 10:56 am
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.

Posted: Wed Aug 01, 2007 10:58 am
by tamyrahlmack
Thanks for your reply.
Should this go before or after the submit button?

Posted: Wed Aug 01, 2007 11:03 am
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.

Posted: Wed Aug 01, 2007 11:13 am
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')
}

Posted: Wed Aug 01, 2007 11:23 am
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...

Posted: Wed Aug 01, 2007 11:25 am
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
  }
}

Posted: Wed Aug 01, 2007 11:31 am
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?

Posted: Wed Aug 01, 2007 11:47 am
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.