943,712 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 2956
  • PHP RSS
Apr 24th, 2009
0

insert into multiple tables based on checkbox

Expand Post »
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

PHP Syntax (Toggle Plain Text)
  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:
PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Light Poster
AON07 is offline Offline
42 posts
since Aug 2008
Apr 24th, 2009
1

Re: insert into multiple tables based on checkbox

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.,
php Syntax (Toggle Plain Text)
  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,
php Syntax (Toggle Plain Text)
  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. ?>
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Apr 24th, 2009
0

Re: insert into multiple tables based on checkbox

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.

php Syntax (Toggle Plain Text)
  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. ?>
Reputation Points: 10
Solved Threads: 0
Light Poster
AON07 is offline Offline
42 posts
since Aug 2008
Apr 25th, 2009
0

Re: insert into multiple tables based on checkbox

Print out the query, execute it in phpmyadmin, Or, simply add, or die(mysql_error()); ie.,
php Syntax (Toggle Plain Text)
  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.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Apr 25th, 2009
0

Re: insert into multiple tables based on checkbox

My database connection was selecting the wrong DB. Its all working now Thanks for all your help.
Reputation Points: 10
Solved Threads: 0
Light Poster
AON07 is offline Offline
42 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Pull Data from External Website
Next Thread in PHP Forum Timeline: Please provide code Logic for a photo gallery





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC