更新查询在Mysql中不起作用,而是在此查询中插入新数据

时间:2016-08-30 11:21:35

标签: php mysql

表名= image_2017。我的图像未通过特定$_GET['id']中的此UPDATE查询进行更新,而是将图像作为新图像插入。我不知道这段代码有什么问题。我是php新手。请帮助我。

<?php

include_once "config.php";
session_start();

if(!isset($_SESSION)){
    header("index.php");
}

    $id = $_GET['id'];    
    //echo $id;

    if(!empty($_FILES)){
        $t = time();
        $filename = $category."_".$t."_".$_FILES['image']['name'];
        $upload = "uploads/";
        $fileupload = move_uploaded_file($_FILES['image']['tmp_name'],$upload.$filename);
        if($fileupload){

            $msg1 = "File uploaded Successfully";
        }else{
            $msg2 = "File uploaded Failed";
        }
    }


    if(!empty($_POST)){

    $category = $_POST['category'];
    $image = $_FILES['image'];

         $query = "UPDATE image_2017 SET category ='$category', image ='$filename' WHERE id ='$id' ";
         $result = $db->query($query);
         if($result){

            $msg3 = "Image Updated Successfully";

         }else{

            $msg4 = "Image not Updated";

         }

        }else{
            //echo "Please enter all the details";
        }

 ?>

<html>
    <body>
        <form class="form news" style="padding:10px;" method="post" action="image-2017.php" enctype="multipart/form-data">
            <div class="row">
                <div class="form-group">
                    <label class="control-label col-md-2">category</label>
                    <div class="col-md-4">
                        <select class="form-control" name="category">
                            <option>--> Select <--</option>
                            <option>Birthday</option>
                            <option>Christmas</option>
                            <option>Fruits</option>
                            <option>Ganesh Chathurthi</option>
                            <option>Green Day</option>
                            <option>Guitar Play</option>
                            <option>Independence Day</option>
                            <option>Krishna Jayanthi</option>
                            <option>Onam</option>
                            <option>Splash Pool</option>
                            <option>Teddy Bear</option>
                            <option>Veg Market</option>
                            <option>Vijayadhasami</option>
                        </select>
                    </div>
                </div>
            </div>
                <br>
            <div class="row">    
                <div class="form-group">
                    <label class="control-label col-md-2">Upload Image</label>
                    <div class="col-md-10">
                        <input type="file" name="image">   
                    </div>
                </div>
            </div>    
                <div class="form-group">
                    <label class="control-label col-md-2"></label>
                    <div class="col-md-10">
                        <button type="submit" class="btn btn-primary">Submit</button>
                        <a href="image-2017.php"><button type="button" class="btn btn-primary">Back</button></a>
                    </div>
                </div>
          </form>
    </body>
</html> 

1 个答案:

答案 0 :(得分:0)

我更新了html,为post

添加id
<?php

include_once "config.php";
session_start();

if(!isset($_SESSION)){
    header("index.php");
}

    $id = $_POST['id'];    
    //echo $id;

    if(!empty($_FILES)){
        $t = time();
        $filename = $category."_".$t."_".$_FILES['image']['name'];
        $upload = "uploads/";
        $fileupload = move_uploaded_file($_FILES['image']['tmp_name'],$upload.$filename);
        if($fileupload){

            $msg1 = "File uploaded Successfully";
        }else{
            $msg2 = "File uploaded Failed";
        }
    }


    if(!empty($_POST)){

    $category = $_POST['category'];
    $image = $_FILES['image'];

         $query = "UPDATE image_2017 SET category ='$category', image ='$filename' WHERE id ='$id' ";
         $result = $db->query($query);
         if($result){

            $msg3 = "Image Updated Successfully";

         }else{

            $msg4 = "Image not Updated";

         }

        }else{
            //echo "Please enter all the details";
        }

 ?>

<html>
    <body>
        <form class="form news" style="padding:10px;" method="post" action="image-2017.php" enctype="multipart/form-data">
            <div class="row">
                <div class="form-group">
                    <label class="control-label col-md-2">category</label>
                    <div class="col-md-4">
                        <select class="form-control" name="category">
                            <option>--> Select <--</option>
                            <option>Birthday</option>
                            <option>Christmas</option>
                            <option>Fruits</option>
                            <option>Ganesh Chathurthi</option>
                            <option>Green Day</option>
                            <option>Guitar Play</option>
                            <option>Independence Day</option>
                            <option>Krishna Jayanthi</option>
                            <option>Onam</option>
                            <option>Splash Pool</option>
                            <option>Teddy Bear</option>
                            <option>Veg Market</option>
                            <option>Vijayadhasami</option>
                        </select>
                    </div>
                </div>
            </div>
                <br>
            <div class="row">    
                <div class="form-group">
                    <label class="control-label col-md-2">Upload Image</label>
                    <div class="col-md-10">
                        <input type="file" name="image">   
                    </div>
                </div>
            </div>    
                <div class="form-group">
                    <label class="control-label col-md-2"></label>
                    <div class="col-md-10">
                        <input type="hidden" name="id" value="<?php echo $_GET['id'];?>">
                        <button type="submit" class="btn btn-primary">Submit</button>
                        <a href="image-2017.php"><button type="button" class="btn btn-primary">Back</button></a>
                    </div>
                </div>
          </form>
    </body>
</html> 

确保以:mypage.php?id=12

的方式访问该页面