Jump to content

Issue to save values in database with multiple checkboxs in CRUD


Recommended Posts

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 by AccuCORE

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 by gw1500se

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();
}
?>

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>

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.