what is the procedure to insert 1 only 1 checkbox value in mysql with php?

2

In the search I've done about checkbox I've seen several alternatives for inserting checkbox values, using array methods and loops.

But I want to insert only 1, it is still necessary to use array and loops.

The following code is returning error, as best suited as possible.

<form method="post" action="trata_check.php">
  <input id="" type="checkbox" name="um" value="um">Um
  <input id="" type="submit"  value="gogopls">
</form>

trata_check.php

<?php
include 'conect.php';

$um = $_POST['um'];

$sql = 
"INSERT INTO part_1
(um)
VALUES 
('$um')";

if ($con->query($sql) === TRUE) {
    echo "realizado";
} else {
    echo "Error: " . $sql . "<br>" . $con->error;
}

$con->close();

?>

I'm not finding a solution.

    
asked by anonymous 17.06.2016 / 23:55

1 answer

1
<form method="post" action="trata_check.php">
  <input type="checkbox" name="um" value="1" id="" <?= (isset($dados['um']) && $dados['um'] == 1) ? 'checked' : '' ?> >Um
  <input id="" type="submit"  value="gogopls">
</form>

<?php
include 'conect.php';

$um = 0;
if(isset($_POST['um']) && !empty($_POST['um'])){
 $um = 1;
}
 $sql = "INSERT INTO part_1 (um) VALUES ('{$um}')";
  if (!$con->query($sql)) {
       echo "Error: " . $sql . "<br>" . $con->error;
  } 
$con->close();
echo "realizado";
    
18.06.2016 / 03:01