Enjoy an ad free experience by logging in. Not a member yet?
Register .
01-29-2011, 10:06 AM
PM User |
#1
New Coder
Join Date: Jan 2011
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
how to insert multiple checkbox value in mysql database
how to insert multiple checkbox value in mysql database in php and what sould be Databse structre for this form.
Please help me Sir
Thank you in advance
This is my PHP code
--------------------------------------------------------------
PHP Code:
<?php include( 'include/config.php' ); if(isset( $_POST [ "submit" ])) { $name = $_POST [ 'name' ]; $mail = $_POST [ 'email' ]; $colour = $_POST [ 'color' ]; $gen = $_POST [ 'gender' ]; mysql_query ( "Insert into user values('','$name','$mail','$colour','$gen')" ) or die( "Data Not Inserted" ); } else { echo "Please select Submit Button" ; } ?>
-------------------------------------------------------------------------------------------------
This is my html From
------------------------------------------------------------------------------------------
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>registration</title>
</head>
<body><form action="add_user.php" method="post" name="form" enctype="multipart/form-data">
<h3 align="center">Registration Form</h3>
<table width="400" height="503" border="0" align="center" >
<tr>
<td width="135" height="59">Name:</td>
<td width="255"><label>
<input type="text" name="name" id="name" />
</label></td>
</tr>
<tr>
<td>Email:</td>
<td><label>
<input type="text" name="email" id="email" />
</label></td>
</tr>
<tr>
<td>Colours</td>
<td><label>
<input type="checkbox" name="color[]" id="color" value="Blue" />Blue<br />
<input type="checkbox" name="color[]" id="color" value="Red" />Red<br />
<input type="checkbox" name="color[]" id="color" value="Green" />Green<br />
<input type="checkbox" name="color[]" id="color" value="Yellow" />Yellow<br />
</label></td>
</tr>
<tr>
<td>Gender:</td>
<td><label>
<input type="radio" name="gender" id="gender" value="Male" />Male
<input type="radio" name="gender" id="gender" value="Female" />Female
</label></td>
</tr>
<tr>
<td height="41"> </td>
<td><label>
<input type="submit" name="submit" id="submit" value="submit" />
</label></td>
</tr>
</table>
</form>
</body>
</html>
-------------------------------------------------------------------------------------------------------
Please Reply Me Sir
Thank you
Last edited by Lamped; 01-29-2011 at 10:46 AM ..
Reason: mistak in name | Added php/code tags - Lamped
01-30-2011, 01:17 AM
PM User |
#2
Regular Coder
Join Date: Apr 2007
Posts: 317
Thanks: 24
Thanked 3 Times in 3 Posts
This should work
Quote:
Originally Posted by
phpfrnd
PHP Code:
mysql_query ( "INSERT INTO user ('name', 'mail', 'colour', 'gen') VALUES ('" . $_POST [ 'name' ] . "', '" . $_POST [ 'mail' ] . "', '" . $_POST [ 'colour' ] . "', '" . $_POST [ 'gen' ] . "')" ) or die( "Data Not Inserted" );
02-02-2011, 09:10 AM
PM User |
#3
New Coder
Join Date: Jan 2011
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
its not working-it Display data not inserted
mysql_query("INSERT INTO user ('name', 'mail', 'colour', 'gen') VALUES ('" . $_POST['name'] . "', '" . $_POST['mail'] . "', '" . $_POST['colour'] . "', '" . $_POST['gen'] . "')") or die("Data Not Inserted");
02-02-2011, 09:19 AM
PM User |
#4
New Coder
Join Date: Jan 2011
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
how to insert Multiple checkbox value into single column of mysql database in php
Last edited by phpfrnd; 02-02-2011 at 09:21 AM ..
Reason: editing
02-03-2011, 02:08 PM
PM User |
#5
Regular Coder
Join Date: Apr 2007
Posts: 317
Thanks: 24
Thanked 3 Times in 3 Posts
Just remove the single quote from the query at the field names. If that does not work for you then check your field type in your sql table. If the field type is an integer then you need to remove the quotes from the values side as well. If the fields are varchar it should be fine.
PHP Code:
mysql_query ( "INSERT INTO user (name, mail, colour, gen) VALUES ('" . $_POST [ 'name' ] . "', '" . $_POST [ 'mail' ] . "', '" . $_POST [ 'colour' ] . "', '" . $_POST [ 'gen' ] . "')" ) or die( "Data Not Inserted" );
02-05-2011, 08:40 AM
PM User |
#6
New Coder
Join Date: Jan 2011
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
hello dear
macleodjb
all fields are working but in mysql dabase 'colour 'field storing only 'array' not all colour.
mysql_query("INSERT INTO user (name, mail, colour, gen) VALUES ('" . $_POST['name'] . "', '" . $_POST['mail'] . "', '" . $_POST['colour'] . "', '" . $_POST['gen'] . "')") or die("Data Not Inserted");
please reply me
02-05-2011, 03:21 PM
PM User |
#7
Regular Coder
Join Date: Apr 2007
Posts: 317
Thanks: 24
Thanked 3 Times in 3 Posts
Quote:
Originally Posted by
phpfrnd
hello dear
macleodjb
all fields are working but in mysql dabase 'colour 'field storing only 'array' not all colour.
mysql_query("INSERT INTO user (name, mail, colour, gen) VALUES ('" . $_POST['name'] . "', '" . $_POST['mail'] . "', '" . $_POST['colour'] . "', '" . $_POST['gen'] . "')") or die("Data Not Inserted");
please reply me
ok, then you will have to take the array and create a string before you insert it into your table.
PHP Code:
$colors = $_POST [ 'color' ]; $selected_colors = "" ; foreach ( $colors as $color ) { $selected_colors .= $color . ", " ; } $selected_colors = substr ( $selected_colors , 0 , - 2 ); mysql_query ( "INSERT INTO user (name, mail, colour, gen) VALUES ('" . $_POST [ 'name' ] . "', '" . $_POST [ 'mail' ] . "', '" . $selected_colors . "', '" . $_POST [ 'gen' ] . "')" ) or die( "Data Not Inserted" );
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 09:06 PM .
Advertisement
Log in to turn off these ads.