dpsq8476 2018-07-02 01: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-02 21: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>
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部