使用Prepared Statement在数据库中插入数据时出错

时间:2018-05-28 04:10:53

标签: php mysql prepared-statement ticket-system

我正在使用PHP和Prepared Statement为我公司的票务系统工作。 添加票证时,您应填写以下字段:

  1. 门票类型
  2. 门票标题
  3. 门票说明
  4. 请求日期
  5. 请求小时
  6. 公司
  7. 访问类型
  8. 优先级
  9. 状态
  10. 指派技术人员
  11. 这有效: 1.您可以选择从数据库中提取的故障单类型。 2.您可以选择从数据库中提取的公司。 3.您可以选择从数据库中提取的访问类型。 4.您可以选择从数据库中提取的技术人员。

    问题在于,当您按下添加票证时,它不会向数据库添加任何内容。

    这是我的代码:

    newticket.php

    <?php
       $projects = ProjectData::getAll();
       $priorities = PriorityData::getAll();
       $ticket= TicketData::getAll();
       $statuses = StatusData::getAll();
       $kinds = KindData::getAll();
       $users = UserData::getAll();
    
       ?>
    <div class="row">
       <div class="col-md-12">
          <div class="card">
             <div class="card-header" data-background-color="blue">
                <h4 class="title">Nuevo Ticket</h4>
             </div>
             <div class="card-content table-responsive">
                <form class="form-horizontal" role="form" method="post" action="./?action=addticket">
                   <div class="form-group">
                      <label for="inputEmail1" class="col-lg-2 control-label">Tipo</label>
                      <div class="col-lg-10">
                         <select name="kind_id" class="form-control" required>
                            <?php foreach($kinds as $p):?>
                            <option value="<?php echo $p->id; ?>"><?php echo $p->name; ?></option>
                            <?php endforeach; ?>
                         </select>
                      </div>
                   </div>
                   <div class="form-group">
                      <label for="inputEmail1" class="col-lg-2 control-label">Titulo</label>
                      <div class="col-lg-10">
                         <input type="text" name="title" required class="form-control" id="inputEmail1" placeholder="Titulo">
                      </div>
                   </div>
                   <div class="form-group">
                      <label for="inputEmail1" class="col-lg-2 control-label">Descripcion</label>
                      <div class="col-lg-10">
                         <textarea class="form-control" name="description" required placeholder="Descripcion"></textarea>
                      </div>
                   </div>
                   <div class="form-group">
                      <label for="inputEmail1" class="col-lg-2 control-label">Fecha de la Visita</label>
                      <div class="col-lg-4">
                         <input name="date_at" id="date_at" class="form-control" type="date">
                      </div>
                      <label for="inputEmail1" class="col-lg-2 control-label">Hora de la Visita</label>
                      <div class="col-lg-4">
                         <input name="time_at" id="time_at" class="form-control" type="time" />
                      </div>
                   </div>
                   <div class="form-group">
                      <label for="inputEmail1" class="col-lg-2 control-label">Proyecto</label>
                      <div class="col-lg-4">
                         <select name="project_id" class="form-control" required>
                            <option value="">-- SELECCIONE --</option>
                            <?php foreach($projects as $p):?>
                            <option value="<?php echo $p->id; ?>"><?php echo $p->name; ?></option>
                            <?php endforeach; ?>
                         </select>
                      </div>
                      <label for="inputEmail1" class="col-lg-2 control-label">Categoria</label>
                      <div class="col-lg-4">
                         <select name="category_id" class="form-control" required>
                            <option value="">-- SELECCIONE --</option>
                            <?php foreach(CategoryData::getAll() as $p):?>
                            <option value="<?php echo $p->id; ?>"><?php echo $p->name; ?></option>
                            <?php endforeach; ?>
                         </select>
                      </div>
                   </div>
                   <div class="form-group">
                      <label for="inputEmail1" class="col-lg-2 control-label">Prioridad</label>
                      <div class="col-lg-4">
                         <select name="priority_id" class="form-control" required>
                            <option value="">-- SELECCIONE --</option>
                            <?php foreach($priorities as $p):?>
                            <option value="<?php echo $p->id; ?>"><?php echo $p->name; ?></option>
                            <?php endforeach; ?>
                         </select>
                      </div>
                      <label for="inputEmail1" class="col-lg-2 control-label">Estado</label>
                      <div class="col-lg-4">
                         <select name="status_id" class="form-control" required>
                            <?php foreach($statuses as $p):?>
                            <option value="<?php echo $p->id; ?>"><?php echo $p->name; ?></option>
                            <?php endforeach; ?>
                         </select>
                      </div>
                   </div>
                   <div class="form-group">
                      <label for="inputEmail1" class="col-lg-2 control-label">Asignar a</label>
                      <div class="col-lg-4">
                         <select name="tecnico_id" class="form-control" required>
                            <option value="">-- SELECCIONE --</option>
                            <?php foreach($users as $p):?>
                            <option value="<?php echo $p->id; ?>"><?php echo $p->name." ".$p->lastname; ?></option>
                            <?php endforeach; ?>
                         </select>
                      </div>
                   </div>
                   <div class="form-group">
                      <div class="col-lg-offset-2 col-lg-10">
                         <button type="submit" class="btn btn-default">Agregar Ticket</button>
                      </div>
                   </div>
                </form>
             </div>
          </div>
       </div>
    </div>
    

    ticketdata.php

    <?php
    class TicketData {
        public static $tablename = "ticket";
    
    
        public function TicketData(){
            $this->name = "";
            $this->lastname = "";
            $this->email = "";
            $this->password = "";
            $this->date_at="";
            $this->time_at="";
            $this->tecnico_id="";
            $this->created_at = "NOW()";
        }
        public function getTicket(){ return TicketData::getById($this->ticket_id); }
        public function getProject(){ return ProjectData::getById($this->project_id); }
        public function getPriority(){ return PriorityData::getById($this->priority_id); }
        public function getStatus(){ return StatusData::getById($this->status_id); }
        public function getKind(){ return KindData::getById($this->kind_id); }
        public function getCategory(){ return CategoryData::getById($this->category_id); }
    
        public function add(){
            $sql = "insert into ticket (title,description,date_at,time_at,category_id,project_id,priority_id,user_id,status_id,kind_id,created_at,tecnico_id) ";
            $sql .= "value (\"$this->title\",\"$this->description\",\"$this->date_at\",\"$this->time_at\",\"$this->category_id\",\"$this->project_id\",$this->priority_id,$this->user_id,$this->status_id,$this->kind_id,$this->created_at,$this->tecnico_id)";
            return Executor::doit($sql);
        }
    
        public static function delById($id){
            $sql = "delete from ".self::$tablename." where id=$id";
            Executor::doit($sql);
        }
        public function del(){
            $sql = "delete from ".self::$tablename." where id=$this->id";
            Executor::doit($sql);
        }
    
    // partiendo de que ya tenemos creado un objecto TicketData previamente utilizamos el contexto
        public function update(){
            $sql = "update ".self::$tablename." set title=\"$this->title\",category_id=\"$this->category_id\",date_at=\"$this->date_at\",time_at=\"$this->time_at\",tecnico_id=\"$this->tecnico_id\",project_id=\"$this->project_id\",priority_id=\"$this->priority_id\",description=\"$this->description\",status_id=\"$this->status_id\",kind_id=\"$this->kind_id\",updated_at=NOW() where id=$this->id";
            Executor::doit($sql);
        }
    
        public static function getById($id){
            $sql = "select * from ".self::$tablename." where id=$id";
            $query = Executor::doit($sql);
            return Model::one($query[0],new TicketData());
        }
    
        public static function getRepeated($pacient_id,$medic_id,$date_at,$time_at){
            $sql = "select * from ".self::$tablename." where pacient_id=$pacient_id and medic_id=$medic_id and date_at=\"$date_at\" and time_at=\"$time_at\"";
            $query = Executor::doit($sql);
            return Model::one($query[0],new TicketData());
        }
    
    
    
        public static function getByMail($mail){
            $sql = "select * from ".self::$tablename." where mail=\"$mail\"";
            $query = Executor::doit($sql);
            return Model::one($query[0],new TicketData());
        }
    
        public static function getEvery(){
            $sql = "select * from ".self::$tablename;
            $query = Executor::doit($sql);
            return Model::many($query[0],new TicketData()); 
        }
    
        public static function getEvents(){
            $sql = "select * from ".self::$tablename;
            $query = Executor::doit($sql);
            return Model::many($query[0],new TicketData());
        }
    
        public static function getAll(){
            $sql = "select * from ".self::$tablename." order by created_at desc";
            $query = Executor::doit($sql);
            return Model::many($query[0],new TicketData());
        }
    
        public static function getAllPendings(){
            $sql = "select * from ".self::$tablename." where status_id=1";
            $query = Executor::doit($sql);
            return Model::many($query[0],new TicketData());
        }
    
    
        public static function getAllByPacientId($id){
            $sql = "select * from ".self::$tablename." where pacient_id=$id order by created_at";
            $query = Executor::doit($sql);
            return Model::many($query[0],new TicketData());
        }
    
        public static function getAllByMedicId($id){
            $sql = "select * from ".self::$tablename." where medic_id=$id order by created_at";
            $query = Executor::doit($sql);
            return Model::many($query[0],new TicketData());
        }
    
        public static function getBySQL($sql){
            $query = Executor::doit($sql);
            return Model::many($query[0],new TicketData());
        }
    
        public static function getOld(){
            $sql = "select * from ".self::$tablename." where date(date_at)<date(NOW()) order by date_at";
            $query = Executor::doit($sql);
            return Model::many($query[0],new TicketData());
        }
    
        public static function getLike($q){
            $sql = "select * from ".self::$tablename." where title like '%$q%'";
            $query = Executor::doit($sql);
            return Model::many($query[0],new TicketData());
        }
    
    
    }
    
    ?>
    

    更新

    对TicketData.php稍作修改,修正了@smith和@ Nick的观察结果。 它们看起来像这样:

    class TicketData {
        public static $tablename = "ticket";
        public function TicketData(){
            $this->name = "";
            $this->title = "";
            $this->description= "";
            $this->lastname = "";
            $this->email = "";
            $this->password = "";
            $this->date_at="";
            $this->time_at="";
            $this->tecnico_id="";
            $this->created_at = "NOW()";
        }
        public function getProject(){ return ProjectData::getById($this->project_id); }
        public function getPriority(){ return PriorityData::getById($this->priority_id); }
        public function getStatus(){ return StatusData::getById($this->status_id); }
        public function getKind(){ return KindData::getById($this->kind_id); }
        public function getCategory(){ return CategoryData::getById($this->category_id); }
        public function add(){
            $sql = "insert into ticket (title,description,date_at,time_at,category_id,project_id,priority_id,user_id,status_id,kind_id,created_at,tecnico_id) ";
            $sql .= "values (\"$this->title\",\"$this->description\",\"$this->date_at\",\"$this->time_at\",\"$this->category_id\",\"$this->project_id\",\"$this->priority_id\",\"$this->user_id\",\"$this->status_id\",\"$this->kind_id\",\"$this->created_at\",\"$this->tecnico_id\")";
            return Executor::doit($sql);
        }
    

    现在,它将保存这些字段:

    1. 故障单类型(kind_id
    2. 门票标题(title
    3. 故障单描述(description
    4. 请求日期(date_at
    5. 请求小时(hour_at
    6. 公司(project_id
    7. 访问类型(category_id
    8. 优先级(priority_id
    9. 状态(status_id
    10. 它不会保存此字段:

      1. 技术员已分配(tecnico_id
      2. addticket-action.php的

            <?php
                $r = new TicketData();
                $r->title = $_POST["title"];
                $r->description = $_POST["description"];
                $r->category_id = $_POST["category_id"];
                $r->project_id = $_POST["project_id"];
                $r->priority_id = $_POST["priority_id"];
                $r->user_id = $_SESSION["user_id"];
                $r->status_id = $_POST["status_id"];
                $r->kind_id = $_POST["kind_id"];
                $r->date_at = $_POST["date_at"];
                $r->time_at = $_POST["time_at"];
                $r->tecnico_id = $_POST["tecnico_id"];
                $r->created_at = $_POST["created_at"];
                $r->add();
                Core::alert("Successfully added!");
                Core::redir("./index.php?view=tickets");
         ?>
        

        我想在清理并转换为正确的预备语句之前使一切正常工作。 我需要更正/添加什么才能使脚本保存(date_at)(hour_at)和(tecnico_id)字段?

1 个答案:

答案 0 :(得分:0)

提供一些日志或后端错误消息可能非常有助于解决此问题。

乍一看,跳出来的主要事情是你实际上并没有使用准备好的声明。你基本上将一个字符串连接在一起制作一个SQL语句,这个非常糟糕有几个原因:

  1. 您很容易受到SQL注入攻击。例如,如果你放 ","",""); DROP TABLE ticket; --进入您的标题字段,有人可以核对您的票证表,因为您的代码没有检查这一点。
  2. 您需要清理您的输入。如果title包含双引号,它将过早地结束您的字符串输入,从而导致SQL失败。
  3. 这是一个相当大的安全漏洞,所以插上它并同时为自己节省一些输入消毒头痛!如果你转换为准备好的声明并且它有效,那么它可能是一个消毒问题。如果它仍然不起作用,请在那里获取一些记录语句,让我们看看你有什么。

    http://php.net/manual/en/mysqli.quickstart.prepared-statements.php https://www.w3schools.com/php/php_mysql_prepared_statements.asp