PHP MySQL 检查ID是否存在

标签 php mysql

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

我想检查该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

关于PHP MySQL 检查ID是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22441178/

相关文章:

php - MySQL 选择 "previous"max(id) WHERE

php - 如何从作者 ID 获取 WordPress 帖子总数?

mysql - 表中查询数据的问题

Mysql查询从 "not in"子句返回值

Mysql - 如何将日期从 ISO 8601 格式转换并保存到 mysql 列?

php_network_getaddresses : getaddrinfo failed: Name or service not known

php - 将值保存为其他表值

javascript - 查找客户端计算机名称

mysql - 丑陋且缓慢的 mysql 查询

PHP PDO 准备语句缓存