mercredi 9 décembre 2020

Insert NULL Value when Checkbox is not selected

the scenario:

i have a dropdownlist and a checkbox. When a value from The Dropdownlist is choosed and the Checkbox is selected, their will be 2 Values be submitted to the Database. But when the Checkbox is not selected, nothing gets submitted to the database. I want to achieve that when the Checkbox is not selected, their should be submitted "null" for that value but the value from the dropdown list should be still submitted (The Value from the dropdown list and the Checkbox are from two different tables, but are foreign keys in another table)

This is the Dropdown list:

<div class="form-group">
<label for="formGroupExampleInput2">Kostenstelle</label>

<?php
$sql4 = "SELECT kstnr, kostenstelle FROM kostenstelle";
$result4 = $connection->query($sql4);

if ($result4->num_rows > 0) {
    echo "<select name='kstnr'>";
    // output data of each row
    while($row = $result4->fetch_assoc()) {
        echo "<option value='" . $row['kstnr'] . "'>" . $row['kostenstelle'] . "</option>";
    }
    echo "</select>";
}
?>
</div>

And this is the checkbox with the submit function:

<input type="checkbox" name="psp-ele" id="Investnr" ><label for="Element">PSP Element</label>
    
</div>
    
<?php
if (isset($_POST['submit'])){
    
    var_dump($_POST);
    $kstnr = $_POST['kstnr'];
    
    if (isset($_POST['psp-ele'])){
        $sqlpsp = "SELECT InvestPSPID FROM investpsp WHERE kstnrr=$kstnr";
        $result1 = $connection->query($sqlpsp);
        if ($result1->num_rows == 1){
            $rowpsp = $result1->fetch_row();
            $investpspid = $rowpsp[0];
        }   else{
            $investpspid ='null';
        }
            
        var_dump($_POST['kstnr'], $investpspid);
        $sql12 = $query  = " INSERT INTO kunde_projekt 
                            (kundenname_projekt, kstnr, info_uebergabe, 
                            standort, InvestPSPID) 
                    VALUES ('$kundenname_projekt','$kstnr', '$info_uebergabe', 
                            '$standort', '$investpspid')";
    
        var_dump($sql12);
        $result2 = $connection->query($sql12);
        var_dump($connection->error);
        var_dump($result2);
    }
}
$kundeprojekt_id=$connection->insert_id;
?>

Does anyone see the mistake an can help? :)



Aucun commentaire:

Enregistrer un commentaire