-2

我正在开发一个用户可以单击项目的项目。如果用户之前点击过它,那么当他尝试再次点击它时,它不应该工作或在数据库上插入值。当我单击第一个项目(我通过 id 直接从数据库中显示项目)时,它会插入到 DB 中,然后当我再次单击它时,它会起作用(给我错误代码)不会插入到 DB 中。当我点击它们时的所有其他项目,即使我第二次、第三次、第四次点击它们,它们也会全部插入数据库。请帮助各位。谢谢

<?php

session_start();
$date = date("Y-m-d H:i:s");

include("php/connect.php");

$query = "SELECT * FROM test ORDER BY `id` ASC LIMIT 3";
$result = mysql_query($query);

if (isset($_SESSION['username'])) {

    $username = $_SESSION['username'];

    $submit = mysql_real_escape_string($_POST["submit"]);

    $tests = $_POST["test"];

    // If the user submitted the form.
    // Do the updating on the database.
    if (!empty($submit)) {
        if (count($tests) > 0) {
            foreach ($tests as $test_id => $test_value) {
                $match = "SELECT user_id, match_id FROM match_select";
                $row1 = mysql_query($match)or die(mysql_error());
                while ($row2 = mysql_fetch_assoc($row1)) {
                    $user_match = $row2["user_id"];
                    $match = $row2['match_id'];
                }
                if ($match == $test_id) {
                    echo "You have already bet.";
                } else {
                    switch ($test_value) {
                        case 1:
                            mysql_query("UPDATE test SET win = win + 1 WHERE id = '$test_id'");
                            mysql_query("INSERT INTO match_select (user_id, match_id) VALUES ('1','$test_id')");
                        break;
                        case 'X':
                            mysql_query("UPDATE test SET draw = draw + 1 WHERE id = '$test_id'");
                            mysql_query("INSERT INTO match_select (user_id, match_id) VALUES ('1','$test_id')");
                        break;

                        case 2:
                            mysql_query("UPDATE test SET lose = lose + 1 WHERE id = '$test_id'");
                            mysql_query("INSERT INTO match_select (user_id, match_id) VALUES ('1','$test_id')"); 
                        break;
                        default:

                    }
                }
            }
        }
    }

    echo "<h2>Seria A</h2><hr/>
        <br/>Welcome,".$username."! <a href='php/logout.php'><b>LogOut</b></a><br/>";

    while ($row = mysql_fetch_array($result)) {

        $id = $row['id'];
        $home = $row['home'];
        $away = $row['away'];
        $win = $row['win'];
        $draw = $row['draw'];
        $lose = $row['lose'];

        echo "<br/>",$id,") " ,$home, " - ", $away;

        echo "
            <form action='seria.php' method='post'>
                <select name='test[$id]'>        
                    <option value=\"\">Parashiko</option>
                    <option value='1'>1</option>
                    <option value='X'>X</option>
                    <option value='2'>2</option>
                </select>
                <input type='submit' name='submit' value='Submit'/>
                <br/>
            </form>
            <br/>";        

        echo "Totali ", $sum = $win+$lose+$draw, "<br/><hr/>";
    }
} else {
  $error = "<div id='hello'>Duhet te besh Log In qe te vendosesh parashikime ndeshjesh<br/><a href='php/login.php'>Kycu Ketu</a></div>";
}

?>
4

3 回答 3

1

你的问题在这里:

$match = "SELECT user_id, match_id FROM match_select";
$row1 = mysql_query($match)or die(mysql_error());
while ($row2 = mysql_fetch_assoc($row1)) {
    $user_match = $row2["user_id"];
    $match = $row2['match_id'];
}

您没有正确检查它。您必须检查相关条目是否match_select存在。否则,将始终等于数据库中最后插入的行的字段:user_idmatch_id$matchmatch_id

$match = "SELECT * 
    FROM `match_select` 
    WHERE `user_id` = '<your_id>'
    AND `match_id` = '$test_id'";
$matchResult = mysql_query($match)or die(mysql_error());
if(mysql_num_rows($matchResult)) {
    echo "You have already bet.";
}

顺便说一下,考虑使用PDOormysqli来操作数据库。mysql_功能已弃用:

http://www.php.net/manual/fr/function.mysql-query.php

于 2013-06-18T09:56:34.833 回答
0

如果数据已经存在,则通过查找表来验证记录的插入。

例如,最简单的方法是

 $query = "SELECT * FROM match_select WHERE user_id = '$user_id'";
 $result = mysql_query($query);

 if(mysql_num_rows($result) > 0)
 {
   // do not insert
 }
 else
 {
   // do something here..
 } 
于 2013-06-18T09:48:31.780 回答
0

在你的表单中你有<select name='test[$id]'>(每个项目一个),然后当你提交你得到的表单时你$tests = $_POST["test"];不需要在表单中指定索引并且可以简单地做<select name='test[]'>,你最终可以添加一个带有 id 的隐藏字段<input type="hidden" value="$id"/>。第二部分是目前不好的验证;您可以通过查询简单地检查项目是否已经存在于数据库中

于 2013-06-18T09:51:04.107 回答