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

时间:2017-09-16 01:26:08

标签: php mysql

这是一个场景:我,管理员,将添加所选学生将其列入候选人名单。这个过程是我将使用下拉列表。

我有两个表tbstudent(数据将来自)。另一个表是tbposition(是否需要获取并在tbcandidates上添加它),以及表tbcandidates(将添加数据)。

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

第一个下拉列表用于挑选学生。只需在下拉列表中选择她的名字,该学生的所有其他行都需要输入到(student_name,student_gender,student_section)。是的,它只是通过在下拉列表中选择她的名字来添加和获取所有行,但问题是:
enter image description here 是的,它添加了所有选定学生的行,但它只添加了相同的数据,即表格顶部的数据。正如您在表格顶部看到的那样,tbstudent是Jane Rechell是女性,另一个是男性,但当我在下拉列表中添加tbstudent的数据时,所有被选中的候选人都得到了什么在jane rechel的第一行的数据上,即使他们是男性,他们都变成了女性。

顺便说一下,另一个下拉列表用于选择从表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>

1 个答案:

答案 0 :(得分:0)

你应该纠正几件事。我将讲述如何做到这一点的程序。希望你能做到自己。

  1. 首先在选择选项中使用ID而不是下拉列表中的名称

  2. 然后在提交表单后在php中。将id存储在变量中并使用id检索相应的信息。现在,您可以从下拉列表中获得相应学生的学生信息和位置。现在将信息保存在$ student_grade,$ student_name,$ student_roll_no等变量中,以便您可以使用它插入新表中。

  3. 在此之后将学生数据插入新表格,即候选人表格