How to Insert Multiple Checkbox Values into a Table

How do I insert multiple checkbox values into a table?

You should specify

<input type="checkbox" name="Days[]" value="Daily">Daily<br>

as array.

Add [] to all names Days and work at php with this like an array.

After it, you can INSERT values at different columns at db, or use implode and save values into one column.


Didn't tested it, but you can try like this. Don't forget to replace mysql with mysqli.

<html>
<body>
<form method="post" action="chk123.php">
Flights on: <br/>
<input type="checkbox" name="Days[]" value="Daily">Daily<br>
<input type="checkbox" name="Days[]" value="Sunday">Sunday<br>
<input type="checkbox" name="Days[]" value="Monday">Monday<br>
<input type="checkbox" name="Days[]" value="Tuesday">Tuesday <br>
<input type="checkbox" name="Days[]" value="Wednesday">Wednesday<br>
<input type="checkbox" name="Days[]" value="Thursday">Thursday <br>
<input type="checkbox" name="Days[]" value="Friday">Friday<br>
<input type="checkbox" name="Days[]" value="Saturday">Saturday <br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

<?php

// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

$checkBox = implode(',', $_POST['Days']);

if(isset($_POST['submit']))
{
$query="INSERT INTO example (orange) VALUES ('" . $checkBox . "')";

mysql_query($query) or die (mysql_error() );

echo "Complete";

}

?>

Insert multiple check box values into multiple rows in one table?

It's solved.

Problem is $pop defined twice one as a var another one in foreach

So after rename the first var every thing ok

$popimp = implode(',', $_POST['pop']);
$pops = explode(',', $pop);
foreach ($pops as $pop )
{
mysql_query("INSERT INTO pops (id, popname) VALUES (LAST_INSERT_ID(), '$pop')");
}

I want to insert multiple checkbox values in multiple columns but same table

You shouldn't be concatenating all the checkbox values. Each INSERT query needs to insert just the current element of $checkbox. You should also use a prepared statement to protect against SQL injection.

You also don't need two loops, just one.

Also, mysqli_query() returns either TRUE or FALSE when it's executing an INSERT query, I'm not sure why you're comparing it to 2.

$N = count($checkbox1);
echo("<p>You selected $N techno(s): ");
$stmt = mysqli_prepare($connection, "INSERT INTO order_table (item_ID) values (?)");
mysqli_stmt_bind_param($stmt, "i", $chk1);
$success = true;
foreach($checkbox1 as $chk1) {
if (!mysqli_stmt_execute($stmt)) {
$success = false;
break;
}
}
if($success)
{
echo'<script>alert("Inserted Successfully")</script>';
}
else
{
echo'<script>alert("Failed To Insert")</script>';
}

How to add multiple checkbox selections to my database using php

Checkboxes represent a list of values. A user can select multiple checkboxes, which means that for every user record you might have a list of values. For this reason you require another table in your database to store these options. In fact you might need three tables in total: blooddonor, activities, activities_per_donor. For more information see What's the best way to store Checkbox Values in MySQL Database?

It is up to you how you design the tables but your activities_per_donor needs to have at least two columns: user_id and activity. You should also create a composite primary key on both columns to avoid duplicate values. The activity column should be referencing your predefined list of activities from the third table so that a user cannot insert an invalid activity.

When your form is correctly created and your checkboxes are named as an array (i.e. name="act[]") then you will receive an array of selected values in PHP in $_POST['act'] variable. If no values are selected then this variable will not be set, so you need to check for that also. You need to process this array and insert each element as a new row into activities_per_donor table

How to store multiple checkboxes using PDO

Most of the time you would be using PDO to interact with the database. To insert the values you need to perform a prepared statement. You need to insert the donor data into one table and their activities into another, which requires that you wrap both inserts in a transaction.

$pdo = new \PDO("mysql:host=localhost;dbname=test;charset=utf8mb4", 'user', 'password', [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_EMULATE_PREPARES => false
]);

if (isset($_POST["name"], $_POST["gender"], $_POST["dob"], $_POST["weight"], $_POST["contact"], $_POST["bloodtype"], $_POST["adress"])) {
$pdo->beginTransaction();

// Insert blood donor
$stmt = $pdo->prepare('INSERT INTO blooddonor(name,gender,dob,weight,contact,bloodtype,adress) VALUES (?,?,?,?,?,?,?)');
$stmt->execute([
$_POST["name"],
$_POST["gender"],
$_POST["dob"],
$_POST["weight"],
$_POST["contact"],
$_POST["bloodtype"],
$_POST["adress"],
]);

$donor_id = $pdo->lastInsertId();

// Insert donor's acitvities
if(isset($_POST['act'])) {
$stmt = $pdo->prepare('INSERT INTO activities_per_donor(donor_id, activity) VALUES (?,?)');
$stmt->bindValue(1, $donor_id);
$stmt->bindParam(2, $activity);
foreach ($_POST['act'] as $activity) {
$stmt->execute();
}
}

$pdo->commit();
}

How to store multiple checkboxes using mysqli

If you have to use mysqli you can still achieve the same with a very similar code. Once more, we start a transaction and perform 2 prepared statements and then commit it into the database.

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'user', 'password', 'test');
$mysqli->set_charset('utf8mb4'); // always set the charset

if (isset($_POST["name"], $_POST["gender"], $_POST["dob"], $_POST["weight"], $_POST["contact"], $_POST["bloodtype"], $_POST["adress"])) {
$mysqli->begin_transaction();

// Insert blood donor
$stmt = $mysqli->prepare('INSERT INTO blooddonor(name,gender,dob,weight,contact,bloodtype,adress) VALUES (?,?,?,?,?,?,?)');
$stmt->bind_param('sssssss', $_POST["name"], $_POST["gender"], $_POST["dob"], $_POST["weight"], $_POST["contact"], $_POST["bloodtype"], $_POST["adress"]);
$stmt->execute();

$donor_id = $mysqli->insert_id;

// Insert donor's acitvities
if(isset($_POST['act'])) {
$stmt = $mysqli->prepare('INSERT INTO activities_per_donor(donor_id, activity) VALUES (?,?)');
$stmt->bind_param('ss', $donor_id, $activity);
foreach ($_POST['act'] as $activity) {
$stmt->execute();
}
}

$mysqli->commit();
}

PHP / MySQL: Insert multiple data from checkbox and save it to multiple row in a table

Use foreach function to print your check box one one by from $_POST['club'] array

$ClubArray=array();
$ClubArray=$_POST['club'];
foreach($ClubArray as $ClubName)
{
//your Insert code here with the insert query ie INSERT INTO bpl_club (club_name) VALUES ('$ClubName')
}


Related Topics



Leave a reply



Submit