php - 只检查第一条记录,而不是所有

标签 php mysql mysqli

编辑:更新!让第一部分工作。但是,我不确定如何在同一个 IF() 语句中检查其他变量。任何人都可以帮我吗?单个 if 语句将拒绝与输入名称完全相同的类。但是,我需要它也拒绝相等的日期和时间。

        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Register Diver</title>
<link rel="stylesheet" href="php_styles.css" type="text/css" />
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1>Aqua Don's Scuba School</h1>
<h2>Registration Confirmation</h2>
<?php
$DiverID = $_GET['diverID'];
if (empty($DiverID))
    exit("<p>You must enter a diver ID! Click your browser's Back button to return to the previous page.</p>");
$DBConnect = @mysqli_connect("localhost", "students", "password")
    Or die("<p>Unable to connect to the database server.</p>"
    . "<p>Error code " . mysqli_connect_errno()
    . ": " . mysqli_connect_error()) . "</p>";
$DBName = "scuba_school";
@mysqli_select_db($DBConnect, $DBName)
    Or die("<p>Unable to select the database.</p>"
    . "<p>Error code " . mysqli_errno($DBConnect)
    . ": " . mysqli_error($DBConnect)) . "</p>";

$TableName = "registration";
$SQLstring = "SELECT * FROM $TableName";
$QueryResult = @mysqli_query($DBConnect, $SQLstring);
if (!$QueryResult) {
    $SQLstring = "CREATE TABLE registration (diverID SMALLINT, class VARCHAR(40), days VARCHAR(40), time VARCHAR(40))";
    $QueryResult = @mysqli_query($DBConnect, $SQLstring)
        Or die("<p>Unable to create the registration table.</p>"
        . "<p>Error code " . mysqli_errno($DBConnect)
        . ": " . mysqli_error($DBConnect)) . "</p>";
    echo "<p>Successfully created the registration table.</p>";
}
?>

<?php
$Class = $_GET['class'];
$Days = $_GET['days'];
$Time = $_GET['time'];
$DiverID = $_GET['diverID'];

$DBConnect = mysqli_connect("localhost", "students", "password");
$DBName = "scuba_school";
@mysqli_select_db($DBConnect, $DBName)
    Or die("<p>Unable to select the database.</p>"
    . "<p>Error code " . mysqli_errno($DBConnect)
    . ": " . mysqli_error($DBConnect)) . "</p>";


$sqlString= "SELECT * FROM `registration` WHERE `diverID` = $DiverID AND `class` = '$Class' AND `days` = '$Days' AND `time` = '$Time'";
$QueryResult = mysqli_query($DBConnect, $sqlString) or die("MySQL error: " . mysqli_error($DBConnect) . "<hr>\nQuery: $QueryResult");  
$row = mysqli_fetch_assoc($QueryResult);

if ($row["class"] == $Class)
{

echo "<p>You are already registered for $Class</p>";
    }

    elseif($row["days"] == $Days && $row["time"] == $Time)
    {
        echo "<p>There is a conflict with $Days or $Time</p>";
        }
else
{
 $SQLstring = "INSERT INTO $TableName VALUES('$DiverID', '$Class', '$Days', '$Time')";
    $QueryResult = @mysqli_query($DBConnect, $SQLstring);
    echo "<p>You are registered for $Class on $Days, $Time. Click your browser's Back button to register for another course or review your schedule.</p>";
}


mysqli_close($DBConnect);
?>

</body>
</html>

最佳答案

使用一个查询“找到每个人都有这些详细信息”,然后你说“如果找到任何人 = 坏,否则 = 好。例如:

SELECT ID FROM $TableName WHERE DiverID = '$DiverId', class = '$class', days='$Days' LIMIT 1

然后你运行那个查询,如果它发现任何东西 (if(count($results) > 0)...) 然后你显示一个错误(或其他什么)如果它没有找到然后它可以安全地添加详细信息。

奖励: 作为旁注,请查看 PDO(mysql_* 函数在 PHP 领域是 no longer supported)并确保在输入进入数据库查询之前过滤和清理输入(谷歌,)

关于php - 只检查第一条记录,而不是所有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13408145/

相关文章:

php - 从 mysql 表中获取不同的行,该表是特定于用户 id 的最后一个条目

php - MySQLi更新和死亡();和退出();不工作

php - 如何将 MySQL 行拆分为连接中每个表的单独变量

php - 易于获取 : Identify all old version numbers of a package?

Javascript:使用 1 个函数但不同的值设置多个字段的值

MYSQL select - 特殊情况

php - 有哪些方法可以在数据库中存储项目的顺序?

php - 单引号不解析

php - 如何在 PHPUnit 中的测试之间共享夹具

php - 如何使用 WHERE 条件从表中的字符串中查找特定值?