Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Inserting multiple checkboxes in a database

Featured Replies

Hi to all

 

I need help for this problem that i'm trying to solve for a while (i'm new in PHP).

I have a form with several checkboxes which values are pulled from a database.

I managed to display them in the form, assign an appropriate value to each, but cannot insert their values into other database.

 

Here's the code:

 

 

<form id="form1" name="form1" method="post" action="">
<?php
$info_id=$_GET['info_id'];
$kv_dodatoci=mysql_query("SELECT * FROM `dodatoci`") or die('ERROR DISPLAYING: '.mysql_error());
while($kol=mysql_fetch_array($kv_dodatoci)){
$id_dodatoci=$kol['id_dodatoci'];
$mk=$kol['mk'];


 echo '<input type="checkbox" name="id_dodatoci[]" id="id_dodatoci" value="'.$id_dodatoci.'" />';
 echo '<label for="'.$id_dodatoci.'">'.$mk.'</label><br />';
 }
?>
<input type="hidden" value="<?=$info_id?>" name="info_id" />
<input name="insert_info" type="submit" value="Insert Additional info" />
</form>
<?php
if(isset($_POST['insert_info']) && is_array($id_dodatoci)){
	echo $id_dodatoci.'<br />';
	echo $mk.'<br />';

// --- Guess here's the problem  ----- //
foreach($_POST['id_dodatoci'] as $dodatok){
	$dodatok_kv=mysql_query("INSERT INTO `dodatoci_hotel`(id_dodatoci,info_id) VALUES ('$dodatok','$info_id')") or die('ERROR INSERTING: '.mysql_error());
}

}


?>

 

I need to insert a new record in the database for each checked checkbox, so i guess the problem lies in the foreach loop.

 

I hope someone can help me solve this or give me some guideline.

 

Thanks in advance.

Hi to all

 

I need help for this problem that i'm trying to solve for a while (i'm new in PHP).

I have a form with several checkboxes which values are pulled from a database.

I managed to display them in the form, assign an appropriate value to each, but cannot insert their values into other database.

 

Here's the code:

 

 

<form id="form1" name="form1" method="post" action="">
<?php
$info_id=$_GET['info_id'];
$kv_dodatoci=mysql_query("SELECT * FROM `dodatoci`") or die('ERROR DISPLAYING: '.mysql_error());
while($kol=mysql_fetch_array($kv_dodatoci)){
$id_dodatoci=$kol['id_dodatoci'];
$mk=$kol['mk'];


 echo '<input type="checkbox" name="id_dodatoci[]" id="id_dodatoci" value="'.$id_dodatoci.'" />';
 echo '<label for="'.$id_dodatoci.'">'.$mk.'</label><br />';
 }
?>
<input type="hidden" value="<?=$info_id?>" name="info_id" />
<input name="insert_info" type="submit" value="Insert Additional info" />
</form>
<?php
if(isset($_POST['insert_info']) && is_array($id_dodatoci)){
	echo $id_dodatoci.'<br />';
	echo $mk.'<br />';

// --- Guess here's the problem  ----- //
foreach($_POST['id_dodatoci'] as $dodatok){
	$dodatok_kv=mysql_query("INSERT INTO `dodatoci_hotel`(id_dodatoci,info_id) VALUES ('$dodatok','$info_id')") or die('ERROR INSERTING: '.mysql_error());
}

}


?>

 

I need to insert a new record in the database for each checked checkbox, so i guess the problem lies in the foreach loop.

 

I hope someone can help me solve this or give me some guideline.

 

Thanks in advance.

Hi i was only working on this idea last week for a future project try this

<html>

<head></head>

<body>

 

<?php

include "connect.php";

if(!isset($_POST['submit'])) {

?>

 

<?php

print "<form action='test.php' method='post'>";

print "Name\n\r<input type='text' name='name' ><br>";

$query=mysql_query("SELECT * from access");

while($result=mysql_fetch_array($query, MYSQL_ASSOC))

{

$access = array("$result[active]");

 

sort($access);

foreach($access as $a)

 

print("<input type='checkbox' name='access[]' value='$a'

>$a<br>");

 

 

 

}

print("<input type='submit' name='submit' value='Select'>");

print("</form>");

 

} elseif(is_array($_POST['access'])) {

 

print "You selected: <br />";

$name=$_POST['name'];

foreach($_POST['access'] as $a )

{

$insert="insert into block (active,name) values ('$a','$name')";

mysql_query($insert) or die ("could not insert");

 

print "$a <br>";

 

}

 

 

 

 

}

else

print("Nothing selected.");

 

 

?>

</body>

</html>

just change the table name and the variables to your requirments

  • Author

thanks for the tip ELITE.

my problem is to loop through all checkboxes, and for each checked, populate a separate record in a database.

actually i don't know how to recognize the which box is checked, and put the appropriate value in db.

 

thanks again

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.