如何限制每个用户只添加3个主题

时间:2014-01-06 09:39:39

标签: php mysql

这是我的subject_registrationfunc.php

<?php

function getTierOne()
{
    $result = mysql_query("SELECT DISTINCT subject_code , subject FROM subjects") 
    or die(mysql_error());

      while($tier = mysql_fetch_array( $result )) 

        {

           echo '<option value="'.$tier['subject_code'].$tier['subject'].'">Subject:- '.$tier['subject_code'].$tier['subject'].'</option>';

        }


}
function getTierTwo()
{
    $result = mysql_query("SELECT DISTINCT subject_code , subject  FROM subjects") 
    or die(mysql_error());

      while($tier = mysql_fetch_array( $result )) 

        {
           echo '<option value="'.$tier['subject_code'].$tier['subject'].'">Subject:- '.$tier['subject_code'].$tier['subject'].'</option>';

        }


}

function getTierThree()
{
    $result = mysql_query("SELECT DISTINCT subject_code , subject FROM subjects") 
    or die(mysql_error());

      while($tier = mysql_fetch_array( $result )) 

        {
           echo '<option value="'.$tier['subject_code'].$tier['subject'].'">Subject:- '.$tier['subject_code'].$tier['subject'].'</option>';

        }

                echo '<input type="submit" name="submit" value="Submit" />';


}

?>

这是我的subject_registration.php

<?php 
    session_start();
  include('db.php');
  include('subject_registrationfunc.php');
?>

<?php
$conn = mysql_connect('localhost','root','password');
mysql_select_db('lecturer');



$myid = $_GET["myid"];


$query = 'SELECT members.ID , persons.FirstName , persons.LastName
FROM members
RIGHT JOIN persons
ON persons.ID = members.ID
WHERE members.ID = '.$myid.' ';

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_assoc($result)) {


?>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>

</head>

<body>

<table>
    <tr>
        <td><form action="" method="post" autocomplete="off"></td>
    <tr>
        <td>Enter your ID number:- <?php echo $row['ID'];?></td>

    </tr>

    <tr>
        <td>Enter your Name number:- <?php echo $row['FirstName'];?></td>

    </tr>

    <tr>
        <td>1st Choice:-</td>
    <td><select name="drop_1" id="drop_1">

      <option value="" selected="selected">Select a Subject</option>

      <?php getTierOne(); ?>

        </select> </td>
    </tr>

    <tr>
        <td>2nd Choice:-</td>
     <td><select name="drop_2" id="drop_2">

      <option value="" selected="selected">Select a Subject</option>

      <?php getTierTwo(); ?>

         </select> </td>
    </tr>
    <tr>
        <td>3rd Choice:-</td>
     <td><select name="drop_3" id="drop_3">

      <option value="" selected="selected">Select a Subject</option>

      <?php getTierThree(); ?>

         </select> </td>
    </tr>


</form>
</tr>
    </table>

<p>
<?php 
if(isset($_POST['submit'])){
        $ID = $row['ID'];
        $name = $row['FirstName'];
    $drop_1 = $_POST['drop_1'];
    $drop_2 = $_POST['drop_2'];
        $drop_3 = $_POST['drop_3'];

        $query = "INSERT INTO user_subject (ID , name , subject) VALUES ('$ID' , '$name', '$drop_1')";
        $query1 = "INSERT INTO user_subject (ID , name , subject) VALUES ('$ID' , '$name' ,'$drop_2')";
        $query2 = "INSERT INTO user_subject (ID , name , subject) VALUES ('$ID' , '$name' , '$drop_3')";

        mysql_query($query) or die(mysql_error());
        mysql_query($query1) or die(mysql_error());
        mysql_query($query2) or die(mysql_error());


        echo 'Successfully added ';


    echo "You selected a ";
    echo $drop_1." ".$drop_2." ".$drop_3;

}
}
?>
   <?php echo "<a href=\"user_subject.php?myid=".$_GET['myid']."\">Back Registered subjects</a>"; ?>
</body>
</html>

我正在尝试做的是每个用户只能将3个主题添加到user_subject表中,如果用户尝试再次添加,它会将数据替换为之前添加的当前主题。

我无法理解如何限制每个用户只有3个主题和替换查询。谢谢

0 个答案:

没有答案