php-无法从mysql更新映像

时间:2018-07-02 09:40:53

标签: php mysql

我想编辑图像。但是由于某种原因,我的代码没有更新图像,而是将数据添加到表中而不是更新值。我在哪里犯错?我也已将查询作为sql命令运行,并且在该查询中运行正常,但是由于某种原因,这里将值附加到表中。

这是我的数据库,名为artist_list: link

这是我的代码:

<?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 个答案:

答案 0 :(得分:0)

好的,我似乎找到了解决方案。从操作中删除“ gallery-mumbai.php”似乎已解决了问题。我不知道为什么我的代码开始工作了!

<?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>