1

I have developed a program to save the checked value of the checkbox to the database.

The selected value is not passing to the database properly. There are no syntax errors in the code.

<?php
$link = mysqli_connect("localhost", "root", "123456", "database");

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>

<form action="#" method="post">
<input type="checkbox" name="check_list[]" value="C/C++"><label>C/C++</label><br/>
<input type="checkbox" name="check_list[]" value="Java"><label>Java</label><br/>
<input type="checkbox" name="check_list[]" value="PHP"><label>PHP</label>  <br/>
<input type="submit" name="submit" value="Submit"/>

</form>

<?php

if(isset($_POST['submit']))
{//to run PHP script on submit
    if(!empty($_POST['check_list']))
        {
            foreach($_POST['check_list'] as $selected)
                {
                    if(isset($_POST['PHP'])) {
                $stmt = $link->prepare('INSERT INTO `checkbox` (`php_value`) VALUES ($selected)');
                        $stmt->bind_param('s', $stmt);
                        $stmt->execute();

                    }



} // if(isset($_POST['submit']))

    mysqli_close($link);
                }
        }


?>
0

1 Answer 1

2

$_POST['PHP'] doesn't exist to be checked, that's why checkbox values don't save to database.

Changed:

if(isset($_POST['PHP']))

With:

 if($selected!="") {

Updated Code:

<?php
$link = mysqli_connect("localhost", "root", "123456", "database");

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>

<form action="" method="post">
<input type="checkbox" name="check_list[]" value="C/C++"><label>C/C++</label><br/>
<input type="checkbox" name="check_list[]" value="Java"><label>Java</label><br/>
<input type="checkbox" name="check_list[]" value="PHP"><label>PHP</label>  <br/>
<input type="submit" name="submit" value="Submit"/>

</form>

<?php

if(isset($_POST['submit']))
{//to run PHP script on submit
    if(!empty($_POST['check_list'])) {
        foreach($_POST['check_list'] as $selected) {
            if($selected!="") {
                $stmt = $link->prepare('INSERT INTO `checkbox` (`php_value`) VALUES (?)');
                $stmt->bind_param('s', $selected);
                $stmt->execute();
                $stmt->close();
            }
        }

        mysqli_close($link);
    }
}


?>

Also added proper indentation for readability.

Sign up to request clarification or add additional context in comments.

7 Comments

Hey, @Karlo Kokkak Thanks for your answer. It's working now. but there is a slight issue that the same checkbox value saved twice in the table. Any clue?
Code works when tested locally. I posted an update anyways.
Thank you so much @Karlo Kokkak. All good now. one more question tho. What I need to do is keep the checkbox checked after it's been checked by a user. I am going to implement a login interface for this and if someone checked the checkbox, it should be shown to all the other users as well. Any suggestion how to do that?
I am out of words to thank you fam. Literally, you saved my life. It's working fine now. Wow, you're a living legend! God bless you my man! <3 @Karlo Kokkak
Hey as a gratitude towards the help you gave me I upvoted some of your questions & answers. :) @Karlo Kokkak
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.