Hi, I need help for this problem that i'm trying to solve for a while (i'm new in PHP). How do i submit the value into database?
Here's the code:

<form name="Form1" method="POST" action="checkbox.php">
<p><input type="checkbox" name="floor tile"> Floor Tiles</p>
<p><input type="checkbox" name="polished tile"> Polished Tiles</p>
<p><input type="checkbox" name="homogeneous tile"> Homogeneous Tiles</p>
<p><input type="checkbox" name="wall tile"> Wall Tiles</p>
<p><input type="submit" name="submit" value="Submit"></p>
</form>


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

/* show which options were selected */
foreach( $_POST as $key=>$val )
{
	if( $key != "Submit" ) {
	echo $key.' - '.$val.'<br />';
	}
}

/* check how many options were selected */
if( count( $_POST ) > 1 ) 
{
	echo '<p>You have selected too many options.</p>';
}
else 
{
	/* put your insert query here */
	$i<count($_POST['checkbox'] as $value)       <--line27
	$_POST['checkbox'][$i]
	$sql = mysql_query("INSERT (tile ) VALUES ('$value') INTO product_type");
}

if (!mysql_query($sql,$con))
{
  die('Error: ' . mysql_error());
}

mysql_close($con)  

?>

The problem is its showing a parse error which is:
(Parse error: parse error in C:\wamp\www\test\checkbox.php on line 27)
I also need to be able to restrict the user to select not more than one choice. Any help would be greatly appreciated! Thanks!

Me too a beginer in php,
i checked ur code but cant understand some parts of it comletely.
as i do php in php4 style and u in php5 style

First i dont understand why u are using checkboxes instead of radion button if u need only one result at a time.

Second why calling $_POST instead of their name/id
i mean like

$_POST['wall tile'], $_POST['floor tile']

etc.

did u tried 'echo' ing the $_POST ?
does it give u the required output?

Thank you for your reply danishbacker!
Please go easy on me for asking what may be an obvious question for many.
The $_POST doesn't give any output as its showing the same error message. Meanwhile, i have changed the checkboxes to radiobuttons. Thanks for the suggestion.

Between, can you show me some guidelines on inserting radiobutton value into database?

while using radio button
u need set values for each radio button to know which one is clicked
dont forget to give same name and id for all radio buttons

<form id="f" name="f" method="post" action="test.php">
 <input type="radio" name="rad" id="rad" value="1" />
 <input type="radio" name="rad" id="rad" value="2" />
 <input type="radio" name="rad" id="rad" value="3" />
<input name="" type="submit" value="Submit" />
</form>

<?php  //test.php
if(isset($_POST['rad']))
	echo $_POST['rad']; //u can get selected item from here
else
	echo "not set";
?>

It is easier to access dom elements either by name or id

It is called multivalued Request parameter. For multi-valued parameter you have to use array notation with name attribute of input form tag.

<input type="checkbox" name="ar[]" value="Value1"/>
 <input type="checkbox" name="ar[]" value="Value2"/>
 <input type="checkbox" name="ar[]" value="Value3"/>

Hope this code will help you.

<?php
$cmd=$_POST["cmd"];  
$hb=$_POST["hb"];
$sel[]=array("","","");
if(isset($cmd)){
	for($i=0;$i<count($hb);$i++) {
		print "<br>Your selection is $hb[$i]";	
		$sel[$hb[$i]-1]="checked";
	}
}
?>
<html>
 <head>
   <title>Check Boxes</title>
</head>
<body>
<form name="form1" method="POST" action="sample1.php">
<br>Select Hobbies
<ul>
  <li>Playing Outdoor games<input type="checkbox" value="1" <?=$sel[0]?> name="hb[]"></li>
  <li>Watching Outdoor games<input type="checkbox" value="2" <?=$sel[1]?>  name="hb[]"></li>
  <li>Reading Books<input type="checkbox" value="3" <?=$sel[2]?> name="hb[]"></li>
</ul>
<input type="submit" value="Add" name="cmd">
</form>
</body>
</html>

while using radio button
u need set values for each radio button to know which one is clicked
dont forget to give same name and id for all radio buttons

<form id="f" name="f" method="post" action="test.php">
 <input type="radio" name="rad" id="rad" value="1" />
 <input type="radio" name="rad" id="rad" value="2" />
 <input type="radio" name="rad" id="rad" value="3" />
<input name="" type="submit" value="Submit" />
</form>

