Hello I am trying to collect some data from data base, change the value and then insert new value back into data base but i cannot get it to work. At the moment a button is clicked that sends value 1 to this script. database value for for light1 is obtained. If Get received is = 1 then toggle light 1 and insert new value back into database. If I run this all separately i can receive the data, toggle the data and insert it. But it will not all run together on one page. What am i doing wrong.
<?php
// but GET value into variable;
$light1recieved = ($_GET['light1']);
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="14Odiham"; // Mysql password
$db_name="sean"; // Database name
$tbl_name = "lights";
// Connect to server and select database.
$link = mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
//Retrieve all data from the table
$sql = "SELECT * FROM $tbl_name WHERE id = 1 LIMIT 1";
$result1 = mysql_query($sql, $link);
// if successfully, displays message "Successful".
if($result1){
echo "Successful";
echo "<BR>";
}
else {
echo "ERROR";
}
while ($row = mysql_fetch_assoc($result)){
$light1 = $row['light1'];
}
echo $light1;
if ($light1recieved == "1"){
$light1 = !$light1;
}
// Insert data into mysql
$sql = "UPDATE $tbl_name SET light1=$light1";
$result = mysql_query($sql);
if($result){
echo "Successful";
echo "<BR>";
}
else {
echo "ERROR";
}
echo $light1;
?>