添加下拉列表,其中包含从数据库填充的值

时间:2013-12-30 12:39:26

标签: php mysql

我正在尝试创建一个下拉列表,其中包含从数据库中填充的值。在下面的代码中,我创建了一个用于查看学生详细信息的表,并创建了删除和添加按钮以添加和删除信息。

问题是在添加class id的详细信息时,我使用了另一个表中的下拉值来填充值,但我无法执行此操作。

<html>
    <head><title>viewstudent</title>
    <?php
        $host = "localhost";
        $user = "root";
        $pass = "";
        $dbname = "my_attendance"; 
        $prefix = "";

        $con = mysql_connect($host, $user, $pass)
            or die ("not connected to db");

        mysql_select_db($dbname, $con)
            or die("database not selected");

        if (isset($_POST['delete'])){
            $deleteqry = "DELETE from student_master WHERE ID='$_POST[hidden]'" ;
            mysql_query($deleteqry, $con);
        };

        if (isset($_POST['add'])){
            $clid=mysql_real_escape_string($_POST['classid']);
            $sid=mysql_real_escape_string($_POST['studentid']);
            $fn=mysql_real_escape_string($_POST['fname']);
            $ln=mysql_real_escape_string($_POST['lname']);
            $dob=mysql_real_escape_string($_POST['dob']);
            $em=mysql_real_escape_string($_POST['email']);
            $ph=mysql_real_escape_string($_POST['phone']);
            $loc=mysql_real_escape_string($_POST['locationid']);
            $emer=mysql_real_escape_string($_POST['emergency']);

            $addquery = ("INSERT INTO student_master(classid, studentid, fname, lname, dob, phone, email, locationid, emergency)
                VALUES('$clid', '$sid', '$fn', '$ln', '$dob', '$ph', '$em', '$loc', '$emer')");

            mysql_query($addquery, $con);
      };

      $query=("SELECT * FROM student_master");
      $result = mysql_query($query);
  ?>
  </head>
  <body>
      <table align="center" width="500" border="1" cellspacing="1" cellpadding="1">
          <tr>
              <th>Id</th><th>classid</th><th>studentid</th><th>fname</th><th>lname</th><th>dob</th><th>phone</th><th>email</th><th>locationid</th><th>emergency</th>
          </tr>

          <?php
              while( $row = mysql_fetch_array($result,MYSQLI_ASSOC))
              {
                  echo "<form action=viewstudentinfo.php method=POST >";
                  echo "<tr align='center'>";
                  echo "<td>" . "<input type=text name=ids value=" .$row['id']." </td>";
                  echo "<td>" . "<input type=text name=classid value=" .$row['classid']." </td>";
                  echo "<td>" . "<input type=number name=studentid value=" .$row['studentid']." </td>";
                  echo "<td>" . "<input type=text name=fname value=" .$row['fname']." </td>";
                  echo "<td>" . "<input type=text name=lname value=" .$row['lname']." </td>";
                  echo "<td>" . "<input type=text name=dob value=" .$row['dob']." </td>";
                  echo "<td>" . "<input type=text name=phone value=" .$row['phone']." </td>";
                  echo "<td>" . "<input type=text name=email value=" .$row['email']." </td>";
                  echo "<td>" . "<input type=text name=locationid value=" .$row['locationid']." </td>";
                  echo "<td>" . "<input type=text name=emergency value=" .$row['emergency']." </td>";
                  echo "<td>" . "<input type=hidden name=hidden value=" .$row['id']." </td>";
                  echo "<td>" . "<input type=submit name=delete value=Delete" ." </td>";
                  echo "</tr>";
                  echo "</form>";
             }
             echo "<form action=viewstudentinfo.php method= post>";
             echo "<tr>";
             echo "<td><input type=text name=id></td>";
             echo "<td><select type=text name=classid></td>";?>

          // populating classid values from another table....

         <?php  
             $query = 'SELECT classid FROM class_master';  

             $result1 = mysql_query($query, $con) or die(mysql_error($con));  

             while ($row = mysql_fetch_array($result1))  
             {  
                 echo '<option value="' . $row["classid"] . '"> ' . $row["classid"] .     '</option>';
             }  
             echo "</select>";
         ?>  
         <?php
             echo "<td><input type=number name=studentid></td>";
             echo "<td><input type=text name=fname></td>";
             echo "<td><input type=text name=lname></td>";
             echo "<td><input type=date name=dob></td>";
             echo "<td><input type=number name=phone></td>";
             echo "<td><input type=email name=email></td>";
             echo "<td><input type=number name=locationid></td>";
             echo "<td><input type=number name=emergency></td>";
             echo "<td>" . "<input type=submit name=add value=ADD" ." </td>";
             echo "</tr>";
             echo "</form>";
             echo "</table>";
             mysql_close($con);
         ?>

2 个答案:

答案 0 :(得分:0)

在代码中找出<td><select type=text name=classid></td>,然后从中删除结束标记</td>。在所有<option> ... </option>关闭</td>

之后

答案 1 :(得分:0)

您正在关闭选择内的<td>

替换

 echo "<td><select type=text name=classid></td>";?>

使用

echo "<td><select type=text name=classid>";?>

并在'</td>'

之后关闭'</select>'
相关问题