<?php  //test.php
if(isset($_POST['rad']))
	echo $_POST['rad']; //u can get selected item from here
else
	echo "not set";
?>

It is easier to access dom elements either by name or id

Hi danish, i tried running your code, but both the echo statements didn't display anything. How do i insert the selected value into database?

p/s: what's dom?

It is called multivalued Request parameter. For multi-valued parameter you have to use array notation with name attribute of input form tag.

<input type="checkbox" name="ar[]" value="Value1"/>
 <input type="checkbox" name="ar[]" value="Value2"/>
 <input type="checkbox" name="ar[]" value="Value3"/>

Hope this code will help you.

<?php
$cmd=$_POST["cmd"];  
$hb=$_POST["hb"];
$sel[]=array("","","");
if(isset($cmd)){
	for($i=0;$i<count($hb);$i++) {
		print "<br>Your selection is $hb[$i]";	
		$sel[$hb[$i]-1]="checked";
	}
}
?>
<html>
 <head>
   <title>Check Boxes</title>
</head>
<body>
<form name="form1" method="POST" action="sample1.php">
<br>Select Hobbies
<ul>
  <li>Playing Outdoor games<input type="checkbox" value="1" <?=$sel[0]?> name="hb[]"></li>
  <li>Watching Outdoor games<input type="checkbox" value="2" <?=$sel[1]?>  name="hb[]"></li>
  <li>Reading Books<input type="checkbox" value="3" <?=$sel[2]?> name="hb[]"></li>
</ul>
<input type="submit" value="Add" name="cmd">
</form>
</body>
</html>

Hi, thank you for the reply!

I executed the code, but it turned out with a notice telling (Notice: Undefined index: hb in C:\wamp\www\test\sample1.php on line3)

The form page seem to display this way:-

Select Hobbies

  • Playing Outdoor games [checkbox] name="hb[]">
  • Watching Outdoor games [checkbox] name="hb[]">
  • Reading Books [checkbox] name="hb[]">

[Add button]

But, when i removed this part: <?=$sel[2]?> from the code, the name="hb[]"> disappear as well from the form page and prints "Your selection is 3" and the same goes for the other radio buttons.

Hope i have explained clear enough for futher action. It may be a small matter, but i dont seem to have idea in solving it. Hear you soon. Thanks!

Sorry, this is working perfect here

Check this i just uploaded a sample the file i was using here. http://peipians.com/tempDomains/test.php

try removing isset() and all those,
just echo $_POST;
see what u r getting

Also the one like Mr adatapost said is also working perfect

<form id="f" name="f" method="post" action="test.php">
 <input type="radio" name="rad[]" id="rad[]" value="1" />
 <input type="radio" name="rad[]" id="rad[]" value="2" />
 <input type="radio" name="rad[]" id="rad[]" value="3" />
<input name="" type="submit" value="Submit" />
</form>

<?php
$ch = $_POST['rad'];
echo $ch[0];
?>

or

echo $_POST['rad'][0];

the DOM stands for Document Object Model the following link will give u a small idea. Also dont forget to check w3schools
http://www.geekinterview.com/talk/6889-dom-meaning.html

hi
if u want multiple values selection than u can use multiple selected list box or check box with the name as arrname[] like this and
u can get this select value after posting the form in array

like $array=$_post;
foreach($array as $val)
{
echo $val;
}

ok now u can get the value by selecting the user
and i view your first threat code
where u need to use $_post;
so u get the ur value but u have not set the value in check box element
<input type='checkbox' value="test" name= 'floor tile' id= 'floor tile' >

thanks

Hi,
Its alright. I tried 'echo'ing $ch = $_POST;
echo $ch[0];
and it displayed the radio button value.
But i couldn't use the same variable to insert the value into database. Any help please. Thanks!

Happy to hear that ur problem was partially solved
Try this

$insertSQL = sprintf("INSERT INTO $table (id,name) VALUES (%d, %s)", $ch[0], $var);

$Result = mysql_query($insertSQL) or die(mysql_error());

otherwise u may need to use some other variable.

hi

you can do this in the loop like

foreach($array as $val)
{
echo $val;
$query="insert into table (filed) value ($val)";
$result=mysql_query($query);
}

if you want to insert selected value in to diffrent row then you can do like this

Thanks

you can use foreach loop for it. If you just required values then the

foreach($array as $val)

is enough or if you want to use index of array as well you can use

foreach($array as $key=>$val)

Happy to hear that ur problem was partially solved
Try this

