【问题标题】:how to insert multiple values of array checkbox in one row with different field name如何在具有不同字段名称的一行中插入多个数组复选框值
【发布时间】:2013-01-28 02:34:16
【问题描述】:

我将使用我以前发布的代码,因为我正在开发同一个程序。我想要的是如何才能将所有选定的值保存在一行中,用户的学生 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> 

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

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

我想要的就是这样保存

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

【问题讨论】:

  • 如果我理解正确,您想为学生添加候选人吗?一个学生可以有多少候选人?您希望保存它的方式不符合数据库规范化规则。 en.wikipedia.org/wiki/Database_normalization
  • @HMR.. 在我的样本中,一个学生只能有两个候选人,是的,我违反了规范化,我知道这一点,这就是我请求帮助纠正它的原因,因为我不知道怎么解决。
  • 如果只有固定数量的候选值,您可以将它们作为学生表中的列。我认为您正在为学生寻找更新声明,并在处理帖子时更新 2 个候选列。
  • @HMR .. 如果是这样,我想怎么做?我还是不知道..
  • 我添加了一些关于如何更新示例表的伪代码的答案。如果您想规范化数据库,您必须将候选人放在单独的表中,删除具有特定学生 ID 的条目并插入用户提交表单时发布的条目。您可能想阅读有关规范化的良好解释(维基百科文章不太好)并查看有关连接表的 mysql 文档。

标签: php javascript


【解决方案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>

【讨论】:

  • 感谢您的回复...您的回答为我提供了我的程序所需的内容。再次感谢..
【解决方案2】:

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

$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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 1970-01-01
    • 2021-03-20
    • 2015-11-13
    相关资源
    最近更新 更多