PHP PDO DATA不会插入数据库[重复]

问题描述 投票:-1回答:1

大家好,

在我的项目中,我使用oop的php

但是当我尝试插入数据时,出现消息错误:

我能做什么?

提前感谢...

Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'code' cannot be null in C:\xampp\htdocs\ahmed2\class\Database.php:51 Stack trace: #0 C:\xampp\htdocs\ahmed2\class\Database.php(51): PDOStatement->execute() #1 C:\xampp\htdocs\ahmed2\class\Employee.php(53): Database->execute() #2 C:\xampp\htdocs\ahmed2\user\employee.php(15): Employee->createEmployee() #3 {main} thrown in C:\xampp\htdocs\ahmed2\class\Database.php on line 51

员工类别:

<?php 
require_once 'Database.php';

class Employee {

   private $conn;
   private $table = 'employees';
   public $validation;
   public $code;
   public $s_name;
   public $hiring_date;
   public $salary;
   public $nb_hours;
   public $taux_remun;

   public function __construct(){
      $this->conn = new Database(); 
      $this->validation = new Validation();
   }


   // Affichage
   public function getEmployee(){
      $query = "SELECT * FROM employees";
      $this->conn->query($query);
      return $this->conn->resultALl();
   }

   // Insertion
   public function createEmployee(){
      $code = $_POST['emp_code'];
      $s_name = $_POST['name_emp'];
      $hiring_date = $_POST['hiringdate'];
      $salary = $_POST['salary_emp'];
      $nb_hours = $_POST['nb_hours'];
      $taux_remun = $_POST['t_remun'];

         $query = "INSERT INTO employees (id, code, s_name, hiring_date, salary, nb_hours, taux_remun) VALUES ('', :code, :s_name, :hiring_date, :salary, :nb_hours, :taux_remun)";

         $this->conn->query($query);
         $this->conn->bind('code', $this->code);
         $this->conn->bind('s_name', $this->s_name);
         $this->conn->bind('hiring_date', $this->hiring_date);
         $this->conn->bind('salary', $this->salary);
         $this->conn->bind('nb_hours', $this->nb_hours);
         $this->conn->bind('taux_remun', $this->taux_remun);

         $this->conn->execute();
         return $this->conn->rowCount();

   }

}

Database.php:

<?php

class Database {

   private $host = 'localhost';
   private $user = 'root';
   private $pass  = '';
   private $db_name = 'office';

   public $conn;
   public $stmt;

   public function __construct(){
      $dsn = 'mysql:host='. $this->host . ';dbname=' . $this->db_name;
      $option = [
         PDO::ATTR_PERSISTENT => true,
         PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
      ];

      try{
         $this->conn = new PDO($dsn, $this->user, $this->pass, $option);
      }catch(PDOException $e){
         die($e->getMessage());
      }
   }

   public function query($query){
      $this->stmt = $this->conn->prepare($query);
   }

   public function bind($param, $value, $type=null){
      if(is_null($type)){
         switch(true){
            case is_int($value) :
               $type = PDO::PARAM_INT;
               break;
            case is_bool($value) :
               $type = PDO::PARAM_BOOL;
               break;
            case is_null($value) :
               $type = PDO::PARAM_NULL;
               break;
            default :
               $type = PDO::PARAM_STR;
         }
      }
      $this->stmt->bindValue($param, $value, $type);
   }

   public function execute(){
      $this->stmt->execute();
   }

   public function rowCount(){
      return $this->stmt->rowCount();
   }

   public function result(){
      $this->execute();
      return $this->stmt->fetch(PDO::FETCH_ASSOC);
   }

   // Affichage
   public function resultAll(){
      $this->execute();
      return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
   }
}

员工页面:

<?php
$page_title = 'listes des employees';
require_once '../init.php';

if(!isset($_SESSION['login']) && ($_SESSION['level'] != 'Responsable_RH')){
   header('Location: ../login.php');
   exit;
}

$employee = new Employee();

