| | |
How to insert checkbox data into mysql?
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: May 2009
Posts: 13
Reputation:
Solved Threads: 0
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:
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!
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)
?>(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!
Last edited by peter_budo; May 25th, 2009 at 3:37 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
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['checkbox'] instead of their name/id
i mean like etc.
did u tried 'echo' ing the $_POST['checkbox'] ?
does it give u the required output?
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['checkbox'] instead of their name/id
i mean like
PHP Syntax (Toggle Plain Text)
$_POST['wall tile'], $_POST['floor tile']
did u tried 'echo' ing the $_POST['checkbox'] ?
does it give u the required output?
•
•
Join Date: May 2009
Posts: 13
Reputation:
Solved Threads: 0
Thank you for your reply danishbacker!
Please go easy on me for asking what may be an obvious question for many.
The $_POST['checkbox'] 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?
Please go easy on me for asking what may be an obvious question for many.
The $_POST['checkbox'] 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
It is easier to access dom elements either by name or id
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
PHP Syntax (Toggle Plain Text)
<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
Last edited by danishbacker; May 25th, 2009 at 7:00 am.
It is called multivalued Request parameter. For multi-valued parameter you have to use array notation with name attribute of input form tag.
Hope this code will help you.
PHP Syntax (Toggle Plain Text)
<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 Syntax (Toggle Plain Text)
<?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>
Failure is not fatal, but failure to change might be. - John Wooden
•
•
Join Date: May 2009
Posts: 13
Reputation:
Solved Threads: 0
•
•
•
•
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
PHP Syntax (Toggle Plain Text)
<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?
•
•
Join Date: May 2009
Posts: 13
Reputation:
Solved Threads: 0
•
•
•
•
It is called multivalued Request parameter. For multi-valued parameter you have to use array notation with name attribute of input form tag.
PHP Syntax (Toggle Plain Text)
<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 Syntax (Toggle Plain Text)
<?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['rad'];
see what u r getting
Also the one like Mr adatapost said is also working perfect
or
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
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['rad'];
see what u r getting
Also the one like Mr adatapost said is also working perfect
php Syntax (Toggle Plain Text)
<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]; ?>
PHP Syntax (Toggle Plain Text)
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['arrname'];
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['floor tile'];
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
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['arrname'];
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['floor tile'];
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
![]() |
Similar Threads
- using session variables to insert data to MYSQL Database (PHP)
- How do I insert multiple checkbox data into MySQL (PHP)
- inserting checkbox values in mysql +PHP (PHP)
Other Threads in the PHP Forum
- Previous Thread: access database from php program
- Next Thread: delete or clear array??
| Thread Tools | Search this Thread |
advanced apache api array basics beginner binary broken cakephp check checkbox class cms code codingproblem combobox cookies cron curl database date datepart display dynamic echo email error file files folder form forms function functions google head href htaccess html image include includingmysecondfileinthechain insert ip javascript job joomla js limit link login mail menu mlm mobile multiple mysql oop outofmemmory paging parse password paypal pdf php problem procedure query radio random recursion remote script search server sessions smarty sms soap source space sql stored syntax system table traffic tutorial unicode up-to-date update upload url validator variable video web webapplications xml youtube






