Ravi Ravi - 7 months ago 9
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,
DB:
db_newsportal
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..

<?php

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


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

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


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...

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


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');
@session_start();


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

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

}else{
echo "Image Insertion Failed";
}

}


}
?>


This is the Database class....

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


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

echo $sql;
if($this->conn->insert_id!=0){
return $this->conn->insert_id;
}else{
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>
</div>
<button type="submit" name="btnSubmit" class="btn btnsuccess">Submit</button>
<button type="reset" class="btn btn-danger">Reset </button>
</form>

Answer

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:ยจ

<?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";
   }