数据未插入此数据库?请问php代码或db中有什么问题吗?

时间:2016-06-12 16:57:12

标签: php html mysql mysqli

这是数据库,    D B:      db_newsportal      id int auto_increment主键,title varchar 255,short_description文本,image varchar 255,created_by外键tbl_admin引用userrname, created_date datetime,modified_date datetime null,modified_by null 由和

创建

这是php ..

<?php

  if(isset($_POST['btnSubmit'])){
   $errror= array();
  $slider= new Slider;

大小的图像验证不会被写入,因为代码变得太长而且难以管理。

 if(!empty($_FILES['sliderimage']['name'])){
     $slider->sliderimage= $_FILES['sliderimage']['name'];
     move_uploaded_file($_FILES['sliderimage']['tmp_name'], 'images/' . $_FILES['sliderimage']['name']);
  }else{

如果图片未上传..

                 $error['image']= "Image required";
       }

如果错误计数为0,则调用创建滑块函数,该函数位于滑块类...

      if(count($error)==0){
      $slider->create_slider();
       }
 }
  ?>

滑块类......         

           require_once "class.databases.php";
          class Slider extends Database{
          public $id, $title, $short_description, $image,  $created_date,$created_by, $modified_date, $modified_by;
          public function create_slider(){


          $this->created_date= date('Y:m:d H:i:s');
          @session_start();

用户名从登录页面会话收到,代码变长,所以我不想写登录功能以及它的类..

          $this->created_by = $_SESSION['username'];  
          $add_info= get_object_vars($this);
          $id= $this-> insert_fields('tbl_slider', array_keys($add_info), array_values($add_info));

    if($id){
        echo "Image Inserted with $id";

    }else{
            echo "Image Insertion Failed";
    }

     }


  }
  ?>

这是数据库类....

<?php

  class Database{
       private $conn;
public function __construct(){
    $this->conn= new mysqli ('localhost', 'root', '', 'db_newsproject');
}

public function execute_sql($sql){

    $res= $this->conn->query($sql);
    $info= array();
    if ($res->num_rows >0) {
        //print_r($res);
        //echo "login Succeess";

        while($row=$res->fetch_object()){
            array_push($info, $row);

        }

    }
    return $info;

// } 
}
function insert_fields($table, $fields, $values){
    // // echo $table;
    // print_r($fields);
    // print_r($values);
    $sql= "insert into $table (";
        foreach($fields as $field){
            $sql=  $sql. "$field,";

        }
        $sql= substr($sql,0, strlen($sql)-1);
        //echo $sql;
        $sql= $sql . ") values(";

        foreach ($values as $value){
            $sql= $sql . " '$value',";
        }
        $sql= substr($sql,0, strlen($sql)-1);
        $sql= $sql . ")";

        $this->conn->query($sql);

当我尝试回显sql时,sql查询不会在滑块页面上回显。

        echo $sql;
        if($this->conn->insert_id!=0){
            return $this->conn->insert_id;
        }else{
            return false;
        }
}
?>

那么问题出在哪里......没有插入图像。

这是html

   <form action="slider.php" method="post" enctype="multipart/form-data">
   <div class="form-group">
   <label>Slider Image input</label>
   <input type="file" name="sliderimage"> </input>
    </div>
    <button type="submit" name="btnSubmit" class="btn btnsuccess">Submit</button>
   <button type="reset" class="btn btn-danger">Reset </button>
   </form>

1 个答案:

答案 0 :(得分:0)

您的代码中存在拼写错误或相当多的错位。你应该去哪里$slider->image=...你做了$slider->sliderimage=...,其中public $image而不是public $sliderimage在Slider Class上宣布了。请参阅以下代码的更正版本:¨

<?php
    if(isset($_POST['btnSubmit'])){
       $errror = array();
       $slider = new Slider;
       if(!empty($_FILES['sliderimage']['name'])){
            // YOU PROBLEM IS RIGHT HERE... NOT $slider->sliderimage
            // RATHER $slider->image AS IT IS IN YOUR SLIDER CLASS 
            $slider->image = $_FILES['sliderimage']['name'];
            move_uploaded_file($_FILES['sliderimage']['tmp_name'], 'images/' . $_FILES['sliderimage']['name']);
       }else{          
           $error['image']= "Image required";
   }