Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert into new table and delete from existing Multiple Checkboxes

Status
Not open for further replies.

darylesha

Programmer
Aug 1, 2007
2
US
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:

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 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.
 
1.Give your form an action, usually the name of the file that will process the form. in this case the same one that contains the form.
2a. make the checkboxes an array:
Code:
<input type = "checkbox" name= "checkbox[red][][/red]" id= "checkbox" />
2b. The check box values would be sorted in the $_POST['checkbox'] variable, once the form is submitted.


3. give each checkbox a value that corresponds to the ID of each row you are outputting. Or create a hidden input that contains it, so its passed back to the processing page.

Code:
...
<input type = "checkbox" name= "checkbox" id= "checkbox" value="[red]<?PHP echo $row['ID'];?>[/red]" />
...

4. Run through the array to get the values for the checked checkboxes and cnstruct your query.

It doesn't matter where in the file you place the code, as long as it is properly conditioned.


Code:
if(isset($_POST['button'])){ [green]\\This checks whether the form has been submitted or not[/green]

  for($i=0;$i<=count($_POST['checkbox']),$i++){ [green]move through the array of checboxes, and form the sql statemtn to delete the checked entries.[/green]
  $sql="DELETE from contactinfo WHERE id= $_POST['checkbox'][$i]";

$res=mysql_query($sql);
[green]\\You can even echo out a message saying whcih records have been deleted if you wanted too here.[/green]  
}
}


WARNING: Not all browsers will send the value attrribute of a checkbox, some will only send whether its checked or not. however for main stream browsers like IE and FF.It works as expected.

WARNING 2, I typed this into the reply box, here, so it might have some errors.


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
What about when I'm inserting into a new table? I imagine that I have to do that first, before I delete. That includes opening and closing databases which sounds kind of tedious. Any suggestions?
 
You can do it before you delete yes:
Before the loop to delete, you can loop again to create the select statements:

Code:
if(isset($_POST['button'])){

 for($i=0;$i<=count($_POST['checkbox']),$i++){[green] move through the array of checkboxes, and form the sql statement to select the Id from the checked entries, and insert it into the other DB.[/green]
  $sql="SELECT *from contactinfo WHERE id= $_POST['checkbox'][$i]";
$Select_res=mysql_query($sql); [green]\\you should get only one row if your ID's are unique.  Then use that to insert into the other DB[/green]
$row=mysql_fetch_array($Select_res);
$insert_query="INSERT into othertablename VALUES($row['ID'],$row['FNAME'],...)";
[green]Select the other DB and execute the query[/green]
$otherDB=mysql_select_db("otherdb'sname",$conn);
$insert_res=mysql_query($insert_query);
}
[green]Once they are all inserted successfully, you can then do the delete loop[/green]

[red]Delete loop goes here, after you've inserted. [/red]

As for the connections, you'll have to connect to the first DB to retrieve the data, and then connect to the other DB to insert it.
Then re-connect to the first DB to delete them, once they have been successfully inserted.

If the DB's are in the same server, you can just use the mysql_select_db() function to choose the Db when you need to.
you could retrieve the rows, place them in an array, and delete them, and once that is done use the array to insert them into the other DB, however, if something goes wrong, you may loose the rows. In the way i posted, you can preserve the information from the rows you wish to delete, until all insertions into the other DB have been completed successfully.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top