insert into multiple tables based on checkbox

Thread Solved

Join Date: Aug 2008
Posts: 42
Reputation: AON07 is an unknown quantity at this point 
Solved Threads: 0
AON07's Avatar
AON07 AON07 is offline Offline
Light Poster

insert into multiple tables based on checkbox

 
0
  #1
Apr 24th, 2009
First off thanks in advance, everyone here has been a great help in the past and know you will be this time..

What i'm trying to achomplish is to submit data from a form into multipule tables in the Mysql DB. However I want the user to select which table the data gets inserted too by using check boxes..

I got the insert working but using the checkboxes are a little past my knowledge base.

Here is the insert code i have so far

  1. <?php
  2. require('FC_DB_connection.php');
  3.  
  4. //check if form has been submited
  5. if(isset($_POST['submit'])){
  6.  
  7. //new code sent from form
  8. $code=$_POST['code'];
  9.  
  10. //to protect from mysql injections
  11. $code = stripslashes($code);
  12.  
  13.  
  14. //inserting data order
  15. $order = mysql_query("SELECT * FROM imob WHERE code='$code'");
  16. if(mysql_num_rows($order) > 0 ){
  17.  
  18. echo "code already in DB";
  19. }
  20. else if(mysql_query("INSERT INTO table1 (code, time)
  21. VALUES ('$code', NOW())"));
  22.  
  23. //declare in the order variable
  24. $result = mysql_query($order); //order executes
  25. if($result){
  26. echo("<br>New Code added");
  27. }
  28. }
  29.  
  30. ?>


Form code:
  1. <form name="addcode" action="<?php $_SERVER['PHP_SELF'];?>" method="POST">
  2. <br />
  3. <input name="code" type="text" value="Enter Code">
  4. <br />
  5. 1:<input type="checkbox" name="1">
  6. 2:<input type="checkbox" name="2">
  7. 3:<input type="checkbox" name="3">
  8. 4:<input type="checkbox" name="4">
  9. <br />
  10. <input type="submit" name="submit" value="Add Code">
  11. </form>

so basically the data from the text box needs to be inserted into all the tables where a checkbox is checked.
"I am the Master Piece of my own life, Everything i'm thinking is creating my future."
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,761
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: insert into multiple tables based on checkbox

 
1
  #2
Apr 24th, 2009
Instead of having different names for your checkboxes, use a checkbox array and on submit, loop through all the selected checkbox and insert a record to the table.
For eg.,
  1. <form name="addcode" action="<?php $_SERVER['PHP_SELF'];?>" method="POST">
  2. <br />
  3. <input name="code" type="text" value="Enter Code">
  4. <br />
  5. 1:<input type="checkbox" name="checkbox" value="table1">
  6. 2:<input type="checkbox" name="checkbox" value="table2">
  7. 3:<input type="checkbox" name="checkbox" value="table3">
  8. 4:<input type="checkbox" name="checkbox" value="table4">
  9. <br />
  10. <input type="submit" name="submit" value="Add Code">
  11. </form>
Then, On submit,
  1. <?php
  2. require('FC_DB_connection.php');
  3.  
  4. //check if form has been submited
  5. if(isset($_POST['submit'])){
  6. //new code sent from form
  7. $textbox_value = mysql_real_escape_string($_POST['code']);
  8. foreach($_POST['checkbox'] as $tablename_value) {
  9. $query = "insert into $tablename_value (col1) values ('$textbox_value')";
  10. mysql_query($query);
  11. }
  12. ..... etc....
  13. ?>
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 42
Reputation: AON07 is an unknown quantity at this point 
Solved Threads: 0
AON07's Avatar
AON07 AON07 is offline Offline
Light Poster

Re: insert into multiple tables based on checkbox

 
0
  #3
Apr 24th, 2009
I tried the following code, it will echo each variable from the loop just fine but it won't insert the data into the database. Everything looks fine but maybe I missed something.

  1. // sent from form
  2. $textbox_value = ($_POST['code']);
  3. $checkbox = ($_POST['checkbox']);
  4. foreach($checkbox as $tablename) {
  5. $query = "INSERT INTO $tablename (code, time) VALUES ('$textbox_value', NOW())";
  6.  
  7. $result = mysql_query($query);
  8. if($result) {
  9. echo("code added to $tablename database <br />");
  10. }
  11. else
  12. echo "could not add code to $tablename database <br />";
  13. }
  14. ?>
"I am the Master Piece of my own life, Everything i'm thinking is creating my future."
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,761
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: insert into multiple tables based on checkbox

 
0
  #4
Apr 25th, 2009
Print out the query, execute it in phpmyadmin, Or, simply add, or die(mysql_error()); ie.,
  1. // sent from form
  2. $textbox_value = ($_POST['code']);
  3. $checkbox = ($_POST['checkbox']);
  4. foreach($checkbox as $tablename) {
  5. $query = "INSERT INTO $tablename (code, time) VALUES ('$textbox_value', NOW())";
  6. $result = mysql_query($query) or die(mysql_error());
  7. if($result) {
  8. echo("code added to $tablename database <br />");
  9. }
  10. else
  11. echo "could not add code to $tablename database <br />";
  12. }
  13. ?>
This will make the code to print the error message on the screen if there is any.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 42
Reputation: AON07 is an unknown quantity at this point 
Solved Threads: 0
AON07's Avatar
AON07 AON07 is offline Offline
Light Poster

Re: insert into multiple tables based on checkbox

 
0
  #5
Apr 25th, 2009
My database connection was selecting the wrong DB. Its all working now Thanks for all your help.
"I am the Master Piece of my own life, Everything i'm thinking is creating my future."
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum


Views: 1439 | Replies: 4
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC