0

我有两张桌子

  1. 学生
  2. csci01成员

我将学生记录放在 csci01members 中,我的代码没有语法错误。但它总是卡在“用户已经是会员”中。即使他不是,我也已经有了添加记录的代码。

但我需要一个错误捕获,以便如果用户已经在 csci01members 表中。它无法添加记录或他无法查看 csci01 的成员,因此该成员将不会出现在添加到表的列表中。

<?php
    $errors="";
        if(isset($_GET['add']))
        {
            $con = mysql_connect("localhost","root","");
            if(!$con)
            { die("could not connect to server".mysql_error()); }

            mysql_select_db("login", $con);

        if (empty($errors)){
        $check = mysql_query("SELECT * from csci01members");

        $check_count = mysql_num_rows($check);  
        if ($check_count == 1) {
             die ("&nbsp;&nbsp;The user is already a member.");
            }
        }

        $result = mysql_query("SELECT * from students where username='$_GET[add]'");

        $result_count = mysql_num_rows($result);    
        if ($result_count == 0) {
                echo "<font color=red><br />&nbsp;&nbsp;The user doesn't exists.</font>";
                }
        else
            {
            while($row = mysql_fetch_array($result))
            {
        echo "Student Number: $row[username]<br>Name: $row[namelast]
                , $row[namefirst]<br><br> was added to the group<br><br>";
        $sn = $row['username'];
        $nl = $row['namelast'];
        $nf = $row['namefirst'];
        $nm = $row['namemi'];   

        mysql_query("INSERT INTO csci01members (username, namelast, namefirst, namemi)
                        VALUES ('$sn', '$nl', '$nf', '$nm')");

        mysql_close($con);
            }
            }
        }
            $con = mysql_connect("localhost","root","");
            if(!$con)
            { die("could not connect to server".mysql_error());}

            mysql_select_db("login", $con);

            $sql="Select * from students";
            $sql_result=mysql_query($sql) 
            or exit("Sql Error".mysql_error());
            $sql_num=mysql_num_rows($sql_result);

                if($row = mysql_num_rows($sql_result) == 0)
                {
                echo "There are no registered student yet<br><br>";
                $name=$row["username"];
                $class=$row["namelast"] .$row["namefirst"];
                $accept = "<a href='?add=$row[username]'> </a>";
                }
                else
                {
                    echo "<table border = 0  width=\"200%\">";
                    echo "<tr>";
                    echo "<td width = '20%' > <b><center>USN</center></b></td>
                                           <td  width = '60%'><b><center>Name</center></b></td> 
                    <td width = '10%'><b><center>Action</center></b></td>";
                    echo "</tr>";

                    while($sql_row=mysql_fetch_array($sql_result))
                    {
                    $name=$sql_row["username"];
                    $class=$sql_row["namelast"] . ', '.$sql_row["namefirst"];
                    $accept = "<a href='?add=$sql_row[username]'>[Add]</a>";

                    echo "<td >".$name."</td>";
                    echo "<td>".$class."</td>"; 
                    echo "<td>".$accept."</td></tr>";
                    } 
                }
                    echo "</table>";
                    mysql_close();
?>
4

1 回答 1

0

您的“count_check”计算表中的所有行,您需要计算添加用户名的行

if (empty($errors)){
    $check = mysql_query("SELECT * from csci01members WHERE username = '".mysql_real_escape_string($_GET['add'])."'");

    $check_count = mysql_num_rows($check);  
    if ($check_count == 1) {
         die ("&nbsp;&nbsp;The user is already a member.");
        }
    }

对于添加标识符,最好使用数字 id。用户名可能包含特殊符号,如空格或其他符号,当您通过 url 传递它们时,它们会被编码

于 2012-07-20T11:53:10.243 回答