I am new to PHP or just not good at it lol, I have been trying to insert checkbox elements into a mysql database.

I have a table called Team with two collumns.

Id int not null auto increment PK
team varchar 30 not null

<?
    <body><form action="insert.php" method="post">

        select team:
        <input type="checkbox"  value="AR"    NAME="team[]">:Argentina
        <input type="checkbox"  value="PL"    NAME="team[]">:Spain
        <input type="checkbox"  value="GB"    NAME="team[]">:Holland
        <input type="checkbox"  value="FR"    NAME="team[]">:France
<br>

  <input name="confirm" type=submit id="confirm" style='border:1px solid #000000;font-family:Verdana, Arial, Helvetica, sans-serif ;font-size:9px;' value='Confirm'>
</body>
</form>
?>

This is the insert.php

<?php
$con = mysql_connect("localhost","****","*****");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
if (!$id) {
    $id= null;
}

<? 

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

$team = 1;

$insert=mysql_query("

INSERT INTO team('team') VALUES ('" . $team. ");

   ?>




Parse error: syntax error, unexpected '<' in C:\wamp\www\examples\insert.php on line 10

Any help or suggestions will be much appreciated.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

<?php
if(isset($_POST['team']))
{
	foreach($_POST['team'] as $value){
		$insert=mysql_query("INSERT INTO team('team') VALUES ('$value')");
	}
}
?>
<html>
<body>
<form method="post" action="chk123.php">
<input type="checkbox" name="team[]" value="AG"> Argentina <br />
<input type="checkbox" name="team[]" value="GE"> Germany <br />
<input type="checkbox" name="team[]" value="BR"> Brazil <br />
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

There! That will work :)

Cheers,
Naveen

Edit: The error was because, you had opened php tag but you opened it again.

<html>
<body>
<form method="post" action="chk123.php">
<input type="checkbox" name="team[]" value="AG"> Argentina <br />
<input type="checkbox" name="team[]" value="GE"> Germany <br />
<input type="checkbox" name="team[]" value="BR"> Brazil <br />
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>




<?php
$con = mysql_connect("localhost","root","****");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());

if(isset($_POST['team']))
{   foreach($_POST['team'] as $value){  
    $insert=mysql_query("INSERT INTO team('team') 
  VALUES ('$value')");
    }
    }
    ?>

I am getting the error below, whats wrong??? :(

Parse error: syntax error, unexpected $end in C:\wamp\www\examples\chk123.php on line 13

Edited 3 Years Ago by mike_2000_17: Fixed formatting

$insert="INSERT INTO team('team')
VALUES ('$value')";
echo $insert;
mysql_query($insert);

Print your query. Execute the same in phpmyadmin or mysql. Check what's the error. Or you can give, mysql_query($insert) or die(mysql_error()); to know what is the error.

Hi, i have tried this but doesn't return anything.

I'm Wondering whether my table design in mysql is incorrect.

I have only two collumns, one to store the ID and one to store the team.

Table Team 
[U]id[/U]      Int(11)    Not Null     auto_increment
team          varchar(30)   Not Null 




<?php
$con = mysql_connect("localhost","root","talk21");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
}
if(isset($_POST['team']))
{   foreach($_POST['team'] as $value){  
    $insert=mysql_query("INSERT INTO team('team') 
  VALUES ('$value')");
    }
    }

echo $insert;mysql_query($insert);

    ?>

Edited 3 Years Ago by mike_2000_17: Fixed formatting

<?php
$con = mysql_connect("localhost","root","talk21");
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
if(isset($_POST['team'])) {    foreach($_POST['team'] as $value) {
    $insert="INSERT INTO team ('team') VALUES ('$value')";
echo $insert;
mysql_query($insert);
}
}

Try this. It will print out the query. Execute it in phpmyadmin or mysql. I don't think your query or the table structure is wrong. Maybe the values of $_POST isn't being passed. Well, execute these queries and find out why its not inserting !

I tried the query it prints "INSERT INTO team ('team') VALUES ('BR')" correctly however it still doesnt store the value into the database, I even selected mysql_select_db("lastr", $con);$sql= the database still not working.

thank you for all your responses by da way.

<?php
$con = mysql_connect("localhost","root","talk21");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if(isset($_POST))
{ foreach($_POST as $value) {
mysql_select_db("lastr", $con);$sql= $insert="INSERT INTO team ('team')
VALUES ('$value')";echo $insert;mysql_query($insert);
}
}

?>

umm.. You don't have a mysql_select_db in your script. If it isn't inserting, use die function with mysql_error with your query. For eg.

$query="select * from tablename";
mysql_query($query) or die(mysql_error());

So, if there's any problem with your query, it will output the error message, which you can use to debug your query.

I have tried it with this function "mysql_query($query) or die(mysql_error());"

there is no error and nothing is still being inserted into the database :(.


<?php
$con = mysql_connect("localhost","root","talk21");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if(isset($_POST))
{ foreach($_POST as $value) {
mysql_select_db("lastr", $con);$sql= $insert="INSERT INTO team ('team')
VALUES ('$value')";echo $insert;mysql_query($insert);
}
}


$query="select * from team";mysql_query($query) or die(mysql_error());

?>

Okay! Take out 'team' and put team. ie.,

$insert="INSERT INTO team (team)
VALUES ('$value')";

Tellme if it works.

Still the same, Nothing going into the database, maybe should insert into id aswell.


<?php
$con = mysql_connect("localhost","root","talk21");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

if(isset($_POST))
{ foreach($_POST as $value) {
mysql_select_db("lastr", $con);$sql= $insert="INSERT INTO team. ('team')
VALUES ('$value')";echo $insert;mysql_query($insert);
}
}


$query="select * from team";mysql_query($query) or die(mysql_error());
?>

Umm.. is Id an auto-increment field ? What did echo $insert print ? Could you show us the statement ? Did you execute that statement in phpmyadmin/mysql ?

INSERT INTO team ('team') VALUES ('GE')INSERT INTO team ('team') VALUES ('BR')

the id is set to auto_increment, when i run the query on phpadmin this is the error.

#1064 - 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 ''team') VALUES ('GE')INSERT INTO team ('team') VALUES ('BR')' at line 1

:) That's what I was talking about.

<?php
$con = mysql_connect("localhost","root","talk21");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("lastr");

if(isset($_POST['team']))
{ 
     foreach($_POST['team'] as $value) {
          $insert="INSERT INTO team (team) VALUES ('$value')";
     mysql_query($insert);
    }
}
?>

This will definitely work. :)

P.S: Next time you post your code, please post it within code-tags.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Great, many thanks for your help :). say if i wanted to do a dynamic table in mysql to insert the checkbox values from is there any threads you could point me to for example or help.

