How to Send multiple selected checkbox data to single row column box and separate them with comma( , ).
In this tutorial we are simply submitting multiple values at a single time to MySQL database table inside single row-column box. The values can be automatically separated via comma inside table cell. So here is the complete step by step tutorial for PHP Insert/Store multiple selected checkbox values in MySQL database.
PHP Insert/Store multiple selected checkbox values in MySQL database.
Code for multiple-checkbox-values.php file.
<!doctype html> <html> <head> <meta charset="utf-8"> <title>PHP Insert/Store multiple selected checkbox values in MySQL database</title> </head> <body> <h2>PHP Insert multiple checkbox values in MySQL database</h2> <form method="post" action=""> <input type="checkbox" name="chkbox[]" value="Computer"><label>COMPUTER</label><br/> <input type="checkbox" name="chkbox[]" value="Laptop"><label>LAPTOP</label><br/> <input type="checkbox" name="chkbox[]" value="Mobile"><label>MOBILE</label><br/> <input type="checkbox" name="chkbox[]" value="Tablet"><label>TABLET</label><br/> <input type="submit" name="submit" Value="submit"> </form> <?php include 'dbconfig.php'; if (isset($_POST['submit'])) { $chkbox = $_POST['chkbox']; $chkNew = ""; foreach($chkbox as $chkNew1) { $chkNew .= $chkNew1 . ","; } $query = "INSERT INTO demo_table_5 (MultipleValue) VALUES ('$chkNew')"; mysql_query($query) or die(mysql_error()); echo "Successfully Submitted."; } ?> </body> </html>
Code for dbconfig.php layout file.
<?php //Define your host here. $hostname = "localhost"; //Define your database username here. $username = "root"; //Define your database password here. $password = ""; //Define your database name here. $dbname = "test"; $conn = mysql_connect($hostname, $username, $password); if (!$conn) { die('Could not connect: ' . mysql_error()); } mysql_select_db($dbname, $conn); ?>
Screenshots: