0

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
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:

<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.

2
Contributors
4
Replies
6
Views
7 Years
Discussion Span
Last Post by AON07
Featured Replies
  • 1

    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., [code=php] <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 … Read More

1

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.,

<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>

Then, On submit,

<?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....
 ?>
0

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.

// 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 />";
}
?>
0

Print out the query, execute it in phpmyadmin, Or, simply add, or die(mysql_error()); ie.,

// 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 />";
}
?>

This will make the code to print the error message on the screen if there is any.

0

My database connection was selecting the wrong DB. Its all working now Thanks for all your help.

This question has already been answered. Start a new discussion instead.
Please be thoughtful and detailed and be sure to adhere to our posting rules.