943,175 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1306
  • PHP RSS
Sep 1st, 2010
0

Inserting multiple checkbox to database

Expand Post »
Hi,
Iam new to PHP I need some help I am trying to insert multiple checkboxes values
<td>
<input name="myvar[]" type="checkbox" value="abc">
abc<br/>
<input name="myvar[]" type="checkbox" value="def">
def<br>
<input name="myvar[]" type="checkbox" value="ghi">
ghi</td>
along with other datas to the database, and I dont know how to do this
I wrote query for inserting other datas to db,(but i dont know how to include the loop value from the checkbox along with this).
$Details=mysql_query("insert into Example values('$ID','$data1','$data2'......) please explain I am new in this field
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
micahgeorge is offline Offline
16 posts
since Sep 2010
Sep 5th, 2010
0
Re: Inserting multiple checkbox to database
assuming you had:
html Syntax (Toggle Plain Text)
  1. <input type="text" name="person" value="John"/>
  2. <input name="sport[]" type="checkbox" value="baseball">
  3. <input name="sport[]" type="checkbox" value="basquetball">
  4. <input name="sport[]" type="checkbox" value="football">
  5. <input name="sport[]" type="checkbox" value="hocket">

then:
php Syntax (Toggle Plain Text)
  1. foreach($_POST['sport'] as $value)
  2. {
  3. $sql = sprintf("INSERT INTO `Atletes`(`personId`,`sportName`) VALUES('%s','%s')"
  4. ,mysql_real_escape_string($_POST['person'])
  5. ,mysql_real_escape_string($value)
  6. );
  7. }
Reputation Points: 116
Solved Threads: 243
Veteran Poster
hielo is offline Offline
1,123 posts
since Dec 2007
Sep 10th, 2010
0
Re: Inserting multiple checkbox to database
thanks heilo,I was able to insert the values, and it will take different rows in the database right?like baseball in one row then basketball in the second row.... and will you please help me in displaying the value from the database too,
Reputation Points: 10
Solved Threads: 0
Newbie Poster
micahgeorge is offline Offline
16 posts
since Sep 2010
Sep 11th, 2010
0
Re: Inserting multiple checkbox to database
Quote ...
it will take different rows in the database right?like baseball in one row then basketball in the second row
correct

but this might be better:
PHP Syntax (Toggle Plain Text)
  1. <input name="sport[3][]" type="checkbox" value="baseball">
  2. <input name="sport[3][]" type="checkbox" value="basquetball">
  3. <input name="sport[3][]" type="checkbox" value="football">
  4. <input name="sport[3][]" type="checkbox" value="hocket">
  5.  
  6. <input name="sport[7][]" type="checkbox" value="baseball">
  7. <input name="sport[7][]" type="checkbox" value="basquetball">
  8. <input name="sport[7][]" type="checkbox" value="football">
  9. <input name="sport[7][]" type="checkbox" value="hocket">

where 3 and 7 are the corresponding personId. So to do the insert:
php Syntax (Toggle Plain Text)
  1. foreach($_POST['sport'] as $personId=>$sports)
  2. {
  3. foreach( $sports as $value)
  4. {
  5. $sql = sprintf("INSERT INTO `Atletes`(`personId`,`sportName`) VALUES('%s','%s')"
  6. ,mysql_real_escape_string( $personId)
  7. ,mysql_real_escape_string($value)
  8. );
  9. }
  10. }

Quote ...
and will you please help me in displaying the value from the database too,
all you have to do is to execute a select statement:
php Syntax (Toggle Plain Text)
  1. $result=mysql_query("SELECT personId,sportName FROM Athletes") or die( mysql_error() );
  2. while($row=mysql_fetch_assoc($result))
  3. {
  4. echo sprintf(PHP_EOL.'<div><input type="textbox" name="sport[%s][]" value="%s"/></div>', $row['personId'], $row['sportName'] );
  5. }
Reputation Points: 116
Solved Threads: 243
Veteran Poster
hielo is offline Offline
1,123 posts
since Dec 2007

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: extension uppercase problem
Next Thread in PHP Forum Timeline: order by time





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


Follow us on Twitter


© 2011 DaniWeb® LLC