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>