簡體   English   中英

如何在具有不同字段名稱的一行中插入數組復選框的多個值

[英]how to insert multiple values of array checkbox in one row with different field name

我將使用我以前發布的代碼,因為我正在同一程序上工作。 我想要的是如何使所有選擇的值保存在一行中,而用戶的學生證將不會重復。 請幫助...

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

在此處輸入圖片說明

我想要的是這樣保存

在此處輸入圖片說明

還有一件事,我如何預覽我在復選框上選擇的所有內容,這是我的預覽輸出,表格下方是用戶單擊復選框時候選人的預覽列表。 但是它僅返回一個,並且僅顯示最后選擇的值,因為將打印多個復選框。 我該如何在數組中應用此方法,例如預覽,我在輸入類型名稱=“ representative”上刪除了“ []”,它可以工作,但在沒有“ []”的情況下不起作用。

在此處輸入圖片說明

我不知道我是否正確閱讀了您的問題,但是...

對於您的插入問題

我不知道您將數據存儲在數據庫中的方式是否是最好的方法,但是如果您想使用已有的內容,則可以像這樣插入記錄(假設您已經放入了一些驗證腳本)以防止用戶選擇兩個以上的候選人):

<?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問題:

我假設您想要這樣的預覽圖像,以顯示帶有idid1和candidate2選項的studentid:

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

這是一些偽代碼,可用於更新示例表:

$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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM