954,180 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Inserting multiple checkbox to database

Hi,
Iam new to PHP I need some help I am trying to insert multiple checkboxes values


abc

def

ghi
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

micahgeorge
Newbie Poster
16 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

assuming you had:

<input type="text" name="person" value="John"/>
<input name="sport[]" type="checkbox" value="baseball">
<input name="sport[]" type="checkbox" value="basquetball">
<input name="sport[]" type="checkbox" value="football">
<input name="sport[]" type="checkbox" value="hocket">


then:

foreach($_POST['sport'] as $value)
{
  $sql = sprintf("INSERT INTO `Atletes`(`personId`,`sportName`) VALUES('%s','%s')"
                ,mysql_real_escape_string($_POST['person'])
                ,mysql_real_escape_string($value)
                );
}
hielo
Veteran Poster
1,124 posts since Dec 2007
Reputation Points: 116
Solved Threads: 243
 

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,

micahgeorge
Newbie Poster
16 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 
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:

<input name="sport[3][]" type="checkbox" value="baseball">
<input name="sport[3][]" type="checkbox" value="basquetball">
<input name="sport[3][]" type="checkbox" value="football">
<input name="sport[3][]" type="checkbox" value="hocket">

<input name="sport[7][]" type="checkbox" value="baseball">
<input name="sport[7][]" type="checkbox" value="basquetball">
<input name="sport[7][]" type="checkbox" value="football">
<input name="sport[7][]" type="checkbox" value="hocket">


where 3 and 7 are the corresponding personId. So to do the insert:

foreach($_POST['sport'] as $personId=>$sports)
{
  foreach( $sports as $value)
  {
   $sql = sprintf("INSERT INTO `Atletes`(`personId`,`sportName`) VALUES('%s','%s')"
                ,mysql_real_escape_string( $personId)
                ,mysql_real_escape_string($value)
                );
  }
}
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:

$result=mysql_query("SELECT personId,sportName FROM Athletes") or die( mysql_error() );
while($row=mysql_fetch_assoc($result))
{
  echo sprintf(PHP_EOL.'<div><input type="textbox" name="sport[%s][]" value="%s"/></div>',  $row['personId'], $row['sportName'] );
}
hielo
Veteran Poster
1,124 posts since Dec 2007
Reputation Points: 116
Solved Threads: 243
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You