$insertSQL = sprintf("INSERT INTO $table (id,name) VALUES (%d, %s)", $ch[0], $var);

$Result = mysql_query($insertSQL) or die(mysql_error());

otherwise u may need to use some other variable.

Hi,
Thanks for helping. Actually, this is wat i got:
Notice: Undefined variable: var in C:\wamp\www\test\checkbox.php on line 44
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 '(id,name) VALUES (3, )' at line 1

Why its a SQL syntax error, due to different php version/style of coding is it? I created two fields for the table called table which are:
id(int)
name(varchar)

If i can insert the values into database, then my problem is considered solved. Hope for kind reply. Thanks!

hi

you can do this in the loop like

foreach($array as $val)
{
echo $val;
$query="insert into table (filed) value ($val)";
$result=mysql_query($query);
}

if you want to insert selected value in to diffrent row then you can do like this

Thanks

Hi Tulsa,

Thanks for the explanations on multiple values selection. How do i insert the selected values in one row under the same field?

thanks!

hi

you can insert multiple value in one row as comma separated value.

$array=$_post['list_box'];
$string=implode(',',$array);

so now your selected option stored in $string variable as a comma separated ok

Thanks

Hi,

Could anyone tell me where did i go wrong with this code, i managed to connect the php with sql server but i couldnt insert the values into database. I get this error:

INSERT INTO table (name) VALUES ('FT')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 'table (name) VALUES ('FT')' at line 1

<form action="write.php" method="post">

PRODUCT TYPE:
<input type="checkbox" value="FT" name="tile[]">:FLOOR TILES
<input type="checkbox" value="HT" name="tile[]">HOMOGENEOUS TILES
<input type="checkbox" value="WT" name="tile[]">:WALL TILES
<input type="checkbox" value="PT" name="tile[]">:POLISHED TILES
<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'>

</form>

[U]This is write.php[/U]

<?php

$con = mysql_connect("localhost","root");

if (!$con)
{
	die('Could not connect: ' . mysql_error());
}

mysql_select_db("test_venus", $con);

if(isset($_POST['tile']))
{      
	foreach($_POST['tile'] as $value) 
	{         
 		$insert="INSERT INTO table (name) VALUES ('$value')";     
 		mysql_query($insert);    	
		echo $insert;
 	}
}

if (!mysql_query($insert, $con))
{
  die('Error: ' . mysql_error());
}

mysql_close($con) 

?>

I tried this for a long time for an answer to this problem, and I couldn't find anything that really helped. I feel that there is no wrong in my codings, but yet i couldnt solve it or may be i dont get to realise. So, any guidance would be gratefully recieved.
Thanks!

try this.

$insert="INSERT INTO `table` (name) VALUES ('$value')";

U have given "table" as table name and "name" as field name.
Both have special meaning in sql. So if u are using it u need to add `` like
eg :-`$tableName`

Better try to avoid using these names. Also i recommend you to use SQLyog.
The community version is free. You can get it from http://www.webyog.com

It is better first try ur sql statements in SQLyog. This will help u t o avoid these type of mistakes.

hi

you have test the value of list box by the echo right ?
so now problem only insert query ok.

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

table is the table name right.
so as per my view mysql not allow to use this reserved key word use to us.

so you need to use table name diffrent rather than 'table' ok
i hope this will solve your problem

Thanks

Hi danish,
Thanks a lot for the answer. It did work. But, i juz want to clarify that what i did (insertion method - without `` for the table name), did work for other scripts, why cant i do the same for this script?
Is this an alternate way?

Hi danish,

Sory for the latest reply, i didn't read your reply properly. Now, i got it. It did work! Its very obvious that im still a novice to php scriptings!

Anyway thanks again!

Hi Tulsa,

Thanks for the reply! It worked. I hope you all don't mind helping on things which are obvious to many but not for me.

Long discussion. Have you got a solution of your problem.

Oh yeah , i got it! :) Thanks a lot to all!

:) happy to hear that ur problem is solved now.
did understood what is `` for?

don't know what is the actual definition. But it works like escape character. ie like we use slashes in string.
Bcoz the names u were using are reserved ones (table,name). May be we can call them as keywords.

There are also many others like date, time, timestamp, desc, asc
If u use any good mysql client (like sqlyog) it will show these reserved names in different color, so that u can identify them.

Also if the problem is solved, mark it as solved.

Good Luck :)

This question has already been answered. Start a new discussion instead.