Php 使用两个下拉列表(数据库中的数据)

Php 使用两个下拉列表(数据库中的数据),php,mysql,Php,Mysql,这是一个场景:我,管理员,将添加被选中的学生,将其放在候选名单上。过程是我将使用下拉列表 我有两个表tbstudent(数据将来自这两个表)。另一个表是tbposition(也是在tbcandidates上获取和添加它所需的表),以及表tbcandidates(将在其中添加数据) 因此,这是管理员将选择候选人并放置位置的页面 第一个下拉列表用于选择学生。只要在下拉列表中选择她的名字,该学生的所有其他行都需要输入到like(学生姓名、学生性别、学生部分)。是的,它只是通过在下拉列表中选择她的名字

这是一个场景:我,管理员,将添加被选中的学生,将其放在候选名单上。过程是我将使用下拉列表

我有两个表
tbstudent
(数据将来自这两个表)。另一个表是
tbposition
(也是在
tbcandidates
上获取和添加它所需的表),以及表
tbcandidates
(将在其中添加数据)

因此,这是管理员将选择候选人并放置位置的页面

第一个下拉列表用于选择学生。只要在下拉列表中选择她的名字,该学生的所有其他行都需要输入到like(学生姓名、学生性别、学生部分)。是的,它只是通过在下拉列表中选择她的名字来添加和获取所有行,但问题是:
是的,它添加了所选学生的所有行,但只添加了相同的数据,即表顶部的数据。正如您所看到的,pf
tbstudent
表顶部的数据是女性Jane Rechell,另一个是男性,但当我在下拉列表中添加
tbstudent
的数据时,所有选中的候选人都得到了第一行数据中的数据,即Jane Rechell,所有人都成为女性,即使他们是男性

另外,另一个下拉列表用于选择从表
tbposition

