Updating / editing images in a database using PHP and MySQL

I am creating backend pages for my site and I want to edit images that already exist in my database using the input type "file".

But when I select a new image, it doesn't change in the database and it doesn't move to its folder.

This is my code:

<?php 
$db = new MySQLi(server,user,password,database);
//other function to show data 
mysqli_query($db,'SET NAMES "utf8" COLLATE "utf8_general_ci"');
if ($db->connect_errno > 0) {
    die ("Failed to connect to database".$db->connect_error);
} 

$id = $_GET["id"];
$news_select = "select news_id,title,content,image,news_date
    from news_slider 
    where news_id=$id";
if (!$news_result=$db->query($news_select)) {
    die($db->error);
}
?>

<!--wrapper start -->
<div id="wrapper">
    <!-- u data -->
    <div id="u_data">
        <?php 
        while ($row=$news_result->fetch_assoc()) { 
            ?>
            <img id="picture" src="../../images/slider_images/<?= $row["image"]; ?>">
            <h3 class="id">News id:</h3> 
            <h3 class="id">Title:</h3>      
            <h3 class="id">Content:</h3>    
            <h3 class="id">News_date : </h3>  

            <ul id="data" >
                <li class="data"><h4><?php echo $row["news_id"] ?></h4> </li>
                <li class="data"><h4><?php echo $row["title"] ?></h4> </li>
                <li class="data" style="height::10%"><h4><?php echo $row["content"] ?></h4></li>
                <li class="data"><h4><?php echo $row["news_date"] ?></h4> </li>
            </ul>
            <?php 
        }
        ?>
    </div>
    <!-- udata end  -->

      

This is my form for changing and editing the database. I will only show part of the question:

<form name="edit_news" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"])?>" method="get" enctype="multipart/form-data">    
    <h3> change pic </h3> <input type="file" name="pic" id="pic" />
    <input type="hidden" name="id" value="<?php echo $id ;?>" />

    <!-- div submit start-->
    <div style="margin:10px 0 0 0" >
        <input type="submit" name="go" value="  GO  ">
        <input type="reset" bame="back" value=" BACK ">
    </div>
    <!--div submit end -->
</form>

      

This piece of code changes the images in the database:

<?php 
if ($_SERVER["REQUEST_METHOD"]== 'GET') {
    //change imge file news
    if (isset($_FILES["pic"]) && !empty($_FILES["pic"])) {
        //difneshion variables to file uploaded 
        //name of file + exetension 
        $imgtype=pathinfo($_FILES["pic"]["name"],PATHINFO_EXTENSION);
        //echo $imgtype."<br />";
        //root directory for file 
        $path="../../images/slider_images/";
        //random name 
        $name=mt_rand(1000,1000000);
        $target=$path.$name.".".$imgtype;
        //image name to type it in DB
        $imageName=$name.".".$imgtype;

        if ($imgtype !='jpg' && $imgtype !='png') {
            echo "invalid file type";
        } else if($_FILES["pic"]["size"] > 2000000) {
            echo "invalid file size";
        }
        /* sles of transfer file uploaded start*/
        //transfer file to folder profile in site root and type the name in DB
        else {
            /* if statement of move file start */
            if (move_uploaded_file($_FILES["pic"]["tmp_name"],$target)) {
                //update imge file
                $imge_update="update news_slider set image='$imageName' where news_id=$id";
                if(!$imge_result=$db->query($imge_update)) {
                    die($db->error); 
                } else {
                //header("location:../../bookstore/bookstore_en/news_control.php");
                     echo "done!!";   
                }
            }//end of if move uploaded
        }//end of else
    }//end of change imge news
}// end of if REQUEST

      

+3


source to share





All Articles