What is the procedure for entering 1 only 1 checkbox value in mysql with php?

Asked

Viewed 51 times

2

In the research I have done on checkbox I have seen several alternatives for inserting checkbox values, using methods with array and loops.

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

The following code is returning error, how to adapt as simply 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 .

  • I need the brackets in the tmb input?

  • I was wrong, there is no array in your code. How are you submiting the form?

  • <input type"Submit" value="gogopls">

  • is receiving the value 1, not the string...

  • What a mistake that happens?

  • No error but receives 1 as value....

  • 1

    I don’t know how but now it has.... mygood Gratz @diegofm

Show 2 more comments

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";

Browser other questions tagged

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