dpsq8476 2018-07-02 09:40
浏览 42
已采纳

php - 无法从mysql更新图像

I would want to edit the image. But instead of updating the image, my code, for some reason is adding the data into the table instead of updating the values. Where am I making mistakes? I have also ran the query as a sql command and it is perfectly working there, but for some reason, here it is appending the values to the table.

Here's my db named artist_list: link

Here's my code:

<?php
session_start();
//Checking User Logged or Not
if(empty($_SESSION['user'])){
 header('location:index.php');
}
//Restrict User or admin to Access moderator.php page
if($_SESSION['user']['role'] !='sadmin' && $_SESSION['user']['role']!='admin_mum'){
 //header('location:admin_pun.php');
    echo"YOU DO NOT HAVE PEMISSION.";
    exit;
}
    $conn = mysqli_connect("localhost", "root", "", "organisation");
    if (isset($_POST['upload'])) {      
        $sql = $conn->prepare("UPDATE `artist_list` SET `name`= ? , `bio`= ? , `image`= ? WHERE id= ?");
        $name = $_POST['name'];
        $bio = $_POST['bio'];
        $image = $_FILES['image']['name'];
        $target = "../images/".basename($image);
        $sql->bind_param("sssi",$name,$bio,$image,$_GET["id"]); 
        if($sql->execute()) {
            $success_message = "Edited Successfully";
        } else {
            $error_message = "Problem in Editing Record";
        }

    }
    $sql = $conn->prepare("SELECT * FROM `artist_list` WHERE id=?");
    $sql->bind_param("i",$_GET["id"]);          
    $sql->execute();
    $result = $sql->get_result();
    if ($result->num_rows > 0) {        
        $row = $result->fetch_assoc();
    }
    $conn->close();
?>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<style>
html {
    display: table;
    margin: auto;
}

body {
    display: table-cell;
    vertical-align: middle;
}
</style>
<title>employee edit </title>
</head>
<body>
    <center><div><img src="#" style="vertical-align: top;"></div></center>
<?php if(!empty($success_message)) { ?>
<div class="success message"><?php echo $success_message; ?></div>
<?php } if(!empty($error_message)) { ?>
<div class="error message"><?php echo $error_message; ?></div>
<?php } ?>
<a href="about-admin_mum.php" class="btn btn-primary" role="button">Back To List</a>
    <form method="POST" action="artist_list-mumbai.php" enctype="multipart/form-data">
    <input type="hidden" name="size" value="1000000">
    <div>
    <input type="file" class="btn btn-success" style="margin : 20px;" name="image">
    </div>
    <div>
    <p style="margin-left : 20px;">name:</p><input type="text" name="name" style="margin-left : 20px;" value="<?php echo $row["name"]?>"><br><br>
    <p style="margin-left: 20px;">Bio:</p><textarea rows="4" cols="50" name="bio" style="margin-left : 20px;"><?php echo $row["bio"]?></textarea>
    </div>
    <div>
    <button type="submit" name="upload" class="btn btn-success" style="margin : 20px;">UPLOAD</button>
    </div>
    </form>

<footer><img src="#"></footer>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • douxiangui5011 2018-07-03 05:31
    关注

    Okay, I seem to have found a solution. Removing "gallery-mumbai.php" from the action seems to have solved the problem. I have no idea why my code has started working though!

    <?php
    session_start();
    //Checking User Logged or Not
    if(empty($_SESSION['user'])){
     header('location:index.php');
    }
    //Restrict User or admin to Access moderator.php page
    if($_SESSION['user']['role'] !='sadmin' && $_SESSION['user']['role']!='admin_mum'){
     //header('location:admin_pun.php');
        echo"YOU DO NOT HAVE PEMISSION.";
        exit;
    }
        $conn = mysqli_connect("localhost", "root", "", "organisation");
        if (isset($_POST['upload'])) {      
            $sql = $conn->prepare("UPDATE `images` SET `images`= ? WHERE id= ?");
            $image = $_FILES['image']['name'];
            $target = "../images/".basename($image);
            $sql->bind_param("si",$image,$_GET["id"]);  
            if($sql->execute()) {
                $success_message = "Edited Successfully";
            } else {
                $error_message = "Problem in Editing Record";
            }
    
        }
        $sql = $conn->prepare("SELECT * FROM `images` WHERE id=?");
        $sql->bind_param("i",$_GET["id"]);          
        $sql->execute();
        $result = $sql->get_result();
        if ($result->num_rows > 0) {        
            $row = $result->fetch_assoc();
        }
        $conn->close();
    ?>
    <html>
    <head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <style>
    html {
        display: table;
        margin: auto;
    }
    
    body {
        display: table-cell;
        vertical-align: middle;
    }
    </style>
    <title>employee edit </title>
    </head>
    <body>
        <center><div><img src="http://10.31.3.3/Nimg/ITHeader2.png" style="vertical-align: top;"></div></center>
    <?php if(!empty($success_message)) { ?>
    <div class="success message"><?php echo $success_message; ?></div>
    <?php } if(!empty($error_message)) { ?>
    <div class="error message"><?php echo $error_message; ?></div>
    <?php } ?>
    <a href="about-admin_mum.php" class="btn btn-primary" role="button">Back To List</a>
        <form method="POST" action="" enctype="multipart/form-data"> /* Removing "gallery-mumbai.php" from action seems to have solved my problem. */
        <input type="hidden" name="size" value="1000000">
        <div>
        <input type="file" class="btn btn-success" style="margin : 20px;" name="image">
        </div>
        <div>
        <button type="submit" name="upload" class="btn btn-success" style="margin : 20px;">UPLOAD</button>
        </div>
        </form>
    
    <footer><img src="http://10.31.3.3/Nimg/Footer1.png"></footer>
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用