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 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献