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
html php mysql mysqli


source to share


No one has answered this question yet

Check out similar questions:

4270
Link. What does this symbol mean in PHP?
2776
How can I prevent SQL injection in PHP?
2568
Should I be using the datetime or timestamp datatype in MySQL?
2414
Why shouldn't I use the mysql_ * functions in PHP?
2335
Removing an element from an array in PHP
2190
How to render text or image with transparent background using CSS?
1762
How to import SQL file using command line in MySQL?
1475
Converting HTML + CSS to PDF with PHP?
1086
How to connect to MySQL database in Python?
769
How to get the sizes of MySQL database tables?



All Articles
Loading...
X
Show
Funny
Dev
Pics