如何在具有不同字段名称的一行中插入多个数组值复选框

时间:2013-01-28 02:34:16

标签: php javascript

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

     <?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> 

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

enter image description here

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

enter image description here

我想要的是像这样保存

enter image description here

还有一件事,我怎么能预览我在复选框上选中的所有内容,这里是我的预览输出,下面是用户单击复选框时候选人的预览列表。但它只返回一个,只返回最后选择的值,因为选择多个复选框将被打印。我怎么能在这个预览的数组中应用这个方法,我删除我的输入类型名称''代表'上的'[]',它可以工作,但不存在'[]'。

enter image description here

2 个答案:

答案 0 :(得分: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问题:

我假设您想要预览的类似内容,以显示有关candidate1和candidate2的选择的学生:

<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>

答案 1 :(得分: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