if(isset($_POST['Ajouter_emp'])){


if($employee->createEmployee() > 0){
         Flasher::setFlash('employé',' bien ajouté.','success');
         header('Location: employee.php');
         exit;
      }else{
         Flasher::setFlash('Erreur',' veuillez réessayer !','danger');
      }
}
require_once 'header.php';
?>


<div class="container">
        <div class="table-wrapper">
            <div class="table-title">
                <div class="row">
                    <div class="col-sm-6">
                  <h2>Gérer  <b>les employés :</b></h2>
               </div>
               <div class="col-sm-6">
                  <a href="#addEmployeeModal" class="btn btn-success" data-toggle="modal"><i class="material-icons">&#xE147;</i> <span>Add New Employee</span></a>
                  <a href="#deleteEmployeeModal" class="btn btn-danger" data-toggle="modal"><i class="material-icons">&#xE15C;</i> <span>Delete</span></a>                 
               </div>
                </div>
            </div>
            <table class="table table-striped table-hover">
                <thead>
                    <tr>
                        <th>#ID</th>
                        <th>Code</th>
                        <th>Nom</th>
                        <th>Date d'embauche</th>
                        <th>Salaire</th>
                        <th>Nombre d'heures</th>
                        <th>Taux </th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>

               <tr>
                  <?php $no = 1;
                   $data =  $employee->getEmployee();
                  foreach($data as $d){ ?>
                     <tr>
                        <td><?php echo $no++; ?></td>
                        <td><?php echo $d['code']; ?></td>
                        <td><?php echo $d['s_name']; ?></td>
                        <td><?php echo $d['hiring_date']; ?></td>
                        <td><?php echo $d['salary']; ?></td>
                        <td><?php echo $d['nb_hours']; ?></td>
                        <td><?php echo $d['taux_remun']; ?></td>
                        <td>
                            <a href="#editEmployeeModal" class="edit" data-toggle="modal"><i class="material-icons" data-toggle="tooltip" title="Modifier">&#xE254;</i></a>
                            <a href="#deleteEmployeeModal" class="delete" data-toggle="modal"><i class="material-icons" data-toggle="tooltip" title="Supprimer">&#xE872;</i></a>
                        </td>
                     </tr>

                  <?php } ?>

                </tr> 
                </tbody>
            </table>
         <div class="clearfix">
                <div class="hint-text">Showing <b>5</b> out of <b>25</b> entries</div>
                <ul class="pagination">
                    <li class="page-item disabled"><a href="#">Previous</a></li>
                    <li class="page-item"><a href="#" class="page-link">1</a></li>
                    <li class="page-item"><a href="#" class="page-link">2</a></li>
                    <li class="page-item active"><a href="#" class="page-link">3</a></li>
                    <li class="page-item"><a href="#" class="page-link">4</a></li>
                    <li class="page-item"><a href="#" class="page-link">5</a></li>
                    <li class="page-item"><a href="#" class="page-link">Next</a></li>
                </ul>
            </div>
        </div>
    </div>
   <!-- Edit Modal HTML -->
   <div id="addEmployeeModal" class="modal fade">
      <div class="modal-dialog">
         <div class="modal-content">
            <form action="" method="post">
               <div class="modal-header">                
                  <h4 class="modal-title">Ajouter un employé</h4>
                  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
               </div>
               <div class="modal-body">

                  <div class="row justify-content-center">
                        <div class="col-md-5 form-group text-center">
                           <?php Flasher::flash(); ?>
                        </div>
                  </div>             
                  <div class="form-group">
                     <label>Code employé :</label>
                     <input type="text" name="emp_code" class="form-control" placeholder="Code employé" autofocus="auto" required>
                  </div>
                  <div class="form-group">
                     <label>Nom employé :</label>
                     <input type="text" name="name_emp" class="form-control" placeholder="Nom employé" required>
                  </div>
                  <div class="form-group">
                     <label>Date d'embauche :</label>
                     <input type="date" name="hiringdate" class="form-control" placeholder="Date d'embauche" required>
                  </div>
                  <div class="form-group">
                     <label>Salaire :</label>
                     <input type="number" name="salary_emp" class="form-control" placeholder="Salaire" required>
                  </div>    
                  <div class="form-group">
                     <label>Nombre d'heures :</label>
                     <input type="number" name="nb_hours" class="form-control" placeholder="Nombre d'heures" required>
                  </div>
                  <div class="form-group">
                     <label>Taux de rémunération :</label>
                     <input type="number" name="t_remun" class="form-control" placeholder="Taux de rémunération" required>
                  </div>           
               </div>
               <div class="modal-footer">
                  <input type="button" class="btn btn-default" data-dismiss="modal" value="fermer">
                  <input type="submit" class="btn btn-success" value="Ajouter" name="Ajouter_emp">
               </div>
            </form>
         </div>
      </div>
   </div>
   <!-- Edit Modal HTML -->
   <div id="editEmployeeModal" class="modal fade">
      <div class="modal-dialog">
         <div class="modal-content">
            <form>
               <div class="modal-header">                
                  <h4 class="modal-title">Edit Employee</h4>
                  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
               </div>
               <div class="modal-body">               
                  <div class="form-group">
                     <label>Name</label>
                     <input type="text" class="form-control" required>
                  </div>
                  <div class="form-group">
                     <label>Email</label>
                     <input type="email" class="form-control" required>
                  </div>
                  <div class="form-group">
                     <label>Address</label>
                     <textarea class="form-control" required></textarea>
                  </div>
                  <div class="form-group">
                     <label>Phone</label>
                     <input type="text" class="form-control" required>
                  </div>               
               </div>
               <div class="modal-footer">
                  <input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">
                  <input type="submit" class="btn btn-info" value="Save">
               </div>
            </form>
         </div>
      </div>
   </div>
   <!-- Delete Modal HTML -->
   <div id="deleteEmployeeModal" class="modal fade">
      <div class="modal-dialog">
         <div class="modal-content">
            <form>
               <div class="modal-header">                
                  <h4 class="modal-title">Delete Employee</h4>
                  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
               </div>
               <div class="modal-body">               
                  <p>Are you sure you want to delete these Records?</p>
                  <p class="text-warning"><small>This action cannot be undone.</small></p>
               </div>
               <div class="modal-footer">
                  <input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">
                  <input type="submit" class="btn btn-danger" value="Delete">
               </div>
            </form>
         </div>
      </div>
   </div>
<!-- <div class="container">
   <table class="table table-bordered mt-5">
      <thead>
         <th>ID</th>
         <th>Code</th>
         <th>Nom</th>
         <th>Date d'embauche</th>
         <th>Salaire</th>
         <th>Nmbre d'heures</th>
         <th>Taux de rémunération</th>
         <th>Action</th>
      </thead>
      <tbody>

      </tbody>
   </table>
</div> -->

<?php require_once 'footer.php'; ?>

大家好,

在我的项目中,我使用oop的php

但是当我尝试插入数据时,出现消息错误:

我能做什么?

提前感谢...

php pdo
1个回答
-1
投票
public function createEmployee(){
      $code = $_POST['emp_code'];
      $s_name = $_POST['name_emp'];
      $hiring_date = $_POST['hiringdate'];
      $salary = $_POST['salary_emp'];
      $nb_hours = $_POST['nb_hours'];
      $taux_remun = $_POST['t_remun'];

         $query = "INSERT INTO employees (id, code, s_name, hiring_date, salary, nb_hours, taux_remun) VALUES ('', :code, :s_name, :hiring_date, :salary, :nb_hours, :taux_remun)";

         $this->conn->query($query);
         $this->conn->bind('code', $this->code);
         $this->conn->bind('s_name', $this->s_name);
         $this->conn->bind('hiring_date', $this->hiring_date);
         $this->conn->bind('salary', $this->salary);
         $this->conn->bind('nb_hours', $this->nb_hours);
         $this->conn->bind('taux_remun', $this->taux_remun);

         $this->conn->execute();
         return $this->conn->rowCount();

   }

在插入查询中,您输入了id,它的值是'',但是由于字段是自动递增的,因此没有意义,因为您将其放在数据库中时认为该''字段是用于自动递增的代码,因此在您的插入查询中删除ID并删除'',然后直接从代码开始。

© www.soinside.com 2019 - 2024. All rights reserved.