简体   繁体   中英

insert from sql data record in the table to another table

i have two tables

  1. students table
  2. csci01members table

I will put the student record in the csci01members, my code doesn't have a syntax error. But it always get's stuck in "The user is already a member". EVEN if he's not ,I already got the code of adding the record.

but i need an error trapping so that if the user is already in the csci01members table. it can't add the record or he cannot view the members of csci01 so that the member already will not be seen in the list of adding into table.

<?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();
?>

Your "count_check" count all rows in table, you needs count rows with added username

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.");
        }
    }

And for add identificator better use numeric id. Username may contain special symbols like space or something else, and when you will pass them by url, them being encoded

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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