获取mySql数据然后更新它

时间:2017-03-08 21:27:50

标签: php mysql

您好我正在尝试从数据库中收集一些数据,更改值然后将新值重新插入数据库但我无法使其工作。此时单击一个按钮,将值1发送到此脚本。获得light1的数据库值。如果Get received = 1,则切换指示灯1并将新值重新插入数据库。 如果我单独运行这个,我可以接收数据,切换数据并插入它。但它不会在一个页面上一起运行。我做错了什么。

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

0 个答案:

没有答案