简体   繁体   中英

Problems to save the value of a checkbox in the database

I have doubts ... I am not able to save my information checkbox in the database, and so little rescue in a search screen, if that is done manually in the database ...

$ TRAMENTO1 = (@$ _POST ["TRAMENTO1 "]=='true') ? $ _POST ["TRAMENTO1"]: 'false';

<Input name="TRAMENTO1" 
    type="checkbox" 
    id="TRAMENTO1" 
    value="true" 
    php if ($TRAMENTO1 == true) {echo "checked"}> 
    />

Do so and only get from my bank to respond "false" even if my checkbox is checked. and only the first two checkbox yet. If you can help me I am very grateful.

Cleiton Capristano

I found a few of things wrong.

  1. One, the name of your input in the HTML is "TRAMENTO1 " but the name as called in PHP is "TRAMENTO1".
  2. There are no brackets around your PHP code within the input*.
  3. There are no brackets between the HTML and the PHP*.
  4. $ TRAMENTO1 and $ _POST don't work as a variable name. No spaces allowed.

As a side note, you might think about generally cleaning things up a bit:

<?php
$TRAMENTO1 = isset($_POST['TRAMENTO1']) ? 'true' : 'false';
?>
<input name="TRAMENTO1" type="checkbox" id="TRAMENTO1" value="true"<?php echo ($TRAMENTO1 == 'true' ? ' checked="checked"' : ''); ?> />

*I see in your revision history that you copied over the code without applying the correct Markdown characters, so these concerns might be moot for the original code.

I don't know if this was broken from the beginning or happened when you copied it to SO. Anyway, here's the code you're looking for:

<?php
    $TRAMENTO1 = $_POST["TRAMENTO1"] ? 1 : 0; // This you can put into the database
?>

<input name="TRAMENTO1" type="checkbox" id="TRAMENTO1" value="true" <?php if ($_POST['TRAMENTO1']) echo 'checked="checked"'; ?> />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM