-1

我试图让我的 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 &nbsp;</a>&nbsp;';
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)");
}
?>
4

3 回答 3

0

你的 if 语句有一个问题:

state如果未定义get 变量,则条件($_GET['state'] == 0)将返回 true。

因此,如果您在没有任何参数的情况下打开站点,则会插入状态 0 的新行。

这就是为什么你要么同时得到 0 和 1,要么得到 0 的两倍。

相反,您应该测试一个不等于零的值。

$unlock = '<a href="doorlock.php?state=1">UNLOCK &nbsp;</a>&nbsp;';
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)");
}
?>
于 2013-04-22T00:34:45.073 回答
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 &nbsp;</a>&nbsp;';
    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)");
}
?> 
于 2013-04-23T04:03:00.953 回答
0

<td> <?PHP echo "<a href='action.php?kisi=$id' onclick='return hs.htmlExpand(this, { objectType: &#39;iframe&#39; } )'>$ad $soyad</a>";?></td>

单击链接时,其他用户无法单击(数据库无法更改)

于 2013-11-06T16:19:55.500 回答