-1

I have the following select in my form, which I need to save in my database:

<select name="openhours" class="form-control selectpicker" >
    <option value=" " >Open Sundays</option>
    <option>Yes</option>
    <option>No</option>
</select>

I made a boolean column in MySQL database. But first of all I cannot figure out if a boolean column still is a name for tinyint?

I have made the following insert function which is working perfectly fine. But how can I bind a boolean in my insert function?

<?php

if(isset($_POST["headline"], $_POST["description"])) {
    $head = trim($_POST["headline"]);
    $desc = trim($_POST["description"]);


    $stmt = $mysqli->prepare("INSERT INTO stores (headline, description) VALUES (?, ?)");
                    // bind variable
                    $stmt->bind_param('ss', $head, $desc);   // bind

                    // execute prepared statement 
                if ($stmt->execute()) {
                    $success = true;
                }

                // close statement
                $stmt->close();

                // close connection
                $mysqli->close();   

if($success) {
    echo "Insert Succesfull";
} else {
    echo "Failed: " .  $stmt->error;
  }
}
?>
McDuck4
  • 662
  • 1
  • 10
  • 33

2 Answers2

2

You should add these lines to your code.

Give options a value

<option value="1">Yes</option>
<option value="0">No</option>

To get posted value

$oh = isset($_POST["openhours"])&&$_POST["openhours"]=="1"?1:0;

Update query

"INSERT INTO stores (headline, description,openhours) VALUES (?, ?,?)"

Bind it

$stmt->bind_param('ssi', $head, $desc, $oh);
Оzgur
  • 432
  • 2
  • 10
  • Thank you for the answer. Is that all to set my booelan? I thought it was more complicated. – McDuck4 Apr 17 '17 at 20:33
  • @McDuck4 yes, it should update your db accordingly. If any problems just comment me. If works accept as answer please. – Оzgur Apr 17 '17 at 20:37
1

You can Use TINYINT(1). BOOLEAN are synonyms of TINYINT(1) in MySQL . Zero is false, anything else is true

check the MySQL manual for this : https://dev.mysql.com/doc/refman/5.7/en/numeric-type-overview.html

this answer should give you the right explanation of your issue : Which MySQL data type to use for storing boolean values

lastly make sure to give values to your select options thanks !

Community
  • 1
  • 1
lotfio
  • 1,916
  • 2
  • 18
  • 34