0

我将使用我以前发布的代码,因为我正在开发同一个程序。我想要的是如何才能将所有选定的值保存在一行中,用户的学生 ID 不会重复。请帮助...

     <?php session_start(); ?>
     <?php
     //server info
      $server = 'localhost';
     $user = 'root';
     $pass = 'root';
    $db = 'user';

        // connect to the database
      $mysqli = new mysqli($server, $user, $pass, $db);

      // show errors (remove this line if on a live site)
           mysqli_report(MYSQLI_REPORT_ERROR);
       ?>
      <?php
     $_SESSION['username'];
     $voter = $_SESSION['username'];
    echo 'Student ID: '. $voter.'';
     echo "<br />";
       if ($_POST['representatives']){
     $check = $_POST['representatives'];
     foreach ($check as $ch){
      global $voter;
      $mysqli->query("INSERT INTO sample (studentid, candidate1) VALUES ('".$voter."', '". $ch ."')");
        echo  $ch. "<br>";
        }
        }
       ?>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
  <html>
 <head>  
    <script type="text/javascript">
    <!--
function get_representatives_value()
 {
  for (var i=0; i < document.list.representatives.length; i++)

  {
 if (document.list.representatives[i].checked)
 {
 return document.getElementById('candidates').innerHTML = document.list.representatives[i].value

 }
 }
 }

 //-->
</script>
title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link href="candidate.css" rel="stylesheet" type="text/css">
</head>
<body> <p id="txt"></p>
<form name="list" action="president2.php" method="post" onSubmit="return get_representatives_value()">
<div id="form"> 
 <?php
// get the records from the database
 if ($result = $mysqli->query("SELECT * FROM candidate_info WHERE position= 'representatives' AND department ='CCEITE' ORDER BY cand_id"))
    {
  // display records if there are records to display
    if ($result->num_rows > 0)
      {
       // display records in a table
    echo "<table border='1' cellpadding='10'>";

     // set table headers
     echo "<tr><th>Student ID</th><th>Candidate ID</td><th>Course</th><th colspan = '3'>Name</th></tr>";

    while ($row = $result->fetch_object())
              {
     // set up a row for each record
    echo "<tr>";
    echo "<td>" . $row->cand_studid . "</td>";
   echo "<td>".$row->cand_id."</td>";
  echo "<td>" . $row->course . "</td>";
  echo "<td coslpan ='5'>" . $row->fname . " ". $row->mname ." ". $row->lname ." </td>";
 echo "<td><input type ='checkbox' name='representatives[]' id='". $row->cand_studid ."' value='" . $row->cand_studid . "' onchange='get_representatives_value()' /></td>";
  echo "</tr>";
                                }
 echo "</table>";
                        }
     // if there are no records in the database, display an alert message
                        else
                        {
      echo "No results to display!";
                        }
                }
       // show an error if there is an issue with the database query
        else
                {
             echo "Error: " . $mysqli->error;
                }

       // close database connection
       $mysqli->close();

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

   ?> 
  </div>
 </form>
 <table>
 <tr><td>Preview List</td></tr>
 <tr><td>Candidates: </td><td id="candidates"> </td></tr>
  </table>
  </body>
 </html> 

这是我的输出和选中复选框的预览

在此处输入图像描述

现在这是我的数据库的预览。这是我从上面的预览中选择的结果,我的桌子上重复了学生 ID。

在此处输入图像描述

我想要的是这样保存

在此处输入图像描述

还有一件事,我如何预览我在复选框上选择的所有内容,这是我的预览输出,表格下方是用户单击复选框时的候选预览列表。但它只返回一个且仅返回最后一个选择的值,因为选择多个复选框将被打印。我怎么能在数组中应用这个方法,因为这个预览,我删除了输入类型 name='representative' 上的 '[]' 并且它可以工作,但在存在 '[]' 的情况下不行。

在此处输入图像描述

4

2 回答 2

1

我不知道我是否正确阅读了您的问题,但是...

对于您的插入问题

我不知道你在数据库中存储数据的方式是否是最好的方法,但如果你想使用你所拥有的,你可以像这样插入你的记录(假设你已经放入了一些验证脚本防止用户选择超过 2 个候选人):

<?php
$_SESSION['username'];
$voter = $_SESSION['username'];
if ($_POST['representatives']){
 $check = $_POST['representatives'];
 $mysqli->query("INSERT INTO sample (studentid, candidate1, candidate2) VALUES ('". $voter ."', '". $check[0] ."', '". $check[1] ."')");
 }
}
?>

对于您的 Preivew 问题:

我假设你想要这样的东西来预览你的学生ID,以显示候选人1和候选人2的选择:

<h3>Preview List</h3>
<table>
<tr><th>StudentID</th><th>Candidate 1</th><th>Candidate 2</th></tr>
<?php
$result = $mysqli->query("SELECT * FROM candidate_info");
while ($row = $result->fetch_object())
{
 echo "<tr><td>" . $row->studentid . "</td><td>" . $row->candidate1 . "</td><td>" . $row->candidate2 . "</td></tr>";
}
$mysqli->close();
?>
</table>
于 2013-01-28T03:57:38.847 回答
0

以下是一些可用于更新示例表的伪代码:

$candidate=array(null,null);
$candidateCounter=0;
foreach ($check as $ch){
    $candidate[$candidateCounter]=$ch;
    $candidateCounter++;
    if(candidateCounter>1){
        something wrong, only 2 candidates can be selected
    }
}
UPDATE sample set $candidate1=candidate[0], $candidate2=candidate[1] WHERE 
    studentid=$voter
于 2013-01-28T03:24:24.750 回答