1

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.

1
  • So stop using a deprecated API. Welcome to 2015. Get modern and take a look at mysqli/PDO AND prepared statements Commented Jan 1, 2015 at 12:57

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you this helped. Here is some minor changes that updated the database: if(isset($_GET['alarm'])){ $alarm = 1;}else{ $alarm = 0;} $sql = "UPDATE elements SET value=" .$alarm. " WHERE element='alarm'";
Is there some way I can load the checkbox as "1" or "checked" from the database?
Yes, with this approach you store either 1 or 0 in the alarm field, then reading that field will give you either of the values. Eventually to display it right in the page, you can add a checked attribute to the checkbox field if the read value is one!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.