-4

我目前正在做一个投票网站。我想知道我是否可以将 PHP 中的“if 条件”放在我在 MySQL 数据库中定义的变量上?我有一个名为“用户”的数据库。这个数据库有三列——用户名、密码和标志。用户名和密码是预定义的。'flag' 默认设置为 0。flag 值为 0 表示用户尚未投票。我想在标志上使用“如果条件”来检查用户是否已经投票。到目前为止,我的代码会将标志更新为 1,但是 if 条件似乎无法正常工作。可能是我没有从我的数据库中正确读取“标志”。我还使用 cookie 来存储用户的用户名,然后读取该 cookie 为该特定用户设置 flag=1。我现在只需要知道:

  1. 从数据库中读取 flag 的值并
  2. 对标志应用 if 条件

谢谢。

编辑:我需要根据标志的值将用户带到不同的页面。如果该特定用户的标志为 0,我希望该用户转到“谢谢”投票页面,否则如果标志为 1,我将他带到“您已经投票”页面。

这是代码:

<?php

$host="localhost"; // Host name 
$username="dbxxxxx"; // Mysql username 
$password="password"; // Mysql password 
$db_name="dbxxxxx_users"; // Database name 
$tbl_name="users"; // Table name 


// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$user = $_COOKIE["details"];  //cookie details has the username the user used to log in

$SQL = "SELECT flag FROM users WHERE Username='$user'"; //THIS IS WHERE I AM TRYING TO READ FLAG, HOWEVER IT DOESN'T WORK. I NEED AN ALTERNATE STATEMENT FOR WHAT I AM DOING HERE.
$flag = mysql_query( $SQL );   
$db_field = mysql_fetch_assoc($flag);  

if($db_field==0)     {       
        mysql_query("UPDATE result SET Votes=Votes+1 //if flag in database = 0 
        WHERE Name='Candidate1'");  //updates result for candidate1 if the user voted for 1
        $user = $_COOKIE["details"];  //reading the cookie again. can be omitted.

        mysql_query("UPDATE users SET flag=1   //changing flag to 1 so user cannot vote again
        WHERE Username='$user'");

        header("location: http://www.lithuaniavote.com/thankyou.html");
 }

else    //flag != 1 or user has already voted
{
    header("location: http://www.lithuaniavote.com/alreadyvoted.html");
}
?>
4

5 回答 5

1
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

if ($stmt = mysqli->prepare("SELECT flag FROM users WHERE username=?")) {

    /* bind params */
    mysqli_stmt_bind_param($stmt, $user);

    /* execute query */
    mysqli_stmt_execute($stmt);

    /* bind result variables */
    mysqli_stmt_bind_result($stmt, $flag);

    if($flag != 0) {
       if ($upd = mysqli->prepare("UPDATE users SET flag = 1 WHERE username=?")) {
           mysqli_stmt_bind_param($upd, $user);
           mysqli_stmt_execute($upd);

           /* did not vote. succes view */
       } 
    } else {
      /* voted already. error view  */
    }
}

$mysqli->close();
于 2013-04-16T17:46:28.390 回答
1

一旦 Query 运行,这将做到这一点:

if( $row['flag'] == 0 ) {
    header('Location: http://www.example.com/thank_you.php');
} else if ($row['flag'] == 1 ) 
    header('Location: http://www.example.com/you_have_voted.php');
} 
于 2013-04-16T17:42:50.183 回答
1

好吧,首先你应该从数据库中选择关于用户的数据

<?php
$flag = false;
// making query text
$query = "SELECT * FROM `users` WHERE `username` = '".$username."'";    
// executing mysql query
$sel = mysql_query($query);
if ($sel AND mysql_num_rows($sel) >= 1) { // if query was successfully executed
    $row = mysql_fetch_object($sel); // making object ($row) from DB data (now you have $row->username, $row->password and $row->flag)
    $flag = $row->flag; // write flag value from DB into php variable
}

if ($flag != false) {
    // user hadn't wote
    // so we can present voting poll here or execute update query if user just voted
} else {
    // user already woted
}
?>
于 2013-04-16T17:41:40.270 回答
1

您可以使用 SQL 执行此操作:

$sql = "select IF(flag='1','Voted','Not Voted') AS 'status' from users where username='$username'";

在代码中,您将执行以下操作:

switch($status){

  case 'Not Voted':
  include('newVoter.php');
  break;

  default:
  include('voted.php');

}

如果您在检查之前没有打印任何内容以进行筛选,您可以执行以下操作:

switch($status){

  case 'Not Voted':
  header('Location: newVoter.php');
  break;

  default:
  header('Location: voted.php');

}
于 2013-04-16T17:44:31.563 回答
1

我不明白为什么你需要检查PHP侧面的标志。您可以根据标志添加过滤器。

SELECT ... FROM votes WHERE flag = 0

否则,您是否正在寻找这样的东西?

$sql = "SELECT flag FROM votes WHERE username = 'bob'";

$result = mysqli_query($sql);
$row = mysqli_fetch_assoc($result);
if( $row['flag'] == 0 ) {
    //do something
}
于 2013-04-16T17:36:22.957 回答