我正在尝试让我的php网站更新mysql数据库,具体取决于用户点击的哪一个(两个中的一个)链接,但是,我要么在数据库中获得两个链接,要么只获得同一个链接。我正在使用if语句,因为我试图区分所点击的链接。
我尝试使用$_GET['state']
,$_REQUEST['state']
,重新安排我的代码,或者使用2 if语句而不是1 else if语句,但仍然没有帮助。 (curr_stat
是我的表),这是我的代码:
<?php
//if links are clicked, will write to database the status
$connect_error= 'Sorry, we are experiencing connection problems.';
$db_name = "database name";
$connection = @mysql_connect("some host", "some username","password") or die($connect_error);
$db3 = @mysql_select_db($db_name,$connection) or die($connect_error);
//if statement
$unlock = '<a href="doorlock.php?state=1">UNLOCK </a> ';
echo $unlock;
$lock = '<a href="doorlock.php?state=0">LOCK</a>';
echo $lock;
if ($_GET['state'] == 1){
$one = 1;
mysql_query("INSERT INTO curr_stat(status) VALUES($one)");
}
elseif ($_GET['state'] == 0){
$zero = 0;
mysql_query("INSERT INTO curr_stat(status) VALUES($zero)");
}
?>
答案 0 :(得分:0)
你的if语句有一个问题:
如果未定义get变量state
,则条件($_GET['state'] == 0)
将返回true。
因此,如果您打开没有任何参数的站点,则会插入状态0的新行。
这就是为什么你要么是0和1或两次0。
相反,你应该测试一个不等于零的值。
$unlock = '<a href="doorlock.php?state=1">UNLOCK </a> ';
echo $unlock;
$lock = '<a href="doorlock.php?state=-1">LOCK</a>';
echo $lock;
if ($_GET['state'] == 1){
$one = 1;
mysql_query("INSERT INTO curr_stat(status) VALUES($one)");
}
elseif ($_GET['state'] == -1){
$zero = 0;
mysql_query("INSERT INTO curr_stat(status) VALUES($zero)");
}
?>
答案 1 :(得分:0)
我得到了它的工作!我最后尝试了不同的东西。我有一个html文件,它也保存状态,一个整数,1或0,根据点击的链接而改变,(doorlock.php?state = 0,它被锁定)。我使用一个函数打开该文件,并读取文件中的数据。我保存了那些数据,并有一个if语句写入数据库。这是我的代码:
<?php
//if links are clicked, will write to database the status
$connect_error= 'Sorry, we are experiencing connection problems.';
$db_name = "database name";
$connection = @mysql_connect("some host", "some username","password") or die($connect_error);
$db = @mysql_select_db($db_name,$connection) or die($connect_error);
//displays links
$unlock = '<a href="doorlock.php?state=1">UNLOCK </a> ';
echo $unlock;
$lock = '<a href="doorlock.php?state=0">LOCK</a>';
echo $lock;
//opens Lockstate.html file,and reads the data stored there
$file = "Lockstate.html";
$fh = fopen($file, 'r');
$data = fread($fh, 1);
fclose($fh);
$state = $data;
//if statement to know what to write to database depending on the new status
// & displays it on the page after being clicked
if($data == 1){
echo 'Door is now unlocked.';
$one = 1;
mysql_query("INSERT INTO curr_stat(status) VALUES($one)");
}
if ($data == 0){
echo 'Door is now locked.';
$zero = 0;
mysql_query("INSERT INTO curr_stat(status) VALUES($zero)");
}
?>
答案 2 :(得分:0)
<td> <?PHP echo "<a href='action.php?kisi=$id' onclick='return hs.htmlExpand(this, { objectType: 'iframe' } )'>$ad $soyad</a>";?></td>
单击链接时,其他用户无法单击(数据库无法更改)