I didn't get you. What do you mean by dynamic table ? you mean creating table on runtime ?

I mean getting the checkbox values from a database and puting it on form instead or hardcoding it. e.g

<form name="form" method="GET" action=" <?php echo 'process.php' ?> ">
Navn<input type="text" name="Navn" /><br>
country<?php echo html_drop_down('select'); ?><br>

<input type="submit" value="Send" />
</form>


$sql = 'SELECT * From Countries';

while ($rec = mysql_fetch_array($result))
{
print "<input name=\"".$rec."\" type=\"checkbox\" value=\"".$rec."\" /> ".$rec
}

$query="select * from countries";
$result=mysql_query($query);
while($row=mysql_fetch_array($result)){
  $id=$row['id'];
 $country=$row['country'];
  print "<input type=\"checkbox\" name=\"chkbox[]\"  value=\"$id\">".$country."<br/>";
}

I have this form code working it prints the checkoxes with the countries.


<form id="form1" name="form1" method="post" action="page2.php">
<?php
$sql = 'SELECT * From Countries';

$con = mysql_connect("localhost","root","talk21") or die;


$result = mysql_db_query(talk21, $sql);

while ($rec = mysql_fetch_array($result))
{
print "\n<input type='checkbox' name=Countries[]' value='".$rec."' > ".$rec." </input>";
}

?><br />
<input type="submit" name="Submit" value="Register" />
</label>
</form>


the page should process the form page2.php,

<?php
$countries = $_REQUEST["Countries"];

for(int $i = 0; $i < $countries.size; i++){
if(isset($countries[$i])){

}
}
?>

the code is incomplete

What is the problem ? And please post your code within CODE tags. Btw, there is no function called .size . Its count or sizeof.

Edited 3 Years Ago by happygeek: fixed formatting

What is the problem ? And please post your code within code tags. Btw, there is no function called .size . Its count or sizeof.

yes you are right sizeof. ,the error is Parse error: syntax error, unexpected '}' in C:\wamp\www\examples\prew_akt.php on line 8.

I want to insert the values selected from the form into a table also, i want create a page where the values of the checkboxes can be edited CHANGED/DELETED.

Edited 3 Years Ago by happygeek: fixed formatting

I dont see any unexpected } in your code. Please post your latest code(with code tags).. And the idea (what I follow) to edit the check box's value is, When the user is in the edit screen and he has changed the checkbox's selection, I first delete all the previously selected checkboxes for that particular user and then re-insert the new values again.

