help with array checkbox

A

Anonymous

Guest
Guys, i always get my error message, i cant insert the values from the checkbox. what isw wrong with my code?

thank you

Code:
<?php 

require_once('dbconnect.php'); 

if (isset($_POST['submit'])) { 
	
		$chk = join (',' , $_POST['check']);
		
		$query = "INSERT INTO test (check) VALUES ('$chk')";
		
		if ($result = @mysql_query ($query)) 

                  { 
                  echo 'OK';
		}
                  else 
                  { 
		echo 'ERROR'; 
                  }
		} 
                  else
                  {
	
?>
<form enctype="multipart/form-data" action="test_add.php" method="post">
  <p>Sample Checkbox Form</p>
  <p> 
    <input type="checkbox" name="check[]" value="Coke">
    Coke </p>
  <p> 
    <input type="checkbox" name="check[]" value="Sprite">
    Sprite</p>
  <p> 
    <input type="checkbox" name="check[]" value="Pepsi">
    Pepsi</p>
  <p>
    <input type="submit" name="submit" value=" submit" />
  </p>
  </form>
  <?
  }
  ?>
[/code]
 
Do this.
Code:
$result = mysql_query ($query) or die("Error: ".mysql_error())
This will print the actual error you are having.
 
INSERT INTO test (check) VALUES ('Coke,Sprite,Pepsi') CAUSED AN ERROR: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'check) VALUES ('Coke,Sprite,Pepsi')' at line 1
 
I get this error message

INSERT INTO test (check) VALUES ('Coke,Sprite,Pepsi') CAUSED AN ERROR: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'check) VALUES ('Coke,Sprite,Pepsi')' at line 1

I have modified my code and still the same error.

please help

Code:
<?php 

require_once('dbconnect.php'); // Connect to the database.

if (isset($_POST['submit'])) { // Handle the form.
	
	if (!empty($_POST['check'])) {
		$c = join (', ' , $_POST['check']);
		}else {
		$c = FALSE;
			echo '<p><font color="red">Please Check 1</font></p>';
	}
		
	if ($c) {
		// Add the contact information
		$query = "INSERT INTO test (check) VALUES ('$c')";
		$result = mysql_query($query) or die($query.' CAUSED AN ERROR: '.mysql_error());
		if ($result = @mysql_query ($query)) { 
				
			echo 'OK';
			
		
		}else { // If the query did not run OK.
			echo '<p><font color="red">Your submission could not be processed due to a system error.</font></p>'; }
		
		
	} 
	else { // Failed a test.
			echo '<p><font color="red">Please click "back" and try again.</font></p>'; }
	


} 

else{
	
?>
<form enctype="multipart/form-data" action="test_add.php" method="post">
  <p>Check</p>
  <p> 
    <input type="checkbox" name="check[]" value="Coke">
    Coke </p>
  <p> 
    <input type="checkbox" name="check[]" value="Sprite">
    Sprite</p>
  <p> 
    <input type="checkbox" name="check[]" value="Pepsi">
    Pepsi</p>
  <p>
    <input type="submit" name="submit" value=" submit" />
  </p>
  </form>
  <?
  }
  ?>
[/quote]
 
here is the answere = you are trying to add 3 different values to 1 column... and not in 3 different rows but in one...
use query for multy-inserts instead...
 
hi, yes I am going to insert 3 values in one db column.

i already got the answer but i have to remove the conditions

Code:
<?php

require_once('dbconnect.php');

if (isset($_POST['submit'])) {

$c = join (',' , $_POST['check']);

$queryq = "INSERT INTO `test` (`check`) VALUES ('$c')";

mysql_query ($queryq) or die(mysql_error());

}
               else
               {

?>
<form enctype="multipart/form-data" action="1.php" method="post">
<p>Sample Checkbox Form</p>
<p>
 <input type="checkbox" name="check[]" value="Coke">
 Coke </p>
<p>
 <input type="checkbox" name="check[]" value="Sprite">
 Sprite</p>
<p>
 <input type="checkbox" name="check[]" value="Pepsi">
 Pepsi</p>
<p>
 <input type="submit" name="submit" value=" submit" />
</p>
</form>
<?
}
?>

now my problem is whenever i add a condition to check if there is at least one item checked it has an error. is there a problem with my structure?

whew!

thnx again
 
Back
Top