Capturing the Boolean value of a PHP checkbox

Asked

Viewed 818 times

3

I am making a product form, where the user needs to inform if the product is used or not, however I am having difficulties in capturing the value of this checkbox, if it is 1 or 0, in the mysql the column is already set as boolean with default 0

logica-add-product.php

 <?php
    include("conecta.php");

    #Variáveis para cadastrar
    $nome = $_POST["nome-produto"];
    $preco = $_POST["preco-produto"];

    $descricao = $_POST["descricao-produto"];

    #Caso de verificação da variável booleana
    $usado = $_POST["usado-produto"];
    if(array_key_exists($usado, $_POST)) {
        $usado = "true";
    }else{
        $usado = "false";
    }

    #Query de inserção
    $query = "insert into produtos(nome, preco, descricao, usado) values('{$nome}', '{$preco}', '{$descricao}', {$usado})";

    #Variável para executar a inserção
    $retornoInsercao = mysqli_query($conexao, $query);

    #Teste para verificar inserção
    if($retornoInsercao){
        header("Location:index.php");
        die();
    }else{

    };

?>

add-products.php

<?php
    include("header.php");
    include("conecta.php");
?>

 <form action="logica-adiciona-produto.php" method="post">
    <fieldset>
        <label>Nome:</label>
        <input type="text" name="nome-produto">
        <label>Preço:</label>
        <input type="number" name="preco-produto">
    </fieldset>
    <fieldset>
        <label>Usado ?</label>
        <input type="checkbox" name="usado-produto" value="true">
        <label>Categoria:</label>
        <select name="">

        </select>
        <label>Descricao:</label>
        <textarea name="descricao-produto"></textarea>
    </fieldset>
    <fieldset>
        <input type="submit">
    </fieldset>
 </form>
  • 1 and 0 same !!! 1 to true and 0 to false

  • continues to receive as 0 in the bank

  • If you mean the code! Neh?

  • even when I mark the checkbox, it registers as false on the server (0)

1 answer

2


Your code is wrong, you are sending a variable that does not match the key name, note:

Change:

#Caso de verificação da variável booleana
$usado = $_POST["usado-produto"];
if(array_key_exists($usado, $_POST)) {
    $usado = "true";
}else{
    $usado = "false";
}

therefore:

$usado = array_key_exists('usado-produto', $_POST) ? 1 : 0;

Explanation:

The structure of the function array_key_exists

bool array_key_exists ( mixed $key , array $array )

where the first parameter is the name of the key to search inside the array and the according to the array properly, that returns true (true) or false (false) in the result, whether or not ...

or also

$usado = isset($_POST['usado-produto']) ? 1 : 0;

Reference: array_key_exists

  • wanted something approximate with what I did, because the teacher in the course used the same method that I am using

  • @Murilogambôa is the first code array_key_exists, Isn’t that it ? just remembering that you put the wrong vestments, IE, was doing wrong...

  • the teacher in the course basically used this > <tr>&#xA; <td></td>&#xA; <td><input type="checkbox" name="usado" value="true"> Usado&#xA;</tr> ... $usado = $_POST['usado'];&#xA;&#xA;if(array_key_exists('usado', $_POST)) {&#xA; $usado = true;&#xA;} else {&#xA; $usado = false;&#xA;}

  • 'usado' is different from $usado and another thing if your teacher taught $_POST['usado']; without checking if there is also a wrong practice, beware.

  • You are passing the variable as parameter of "array_key_exists" what you should pass is the name of the "key" that you want to check if it exists.

  • @Jhonnyrogervirgiliodasilva was what I reported in the reply.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.