943,782 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 10012
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
May 25th, 2009
0

How to insert checkbox data into mysql?

Expand Post »
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!
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
shena is offline Offline
57 posts
since May 2009
May 25th, 2009
0

Re: How to insert checkbox data into mysql?

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
PHP Syntax (Toggle Plain Text)
  1. $_POST['wall tile'], $_POST['floor tile']
etc.

did u tried 'echo' ing the $_POST['checkbox'] ?
does it give u the required output?
Reputation Points: 10
Solved Threads: 7
Junior Poster in Training
danishbacker is offline Offline
97 posts
since Apr 2008
May 25th, 2009
0

Re: How to insert checkbox data into mysql?

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?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
shena is offline Offline
57 posts
since May 2009
May 25th, 2009
0

Re: How to insert checkbox data into mysql?

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)
  1. <form id="f" name="f" method="post" action="test.php">
  2. <input type="radio" name="rad" id="rad" value="1" />
  3. <input type="radio" name="rad" id="rad" value="2" />
  4. <input type="radio" name="rad" id="rad" value="3" />
  5. <input name="" type="submit" value="Submit" />
  6. </form>
  7.  
  8. <?php //test.php
  9. if(isset($_POST['rad']))
  10. echo $_POST['rad']; //u can get selected item from here
  11. else
  12. echo "not set";
  13. ?>

It is easier to access dom elements either by name or id
Last edited by danishbacker; May 25th, 2009 at 7:00 am.
Reputation Points: 10
Solved Threads: 7
Junior Poster in Training
danishbacker is offline Offline
97 posts
since Apr 2008
May 25th, 2009
0

Re: How to insert checkbox data into mysql?

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)
  1. <input type="checkbox" name="ar[]" value="Value1"/>
  2. <input type="checkbox" name="ar[]" value="Value2"/>
  3. <input type="checkbox" name="ar[]" value="Value3"/>

Hope this code will help you.

PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $cmd=$_POST["cmd"];
  3. $hb=$_POST["hb"];
  4. $sel[]=array("","","");
  5. if(isset($cmd)){
  6. for($i=0;$i<count($hb);$i++) {
  7. print "<br>Your selection is $hb[$i]";
  8. $sel[$hb[$i]-1]="checked";
  9. }
  10. }
  11. ?>
  12. <html>
  13. <head>
  14. <title>Check Boxes</title>
  15. </head>
  16. <body>
  17. <form name="form1" method="POST" action="sample1.php">
  18. <br>Select Hobbies
  19. <ul>
  20. <li>Playing Outdoor games<input type="checkbox" value="1" <?=$sel[0]?> name="hb[]"></li>
  21. <li>Watching Outdoor games<input type="checkbox" value="2" <?=$sel[1]?> name="hb[]"></li>
  22. <li>Reading Books<input type="checkbox" value="3" <?=$sel[2]?> name="hb[]"></li>
  23. </ul>
  24. <input type="submit" value="Add" name="cmd">
  25. </form>
  26. </body>
  27. </html>
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
May 25th, 2009
0

Re: How to insert checkbox data into mysql?

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)
  1. <form id="f" name="f" method="post" action="test.php">
  2. <input type="radio" name="rad" id="rad" value="1" />
  3. <input type="radio" name="rad" id="rad" value="2" />
  4. <input type="radio" name="rad" id="rad" value="3" />
  5. <input name="" type="submit" value="Submit" />
  6. </form>
  7.  
  8. <?php //test.php
  9. if(isset($_POST['rad']))
  10. echo $_POST['rad']; //u can get selected item from here
  11. else
  12. echo "not set";
  13. ?>

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?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
shena is offline Offline
57 posts
since May 2009
May 25th, 2009
0

Re: How to insert checkbox data into mysql?

Click to Expand / Collapse  Quote originally posted by adatapost ...
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)
  1. <input type="checkbox" name="ar[]" value="Value1"/>
  2. <input type="checkbox" name="ar[]" value="Value2"/>
  3. <input type="checkbox" name="ar[]" value="Value3"/>

Hope this code will help you.

PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $cmd=$_POST["cmd"];
  3. $hb=$_POST["hb"];
  4. $sel[]=array("","","");
  5. if(isset($cmd)){
  6. for($i=0;$i<count($hb);$i++) {
  7. print "<br>Your selection is $hb[$i]";
  8. $sel[$hb[$i]-1]="checked";
  9. }
  10. }
  11. ?>
  12. <html>
  13. <head>
  14. <title>Check Boxes</title>
  15. </head>
  16. <body>
  17. <form name="form1" method="POST" action="sample1.php">
  18. <br>Select Hobbies
  19. <ul>
  20. <li>Playing Outdoor games<input type="checkbox" value="1" <?=$sel[0]?> name="hb[]"></li>
  21. <li>Watching Outdoor games<input type="checkbox" value="2" <?=$sel[1]?> name="hb[]"></li>
  22. <li>Reading Books<input type="checkbox" value="3" <?=$sel[2]?> name="hb[]"></li>
  23. </ul>
  24. <input type="submit" value="Add" name="cmd">
  25. </form>
  26. </body>
  27. </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!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
shena is offline Offline
57 posts
since May 2009
May 26th, 2009
0

Re: How to insert checkbox data into mysql?

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
php Syntax (Toggle Plain Text)
  1. <form id="f" name="f" method="post" action="test.php">
  2. <input type="radio" name="rad[]" id="rad[]" value="1" />
  3. <input type="radio" name="rad[]" id="rad[]" value="2" />
  4. <input type="radio" name="rad[]" id="rad[]" value="3" />
  5. <input name="" type="submit" value="Submit" />
  6. </form>
  7.  
  8. <?php
  9. $ch = $_POST['rad'];
  10. echo $ch[0];
  11. ?>
or
PHP Syntax (Toggle Plain Text)
  1. 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
Reputation Points: 10
Solved Threads: 7
Junior Poster in Training
danishbacker is offline Offline
97 posts
since Apr 2008
May 26th, 2009
0

Re: How to insert checkbox data into mysql?

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
Reputation Points: 13
Solved Threads: 15
Junior Poster in Training
Tulsa is offline Offline
77 posts
since May 2009
May 27th, 2009
0

Re: How to insert checkbox data into mysql?

Hi,
Its alright. I tried 'echo'ing $ch = $_POST['rad'];
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!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
shena is offline Offline
57 posts
since May 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: access database from php program
Next Thread in PHP Forum Timeline: delete or clear array??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC