繁体   English   中英

PHP MySQL检查ID是否存在

[英]PHP MySQL Check if ID exists

我做了类似的事情,但它不起作用。

我想检查数据库中的ID是否存在,如果是,请插入数据,如果不再尝试。

我使用此代码进行检查,但似乎不起作用。

我输入正确或不正确它进入accepted.html页面,它不会在我的时间表中插入数据。 我希望它检查ID是否为真在时间表中插入数据。

 if ($player == null || $witness == null) {
        echo "Do not use the same player and witness or maybe you forgot to add it...";
        echo '<META HTTP-EQUIV="Refresh" Content="2; URL=index.php">';
    } else {
        $sql = "SELECT (`id`) FROM player WHERE id={$player} ";
        $result = mysql_query($sql);
        if ($result == true) {
            $sql2 = "INSERT INTO `members` (`player`, `hours`, `minutes`, `witness`, `type`,`date`,`comments`) VALUES ('$player', '$hours', '$minutes', '$witness', '$type', '$date','$comments')";
            $result2 = mysql_query($sql2);

            if ($result2)
            {
                header('Location: accepted.html');
            }
            else {
                echo mysql_error();
            }
        }else{
            header('Location: contactAdmin.html');
        }
    }
$sql = "SELECT (`id`) FROM $tbl_name WHERE id=$$player ";

应该

$sql = "SELECT `id` FROM $tbl_name WHERE id= $player ";

你有id=$$player

您的代码中存在一些问题。
有一点是第一个sql查询是使用错误的变量$$player应该是$player

$sql = "SELECT `id` FROM $tbl_name WHERE id= $player ";

其次,您的插入查询永远不会执行,因为您传入了错误的变量。 你传入$sql而不是$sql2

$sql2 = "INSERT INTO `members` (`player`, `hours`, `minutes`, `witness`, `type`,`date`,`comments`) VALUES ('$player', '$hours', '$minutes', '$witness', '$type', '$date','$comments')";
$result2 = mysql_query($sql2);

第三,你的header是在错误的地方。 它应该在开幕后放置{

if ($result2) {
    header('Location: accepted.html');
} else {
    echo mysql_error();
}

我发现3个错误... 1个$ sql查询,2个,标头加载,3个,放置标题vs mysql_error。

  if ($player == null || $witness == null) {
            echo "Do not use the same player and witness or maybe you forgot to add it...";
            echo '<META HTTP-EQUIV="Refresh" Content="2; URL=index.php">';
        } else {
            $sql = "SELECT (`id`) FROM $tbl_name WHERE id='{$player}' ";
            $result = mysql_query($sql);
            if (mysql_num_rows($result) <= 0) {
                $sql2 = "INSERT INTO `members` (`player`, `hours`, `minutes`, `witness`, `type`,`date`,`comments`) VALUES ('$player', '$hours', '$minutes', '$witness', '$type', '$date','$comments')";
                $result2 = mysql_query($sql2);

                if ($result2)
                {
                     header('Location: accepted.html');
                }
                else {
                    echo mysql_error();
                }
            }else{
                header('Location: contactAdmin.html');
            }
        }

---编辑第四:$ result2应该使用$ sql2作为查询。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM