André
André

Reputation: 82

Get value from checkbox when checked and send it to mySQL

I am building a website interface to control a Arduino through a webserver on my Raspberry for a university project. I am using php, python and a mySQL database to set and store the "status" of the equipment. I don't have much experience in php or python but im making dew. My problem however is using a checkbox to switch these devices on or off. I created a connection to the database in php. I have researched alot of giberish that doesn't seem to relate to my project. Here is my test.php code:

The PHP:

//$alarm = $_GET["alarm"];

>    $sql = mysql_query("UPDATE elements SET value=" .
mysql_real_escape_string($_GET["alarm"])
. " WHERE element='alarm'");

>    if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {   echo "Error updating record: " . $conn->error;
}

>    $sql = "SELECT value FROM elements WHERE element='Alarm'";
>    result = $conn->query($sql);

>    if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
    echo "Value: " . $row["value"]. "<br>";
}
} else {
echo "0 results";
}

$conn->close();

The HTML:

`<form id="alarm" action="test.php" method="get">`

`<input type="checkbox" name="alarm" value="1">Alarm`

`</form>`

Please if you can just point me on the right direction. I appreciate any feedback in advance.

Upvotes: 1

Views: 791

Answers (1)

deviloper
deviloper

Reputation: 7240

the value of the inputs with the type checkbox are sent if only they are checked so you better set $alarm accordingly:

$alarm = 'Off'; //or preferably $alarm = false;
if(isset($_GET['alarm']))
$alarm = 'On'; // or preferably $alarm = true;

Then run your update query to set the value of the field alarm as you have recieved from the form!

Upvotes: 1

Related Questions