将复选框值数组插入数据库(包括未选中的复选框)

时间:2018-07-14 02:30:18

标签: php html mysqli

在下面的表格中,从我的数据库的学生表中选择了学生。对于选择的每个学生,如果学生不在,则选中一个复选框,如果学生在场,则取消选中该复选框。稍后提交该表单,以便将其插入我数据库的 exam_status 表中。

//[Home Navbar Desktop View]: https://i.stack.imgur.com/THf6k.jpg 
//[Home Navbar Mobile View]: https://i.stack.imgur.com/CiJOc.png
//[Home Navbar/Sidebar Extend Mobile View]: https://i.stack.imgur.com/HhJxQ.png

import React, { Component } from 'react';
import { Switch, Route } from 'react-router-dom';
import Home from "./components/home/home";
import About from "./components/about/about";
import Portfolio from "./components/portfolio/portfolio";
import Services from "./components/services/services";
import { Collapse, Navbar, NavbarToggler, NavbarBrand, Nav, NavItem, NavLink, Button } from 'reactstrap';
import './App.css';
import 'tachyons';

class App extends Component {
  constructor(props) {
    super(props);

    this.toggle = this.toggle.bind(this);
    this.state = {
      isOpen: false
    };
  }
  toggle() {
    this.setState({
      isOpen: !this.state.isOpen
    });
  }
  render() {
    return (
        <div className='App'>
        <Navbar className='sticky-top' color="light" light expand="md">
          <NavbarBrand href="/"><b className='logo'>Amecle</b></NavbarBrand>
          <NavbarToggler className="order-first" onClick={this.toggle} />
          <Collapse isOpen={this.state.isOpen} navbar>
            <Nav className="ml-auto" navbar>
              <NavItem>
                <NavLink href="/about">About</NavLink>
              </NavItem>
              <NavItem>
                <NavLink href="/services">Services</NavLink>
              </NavItem>
              <NavItem>
                <NavLink href="/portfolio">Portfolio</NavLink>
              </NavItem>
            </Nav>
          </Collapse>
          <Button className="still_on_view" outline color="primary">Request a quote</Button>{' '}
        </Navbar>

和我的操作页面“ action.php ”如下

<form method="POST" action="action.php">
<?php
  $query = "SELECT * from student ORDER BY student_name,student_surname";
          $result=mysqli_query($conn,$query);
          if(false===$result)
          {
            printf("error: %s \n",mysqli_error($conn));
          }

          while($row= $result->fetch_assoc()) 
          {
                $studentmatricule = $row['student_matricule'];
                $studentname = $row['student_name'];
                $studentsurname = $row['student_surname'];
?>            
            <div id="studentdiv">
              <label>Matricule</label>
              <input type="text" name="matricule[]" value="<?php echo "$studentmatricule)"; ?>" readonly>

              <label>Name</label>
              <input type="text" name="name[]" value="<?php echo "{$studentname} {$studentsurname}"; ?>" readonly>

              <label > Absent
                  <input type="checkbox" name="absent[]" value="absent" />
              </label>
            </div> <br><br>
        <?php
          }
        ?>
            <input type="submit" name="submit" value="submit">
</form>

现在的问题是,它不能像我想要的那样工作。结果总是使第一个学生缺席,其余的学生缺席。我已尽力而为,也确实进行了研究,但没有成功。请周围的任何人帮助我吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

<form method="POST" action="action.php">
    <?php
      $query = "SELECT * from student ORDER BY student_name,student_surname";
      $result=mysqli_query($conn,$query);
      if(false===$result)
      {
        printf("error: %s \n",mysqli_error($conn));
      }
      $index = 0;
      while($row= $result->fetch_assoc()) 
      {
            $index++;
            $studentmatricule = $row['student_matricule'];
            $studentname = $row['student_name'];
            $studentsurname = $row['student_surname'];
    ?>            
        <div id="studentdiv">
          <label>Matricule</label>
          <input type="text" name="studenInfo[<?php echo $index; ?>][matriculate]" value="<?php echo $studentmatricule; ?>" readonly>

          <label>Name</label>
          <input type="text" name="studenInfo[<?php echo $index; ?>][name]" value="<?php echo $studentname." ".$studentsurname; ?>" readonly>

          <label > Absent
              <input type="checkbox" name="studenInfo[<?php echo $index; ?>][status]" value="absent" />
          </label>
        </div> <br><br>
    <?php
      }
    ?>
        <input type="submit" name="submit" value="submit">

像这样更新您的邮件文件。我已经将表单名称更改为单个数组。原因是未选中复选框值时,复选框值不会发布到页面上。因此,如果您使用相同的名称,则无法跟踪选中了哪个复选框。

并像这样更新您的action.php,

<?php 
$conn = mysqli_connect("localhost","username","password","db_name"); // update this values as per your configuration
$studenInfo = (!empty($_POST['studenInfo'])) ? $_POST['studenInfo'] : [];
foreach($studenInfo as $value ) {
    $status = (isset($value['status'])) ? 'absent' : 'present';
    $query = "INSERT INTO exam_status (student_name, student_matricule,status) VALUES ('". $value['name'] . "','". $value['matriculate'] . "','". $status . "')";
    $result=mysqli_query($conn,$query);
}
?>

我使用了自己的表架构,其中我在exam_status表中添加了student_name以便进行更好的跟踪。现在,您可以看到值正确更新。另外,如果需要插入多个数据,也可以使用批量插入(注意:我在此答案中使用了批量插入,我只是按照您使用的方式进行操作)