| | |
insert into multiple tables based on checkbox
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
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
Form code:
so basically the data from the text box needs to be inserted into all the tables where a checkbox is checked.
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)
<?php require('FC_DB_connection.php'); //check if form has been submited if(isset($_POST['submit'])){ //new code sent from form $code=$_POST['code']; //to protect from mysql injections $code = stripslashes($code); //inserting data order $order = mysql_query("SELECT * FROM imob WHERE code='$code'"); if(mysql_num_rows($order) > 0 ){ echo "code already in DB"; } else if(mysql_query("INSERT INTO table1 (code, time) VALUES ('$code', NOW())")); //declare in the order variable $result = mysql_query($order); //order executes if($result){ echo("<br>New Code added"); } } ?>
Form code:
PHP Syntax (Toggle Plain Text)
<form name="addcode" action="<?php $_SERVER['PHP_SELF'];?>" method="POST"> <br /> <input name="code" type="text" value="Enter Code"> <br /> 1:<input type="checkbox" name="1"> 2:<input type="checkbox" name="2"> 3:<input type="checkbox" name="3"> 4:<input type="checkbox" name="4"> <br /> <input type="submit" name="submit" value="Add Code"> </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."
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.,
Then, On submit,
For eg.,
php Syntax (Toggle Plain Text)
<form name="addcode" action="<?php $_SERVER['PHP_SELF'];?>" method="POST"> <br /> <input name="code" type="text" value="Enter Code"> <br /> 1:<input type="checkbox" name="checkbox" value="table1"> 2:<input type="checkbox" name="checkbox" value="table2"> 3:<input type="checkbox" name="checkbox" value="table3"> 4:<input type="checkbox" name="checkbox" value="table4"> <br /> <input type="submit" name="submit" value="Add Code"> </form>
php Syntax (Toggle Plain Text)
<?php require('FC_DB_connection.php'); //check if form has been submited if(isset($_POST['submit'])){ //new code sent from form $textbox_value = mysql_real_escape_string($_POST['code']); foreach($_POST['checkbox'] as $tablename_value) { $query = "insert into $tablename_value (col1) values ('$textbox_value')"; mysql_query($query); } ..... etc.... ?>
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
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)
// sent from form $textbox_value = ($_POST['code']); $checkbox = ($_POST['checkbox']); foreach($checkbox as $tablename) { $query = "INSERT INTO $tablename (code, time) VALUES ('$textbox_value', NOW())"; $result = mysql_query($query); if($result) { echo("code added to $tablename database <br />"); } else echo "could not add code to $tablename database <br />"; } ?>
"I am the Master Piece of my own life, Everything i'm thinking is creating my future."
Print out the query, execute it in phpmyadmin, Or, simply add,
This will make the code to print the error message on the screen if there is any.
or die(mysql_error()); ie., php Syntax (Toggle Plain Text)
// sent from form $textbox_value = ($_POST['code']); $checkbox = ($_POST['checkbox']); foreach($checkbox as $tablename) { $query = "INSERT INTO $tablename (code, time) VALUES ('$textbox_value', NOW())"; $result = mysql_query($query) or die(mysql_error()); if($result) { echo("code added to $tablename database <br />"); } else echo "could not add code to $tablename database <br />"; } ?>
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
![]() |
Other Threads in the PHP Forum
- Previous Thread: Pull Data from External Website
- Next Thread: online donation with paypal
Views: 1439 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl customizableitems database date directory display download dynamic echo email error file files folder form format forms forum function functions google headmethod href htaccess html image include insert integration ip java javascript joomla jquery limit link login loop mail malfunctioning menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select server sessions sms soap source space speed sql structure syntax system table tutorial update updates upload url validation validator variable video web xml youtube






