4

Something basic but I don’t see why it doesn’t work I want to save the state of a checkbox in a database, type Boolean, if checked 1 else 0 here is my code:

<input type="checkbox" name="stok"  value="1"
  <?php if(isset($_POST['stok'])) echo "checked='checked'"; ?>
/>
<?php
    if (isset($_POST['stok']) && !empty($_POST['stok']) && $_POST['stok'] === 'on')               
        $stok = 1;  
    else 
        $stok = 0;      

    mysql_query ("update tab set etat_vendu=$stok where id=$big");
?>  

//this is my var $stok=$row['stok'];   
4
  • $_POST['stok'] === 'on' should be $_POST['stok'] == '1', I can't see the value "on" Commented May 23, 2013 at 12:16
  • Hi @chandresh : No don't work with =='1' Commented May 23, 2013 at 12:30
  • means what don't work with ==1? Commented May 23, 2013 at 12:34
  • What is $big, can you post your whole code? Commented May 23, 2013 at 12:40

6 Answers 6

2

This will work:

if(isset($_POST['stok'])){
    //$stok is checked and value = 1
    $stok = $_POST['stok'];
}
else{
    //$stok is nog checked and value=0
    $stok=0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, I made the store in database but when I return to modify the product the stock checkbox is not check !!
1
$stock = $_POST['stok'] ?? 0; // Null coalescing Since PHP7

Comments

0

Try changing $_POST['stok'] === 'on' into $_POST['stok'] === '1' in your if.

Comments

0

Your if-statement can be shorter, way shorter since you're already passing a value :)

if (isset($_POST['stok']))
    $stok = $_POST['stok']; // equals 1 since that's the given value
else
    $stok = 0;

Edit: don't forget to check your UPDATE() statement:

mysql_query("UPDATE tab SET etat_vendu='".$stok."' WHERE id='".$big."'");

Add data sanitation where necessary.

1 Comment

What is the problem? Isn't the database updated or is the checkbox not checked while the value in the database is 1?
0

I think I know what you want now

$result = mysqli_query($con,"SELECT etat_vendu FROM tab WHERE id=".$big);
    while($row = mysqli_fetch_array($result))
    {
        $stok = $row['etat_vendu'];
    }

And your checkbox

    <form action='index.php' method='post'>
<?php
    if($stok != 0){
    echo "<input type='checkbox' name='stok'  value='1' checked='checked'/>";
    }
    else{
    echo "<input type='checkbox' name='stok'  value='1'/>";
    }
?>
    <input type='submit' name='submit' value='submit'>
    </form>
<?php
            if(isset($_POST['stok'])){
                //$stok is checkend and value = 1
                $stok = $_POST['stok'];
            }
            else{
                $stok=0;
            }
?>

1 Comment

No prob, glad I could help
0

As simple as that

if($fetch['proved'] === '1') {
   $p1 = 1;
   $p0 = 0;
} else {
   $p1 = 0;
   $p0 = 1;
}

<select name="proved">
  <option value="<?php echo $p0 ?>"><?php echo $p0 ?></option>
  <option selected value="<?php echo $p1 ?>"><?php echo $p1 ?></option>
</select>

Comments

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.