Edited 3 Years Ago by happygeek: fixed formatting

Hi again yes I want one page to edit the checkboxs values so they can be updated or added/deleted.

the code here is to store the selected values into a database after the form is submitted.

 <?php
$countries = $_REQUEST["Countries"];

for(int $i = 0;  $i < $countries.sizeof.; i++){
if(isset($countries[$i])){

}
}

This code below is intended for to update or add/delete countries.

// ADDING Countries HERE //
if(array_key_exists('add', $_GET)){
    $id = $_POST['id'];
    $countries = $_POST['countries'];

    $sql = mysql_query("INSERT INTO countries (id, countries,)
          VALUES('$id', '$countries', )") or die (mysql_error());
}
    if (!$sql) {
        $message = "Error has occured";

        } else {
        $statusa = '1';
        $message = "editcountry has been ADDED successfully";
    }
// START EDITING countries //

if(array_key_exists('edit', $_GET)){
    $id = $_POST['editid'];
    $editcountry = $_POST['editcountry'];

    $sqla = "UPDATE `countries` SET `editcountry` = '$editcountry',WHERE `id` = '$id' ";

    $sql = mysql_query($sqla) or die (mysql_error());
    $statusa = '1';
    $message = "countries has been EDITED successfully";

?>
<html>
<body>

      Add Countries
        <form action="admin.php?add=1" method="post" name="add" id="add">

        <input type='checkbox' name=Countries[]' value='".$rec['id']."' > ".$rec['Country']." </input>";     
        <input name="submit" type="submit" id="submit" value="Submit">
        </form>
      <p>

      <?
      if ($statusa == 1){
        echo $message ;
      }
      ?>

      Edit Countries
      <?
        $sql = "SELECT * FROM `property`";
        $send = mysql_query($sql) or die(mysql_error());

        ?>
            <form action="admin.php?edit=1" method="post" name="edit" id="edit">
        ID 
        <select name="editid" id="editid">
        <?  while ($row = mysql_fetch_object($send)) {
            echo "<option>$row->id<option>";
            }
        ?>
        </select>        
        <br>
        DELETE 
        <select name="editdelete" id="editdelete">
          <option selected>No</option>
          <option>Yes</option>
          </select>
        <br>

       <input type='checkbox' name=editCountries[]' value='".$rec['id']."' > ".$rec['Country']." </input>";  

</body>
</html>

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Error 1.

for(int $i = 0; $i < $countries.sizeof.; i++){

My apologies. I said there is no sizeof. But there is sizeof. But this isn't the way to use it. Its sizeof($variable);
Error 2.

$sql = mysql_query("INSERT INTO countries (id, countries,)
VALUES('$id', '$countries', )") or die (mysql_error());

Wrong query. What's the "," doing ?
Error 3. You are using method=POST in the form . But you are searching in $_GET ?

if(array_key_exists('edit', $_GET)){

Anyway, here is a simple example to insert teams to the table, list selected teams and delete the teams from the table.

<?php //insert to the table
if(isset($_POST['submit'])){
	mysql_connect("localhost","root");
	mysql_select_db("test");
	foreach($_POST['country'] as $value){
		$query="insert into country (team) values ('$value')";
		mysql_query($query);
	}
}
?>
<html>
<body>
<form method="post" action="checkbox.php">
<input type="checkbox" name="country[]" value="AG">Argentina <br />
<input type="checkbox" name="country[]" value="GE">Germany <br />
<input type="checkbox" name="country[]" value="BR">Brazil <br />
<input type="checkbox" name="country[]" value="SW">Sweden <br />
<input type="checkbox" name="country[]" value="EN">England <br />
<input type="checkbox" name="country[]" value="SC">Some country <br />
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
<?php //list inserted teams select teams you want to delete and click submit and it will delete. 
	mysql_connect("localhost","root");
	mysql_select_db("test");
	
	if(isset($_POST['submit'])) {
		foreach($_POST['country'] as $value) {
			$query="delete from country where team='$value'";
			mysql_query($query);
		}
	}
	
	$query="select * from country";
	$result=mysql_query($query);
	echo "<form method=\"post\" action=\"editcheckbox.php\">";
	while($row=mysql_fetch_array($result)) {
		$country=$row['team'];
		echo "<input type=\"checkbox\" name=\"country[]\" value='$country'>$country<br />";
	}
	echo "<input type='submit' name='submit' value='submit'>";
	echo "</form>";
?>

I hope that helps :)

This article has been dead for over six months. Start a new discussion instead.