2017-09-16 291 views
-1

这是场景:我管理员将添加所选学生将其放在候选人列表中。该过程是我将使用下拉列表。使用两个下拉列表(数据库中的数据)

我有两个表tbstudent(这是数据将来自)。另一个表格是tbposition(需要的表格可以在tbcandidates上获取并添加)和表格tbcandidates(其中将添加数据)。

所以这是页面,管理员会挑选候选人,并把位置
enter image description here

第一个下拉列表是采摘的学生。通过在下拉列表中选择她的姓名,该学生的所有其他行都需要输入,如(student_name,student_gender,student_section)。是的,它通过在下拉列表中选择她的名字来添加并获取所有行,但问题是:
enter image description here 是的,它添加了所选学生的所有行,但它只添加了相同的数据桌子的顶部。正如你所看到的表pf tbstudent上的数据是简Rechell谁是女性,另一个是男性,但是当我在下拉列表中添加tbstudent的数据时,所有选择的候选人都获得了第一行的数据即使他们是男性,他们全部都成为女性。

顺便说一句,另一个下拉列表是选择是从表中检索位置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> 
+0

请帮助我。 –

回答

0

有几件事情你应该纠正。我会告诉程序如何做到这一点。希望你能做到你的自我。

  1. 首先在选择选项使用ID表格提交后的价值没有名字两个下拉

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

  3. 这样做,插入学生数据插入到新表即,候选表格后

相关问题