Ravi Ravi - 4 months ago 7
SQL Question

The data is not being inserted on this database? Is there something wrong in the php code or in the db please tell?

this is the database,
id int auto_increment primary key, title varchar 255, short_description text, image varchar 255, created_by foreign key tbl_admin references userrname,
created_date datetime, modified_date datetime null, modified_by null
The created by and

This is the php..


$errror= array();
$slider= new Slider;

image validation of size is not writtenn because the code becomes too long and and difficult to manage here.

$slider->sliderimage= $_FILES['sliderimage']['name'];
move_uploaded_file($_FILES['sliderimage']['tmp_name'], 'images/' . $_FILES['sliderimage']['name']);

if the image is not uploaded..

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

if the error counts 0 then the create slider function is called which is on slider class...


The slider class...

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');

The username is receieved from the login pages session and the code becomes long so i didnot want to write the login function as well its class..

$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));

echo "Image Inserted with $id";

echo "Image Insertion Failed";



This is the Database class....


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) {
//echo "login Succeess";

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 . ")";


When i try to echo the sql the sql query is not echoed on the slider page.

echo $sql;
return $this->conn->insert_id;
return false;

SO where is the problem..the image is not being inserted.

this is the 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>
<button type="submit" name="btnSubmit" class="btn btnsuccess">Submit</button>
<button type="reset" class="btn btn-danger">Reset </button>


There was a typo or rather variable misplacement in your code. Where you were supposed to do $slider->image=... You did $slider->sliderimage=... of which public $image and not public $sliderimage was declared on your Slider Class. See the Corrected version of your code below:ยจ

       $errror = array();
       $slider = new Slider;
            // 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']);
           $error['image']= "Image required";