I need to upload some information like Name, Image to database using PHP.
I tried doing this:
Add_tour.php:
<?php
$app=new app();
$targetDir = "/upload/";
if(isset($_POST['tour_name'])) {
$tour_name=isset($_POST['tour_name'])?trim($_POST['tour_name']):'';
$_SESSION['tour_name']=$tour_name;
}
if(isset($_POST['tour_img'])) {
$tour_img=isset($_POST['tour_img'])?trim($_POST['tour_img']):'';
$_SESSION['tour_img']=$tour_img;
move_uploaded_file($_FILES["tour_img"]["tmp_name"],$targetDir);
}
$ID=$app->add_tour();
?>
<form action="/admin/add_tour.php" method="post">
<input type="text" class="form-control" id="tour_name" name="tour_name">
<input type="file" data-toggle="custom-file-input" id="tour_img" name="tour_img">
</form>
And then my app.class.php file:
public function add_tour(){
$tour_name=isset($_SESSION['tour_name'])?$_SESSION['tour_name']:'';
$tour_img=isset($_SESSION['tour_img'])?$_SESSION['tour_img']:'';
$data="
tour_name ='".$this->db->escape($tour_name)."' ,
tour_img ='".$this->db->escape($tour_img)."'
";
$id=0;
if($tour_name!='' && $tour_img!='')
{
//find row
$res=$this->db->RawQuery("SELECT id FROM table WHERE
tour_name ='".$this->db->escape($tour_name)."' AND
tour_img ='".$this->db->escape($tour_img)."'
LIMIT 1;");
foreach($res as $row)
$id=$row['id'];
if($id==0)
{
$id=$this->db->RawQuery("INSERT INTO table SET $data ;");
$html.=ob_get_contents();
ob_end_clean();
}
return $id;
}
The problem is that only the name of the file will be added to the database. The file itself is not getting uploaded to the "upload" directory.