<?php
session_start();
require('../connection.php');
//If your session isn't valid, it returns you to the login screen for protection
 if(empty($_SESSION['admin_id'])){
 header("location:access-denied.php");
 } 
 //retrive candidates from the tbcandidates table
 $result=mysql_query("SELECT * FROM tbCandidates")
     or die("There are no records to display ... \n" . mysql_error()); 
 if (mysql_num_rows($result)==0){

}
?>
<?php
 // retrieving positions sql query
 $positions_retrieved=mysql_query("SELECT * FROM tbPositions")
 or die("There are no records to display ... \n" . mysql_error()); 
 $name_retrieved=mysql_query("SELECT * FROM tbstudent")

   or die("There are no records to display ... \n" . mysql_error());


 $row = mysql_fetch_array($name_retrieved);
 if($row)
 {
  //get data from db
  $name = $row['student_name'];
  $gender =$row['student_gender'];
  $grade =$row['student_grade'];
  $section =$row['candidate_section'];

  }

  ?>

 <?php
 // inserting sql query 
 if (isset($_POST['Submit']))
 {

 $newCandidateName = $_POST['name']; //prevents types of SQL injection

  $newCandidatePosition = $_POST['position']; 
  $sql = mysql_query( "INSERT INTO tbCandidates(candidate_name,candidate_gender,candidate_grade,candidate_section,candidate_position) VALUES ('$newCandidateName','$gender','$grade','$section','$newCandidatePosition')" )
    or die("Could not insert candidate at the moment". mysql_error() );

 // redirect back to candidates
  header("Location: candidates.php");
}
?>
<?php
// deleting sql query
 // check if the 'id' variable is set in URL

  if (isset($_GET['id']))
  {
  // get id value
  $id = $_GET['id'];

   // delete the entry
   $result = mysql_query("DELETE FROM tbCandidates WHERE candidate_id='$id'")
  or die("The candidate does not exist ... \n"); 

  // redirect back to candidates
  header("Location: candidates.php");
  }
  else
  // do nothing   
  ?>
  <!DOCTYPE>
  <html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <title>Administration Control Panel:Candidates</title>
   <link href="css/admin_styles.css" rel="stylesheet" type="text/css" />
   <script language="JavaScript" src="js/admin.js">
   </script>
  </head>
  <body bgcolor="tan">

  <div id="mySidenav" class="sidenav">
   <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
    <a href="admin.php">HOME</a>
     <a href="positions.php">MANAGE POSITION</a>
     <a href="candidates.php">MANAGE CANDIDATES</a>
     <a href="refresh.php">POLL RESULTS</a>
      <a href="logout.php">LOGOUT</a>
  </div>
  <div id="main">
  <span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; </span>

 <div id="page">
  <div id="header">
    <h1>MANAGE CANDIDATES</h1>

    </div>
   <div id="container">
   <table width="380" align="center">
    <CAPTION><h3>ADD NEW CANDIDATE</h3></CAPTION>
     <form name="fmCandidates" id="fmCandidates" action="candidates.php" method="post" onsubmit="return candidateValidate(this)">
  <tr>
    <td>Candidate Name</td>
        <!--<td><input type="combobox" name="name" value="<?php echo $name; ?>"/></td>-->
      <td><SELECT NAME="name" id="name">select
      <OPTION VALUE="select">select
     <?php
     //loop through all table rows
     while ($row=mysql_fetch_array($name_retrieved)){
     echo "<OPTION VALUE=$row[student_name]>$row[student_name]>$row[student_grade]";
    //mysql_free_result($positions_retrieved);
     //mysql_close($link);
     }
    ?>
     </SELECT>
     </td>
  </tr>
 <tr>
     <td>Candidate Position</td>
     <!--<td><input type="combobox" name="position" value="<?php echo $positions; ?>"/></td>-->     
     <td><SELECT NAME="position" id="position">select
      <OPTION VALUE="select">select
      <?php
      //loop through all table rows
      while ($row=mysql_fetch_array($positions_retrieved)){
    echo "<OPTION VALUE=$row[position_name]>$row[position_name]";
     //mysql_free_result($positions_retrieved);
     //mysql_close($link);
    }
     ?>
      </SELECT>
       </td>
  </tr>
  <tr>
     <td>&nbsp;</td>
     <td><input type="submit" name="Submit" value="Add" /></td>
  </tr>
  </table>
   <hr>
      <table border="0" width="620" align="center">
     <CAPTION><h3>AVAILABLE CANDIDATES</h3></CAPTION>
      <tr>

     <th>Candidate Name</th>
        <th>Gender</th>
   <th>Grade</th>
    <th>Section</th>
      <th>Candidate Position</th>
    <th>Image</th>

   </tr>
        <?php
             mysql_connect("localhost", "root","") or die(mysql_error()); 
   //Connect to server
          mysql_select_db("poll") or die("Cannot connect to database"); //connect to database
            $result = mysql_query("Select * from tbcandidates"); // SQL Query
    while($row = mysql_fetch_array($result))
    {
      Print "<tr>";

        Print '<td align="center">'. $row['candidate_name'] . "</td>";
         Print '<td align="center">'. $row['candidate_gender'] . "</td>";
          Print '<td align="center">'. $row['candidate_grade'] . "</td>";
         Print '<td align="center">'. $row['candidate_section'] . "</td>";

   Print '<td align="center">'. $row['candidate_position'] . "</td>";
            Print '<td><img src="date:image/jpeg;base64,' .base64_encode($row['img']).'"height="60 width="75 /></td>';
        Print '<td align="center"><a href="#" onclick="myFunction('.$row['candidate_id'].')">delete</a> </td>';

        Print '<td align="center">'. $row['available']. "</td>";

       Print "</tr>";
         }
         mysql_free_result($result);
   mysql_close($link);
       ?>
  </table>
   <script>
      function myFunction(candidate_id)
       {
       var r=confirm("Are you sure you want to delete this record?");
       if (r==true)
         {
          window.location.assign("delete.php?id=" + candidate_id);
        }
      }
     </script>
  <hr>
   </div>
   <div id="footer"> 
   <div class="bottom_addr">



   </div>
  </div>
  </div>
  <style>


 </script>
 </body>
 </html>

有几件事你应该纠正。我会告诉你怎么做。希望你能自己做

  • 选择选项中的第一个在两个下拉列表的值中使用id而不是名称

  • 然后在表单提交后使用php。将id存储在一个变量中,并使用id检索相应的信息。现在,您可以从下拉列表中获得相应学生的信息和位置。现在将信息保存在变量中,如$student\u grade、$student\u name、$student\u roll\u no等,以便您可以使用它插入到新表中

  • 完成此操作后,将学生数据插入新表,即候选表


  • 请帮帮我。