AccuCORE Posted November 14, 2020 Share Posted November 14, 2020 (edited) Hello I need a great help for my problem, I tried to insert multiple checkboxs for "$_POST active" in my CRUD and the values must be saved in the database but will be saved as "Array" everytime. I used this code but where exactly I need to put it? foreach($_POST['active'] as $act){//query? } Here the PHP code <?php require_once "../lakota/config.php"; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (isset($_POST['faction'], $_POST['stations'], $_POST["active"], $_POST['pending'], $_POST['influence'], $_POST['id_fact'])) { if(isset($_POST["submit"])){ $activearr=$_POST["active"]; $newvalues= implode(",", $activearr); include_once "../lakota/checkboxClass.php"; $checkBoxClass=new checkboxClass(); echo $checkBoxClass->addtoDatabase($newvalues); } $sql = "INSERT INTO lakotabgs (faction, stations, active, pending, influence, id_fact) VALUES (?,?,?,?,?,?)"; if ($stmt = $link->prepare($sql)) { $stmt->bind_param("ssssss", $_POST['faction'], $_POST['stations'], $_POST["active"], $_POST['pending'], $_POST['influence'], $_POST['id_fact']); if ($stmt->execute()) { header("location: ../lakota/index.php"); exit(); } else { echo "Error! Try again later."; } $stmt->close(); } } $link->close(); } ?> HTML code <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Add new info</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> </head> <body> <form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post"> <label><b>Active States</b></label> <input name="active[]" class="form-control" id="example1" type="checkbox" value="None" /> <label for="example1">None</label> <input name="active[]" class="form-control" id="example2" type="checkbox" value="Everybody" /> <label for="example2">Everybody</label> <input name="active[]" class="form-control" id="example3" type="checkbox" value="Unknown" /> <label for="example3">Unknown</label> <input type="submit" id="submit" name="submit" class="btn btn-primary" value="Add new info"> <a href="../lakota/index.php" class="btn btn-default">Back</a> </form> </body> </html> Edited November 14, 2020 by AccuCORE Quote Link to comment https://forums.phpfreaks.com/topic/311709-issue-to-save-values-in-database-with-multiple-checkboxs-in-crud/ Share on other sites More sharing options...
gw1500se Posted November 14, 2020 Share Posted November 14, 2020 (edited) Your code is a bit confusing so I'm not sure I am following what you are trying to do. You convert $_POST["active"] to a comma separated string ($newvalues) but when you store it in the database you are binding an array ($_POST["active"]) to a column. You can't store an array per se as a column in MySQL. You should be binding $newvalues to a string column in your database. Edited November 14, 2020 by gw1500se Quote Link to comment https://forums.phpfreaks.com/topic/311709-issue-to-save-values-in-database-with-multiple-checkboxs-in-crud/#findComment-1582377 Share on other sites More sharing options...
AccuCORE Posted November 14, 2020 Author Share Posted November 14, 2020 That is a test code, I thought it worked but nothing. This is one give me the "Array" in stored in database, what can I do for this code? <?php require_once "../lakota/config.php"; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (isset($_POST['faction'], $_POST['stations'], $_POST["active"], $_POST['pending'], $_POST['influence'], $_POST['id_fact'])) { $sql = "INSERT INTO lakotabgs (faction, stations, active, pending, influence, id_fact) VALUES (?,?,?,?,?,?)"; if ($stmt = $link->prepare($sql)) { $stmt->bind_param("ssssss", $_POST['faction'], $_POST['stations'], $_POST["active"], $_POST['pending'], $_POST['influence'], $_POST['id_fact']); if ($stmt->execute()) { header("location: ../lakota/index.php"); exit(); } else { echo "Error! Try again later."; } $stmt->close(); } } $link->close(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/311709-issue-to-save-values-in-database-with-multiple-checkboxs-in-crud/#findComment-1582382 Share on other sites More sharing options...
gw1500se Posted November 14, 2020 Share Posted November 14, 2020 You can't store an array in a database as a single column. I already suggested what to do. Alternatively you can store the array in a join table where each item is a column. 1 Quote Link to comment https://forums.phpfreaks.com/topic/311709-issue-to-save-values-in-database-with-multiple-checkboxs-in-crud/#findComment-1582386 Share on other sites More sharing options...
AccuCORE Posted November 14, 2020 Author Share Posted November 14, 2020 Thank you for your help, I found a new way with javascript Jquery 1.5.2 and it works fine. <script type="text/javascript"> function updateTextArea() { var allVals = []; $('.tagstates :checked').each(function(i) { allVals.push((i!=0?"\r\n":"")+ $(this).val()); }); $('#statesel').val(allVals).attr('rows',allVals.length) ; } $(function() { $('.tagstates input').click(updateTextArea); updateTextArea(); }); </script> <textarea name="active" id="statesel"></textarea> <div class="tagstates"> <label><input type="checkbox" value="2D Animation">2D Animation</label> <label><input type="checkbox" value="3D Animation">3D Animation</label> <label><input type="checkbox" value="Animatronics">Animatronics</label> <label><input type="checkbox" value="Architectural">Architectural</label> <label><input type="checkbox" value="Cartoon">Cartoon</label> <label><input type="checkbox" value="Cell Animation">Cell Animation</label> <label><input type="checkbox" value="Character Animation">Character Animation</label> <label><input type="checkbox" value="Cut & Paste">Cut & Paste</label> <label><input type="checkbox" value="Doodle">Doodle</label> <label><input type="checkbox" value="HDR">HDR</label> <label><input type="checkbox" value="High Speed">High Speed</label> <label><input type="checkbox" value="Illustration">Illustration</label> <label><input type="checkbox" value="Live Action">Live Action</label> <label><input type="checkbox" value="Macro">Macro</label> <label><input type="checkbox" value="Motion Design">Motion Design</label> <label><input type="checkbox" value="Motion Graphics">Motion Graphics</label> <label><input type="checkbox" value="Moving Installation">Moving Installation</label> </div> Quote Link to comment https://forums.phpfreaks.com/topic/311709-issue-to-save-values-in-database-with-multiple-checkboxs-in-crud/#findComment-1582402 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.