PHP 图像上传更改所有数据库行图像(或不更改任何人)



>我遇到了问题。我的上传(插入(和编辑脚本会更改所有图像,而不仅仅是我需要的图像。我不明白为什么会这样,因为它工作得很好,一瞬间它就坏了并改变了所有图片。如果我插入新的帖子(标题,ID,图像等(,它也会更改所有其他图像。

不知道为什么。我发现了一个错误,但不知道为什么会这样。图片上传不会插入图片名称,但会插入"1" 这是图片: imgur.com/a/Q79ac 之前很好地存储了图像名称。

这是我的ViewParts.php(主页(:

    <?php
    require_once("../../System/class.partcatalogue.php");
    require_once("../../Style/header.php");
    $part = new PARTS();
    $target_dir = "../../Images/Parts/";
    $target_file = $target_dir . basename(isset($_FILES["UploadImage"]["name"]));
    if((isset($_GET['action']) ? $_GET['action'] : null) == "forminsert")
    {       
    $name = strip_tags($_POST['name']);
    $manufacture = strip_tags($_POST['manufacture']);
    $category = strip_tags($_POST['category']);
    $code = strip_tags($_POST['code']);
    $info = strip_tags($_POST['info']);
    if (move_uploaded_file($_FILES["UploadImage"]["tmp_name"], $target_file)) {
        $image = $target_file;
    } else {
    }
    if($name=="")   {
        $error[] = "Please, insert name!";  
    }
    if($manufacture=="")    {
        $error[] = "Please, insert manufacture!";   
    }
    else if($category=="")  {
        $error[] = "Please, select category!";  
    }
    else if($code=="")  {
        $error[] = 'Please, insert code!';
    }
    else if($info=="")  {
        $error[] = 'Please, insert info!';
    }
    if($part->insertPart($image,$name,$manufacture,$category,$code,$info)){ 
                    $auth_user->redirect('ViewParts.php');
                }
    }
    if((isset($_POST['action']) ? $_POST['action'] : null) == "formedit")
    {
    $part_id = strip_tags($_POST['part_id']);
    $name = strip_tags($_POST['name']);
    $manufacture = strip_tags($_POST['manufacture']);
    $category = strip_tags($_POST['category']);
    $code = strip_tags($_POST['code']);
    $info = strip_tags($_POST['info']);
    if (move_uploaded_file($_FILES["UploadImage"]["tmp_name"], $target_file)) {
        $image = $target_file;
    } else {
    }
    if($name=="")   {
        $error[] = "Please, insert name!";  
    }
    if($manufacture=="")    {
        $error[] = "Please, insert manufacture!";   
    }
    else if($category=="")  {
        $error[] = "Please, select category!";  
    }
    else if($code=="")  {
        $error[] = 'Please, insert code!';
    }
    else if($info=="")  {
        $error[] = 'Please, insert info!';
    }
    if($part->editPart($part_id,$name,$manufacture,$category,$code,$info,$image)){  
                    $auth_user->redirect('ViewParts.php');
                }
    }
    if((isset($_GET['action']) ? $_GET['action'] : null) == "delete")
    {
    $part_id = $_GET['part_id'];
    $part->deletePart($part_id);
    }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="../../Style/bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="../../Style/bootstrap/css/bootstrap-theme.min.css" rel="stylesheet" media="screen">
<link rel="stylesheet" href="../../Style/style.css" type="text/css"  />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
    <div class="container">
        <div class="row">
        <div class="col-sm-9 col-md-9">
            <div class="panel-group" id="accordion">
                <div class="panel panel-default">
       <form method="post" id="login-form">
        <center><h2 class="form-signin-heading">PARTS</h2><hr /></center>
        <div id="error">
        <?php
            if(isset($error))
            {
                ?>
                <div class="alert alert-danger">
                   <i class="glyphicon glyphicon-warning-sign"></i> &nbsp; <?php echo $error; ?> !
                </div>
                <?php
            }
        ?>
        </div>
        <div class="form-group">
        <?php $part->selectParts(); ?>
        <span id="check-e"></span>
        </div>
        <hr />
      </form>
      </div>
      </div>
      </div>
      </div>
      </div>

    <hr />

    <?php 
        if((isset($_GET['action']) ? $_GET['action'] : null) == "edit"){
        $part_id = $_GET['part_id'];    ?>
        <div class="container">

       <form class="form-signin" method="post" id="login-form" enctype="multipart/form-data" action="ViewParts.php?action=formedit">
        <center><h2 class="form-signin-heading">EDIT PART</h2><hr /></center>
        <div id="error">
        <?php
            if(isset($error))
            {
                ?>
                <div class="alert alert-danger">
                   <i class="glyphicon glyphicon-warning-sign"></i> &nbsp; <?php echo $error; ?> !
                </div>
                <?php
            }
        ?>
        </div>
        <div class="form-group">
        <?php $part->selectPartForEdit(); ?>
        <span id="check-e"></span>
        </div>
        <hr />
        <div class="form-group">
            <center><button type="submit" name="btnedit" class="btn btn-default">Edit</button></center>
        </div>
      </form> 
    </div>
        <?php } ?>

    <hr />
     <form class="form-signin" method="post" id="insert-form" enctype="multipart/form-data" action="ViewParts.php?action=forminsert">
     <center><h3 class="form-signin-heading">ADD PART</h3><hr /></center>
        <div class="form-group">
        <input class="form-control" type='file' name='UploadImage'>
        <span id="check-e"></span>
        </div>
        <div class="form-group">
        <input type="text" class="form-control" name="name" placeholder="Name" required />
        <span id="check-e"></span>
        </div>
        <div class="form-group">
        <select name="manufacture" class="form-control" id="manufacture"> <option value=''>Select Manufacture<?php $part->selectManufacture(); ?></option></select>
        <span id="check-e"></span>
        </div>
        <div class="form-group">
        <input type="text" class="form-control" name="info" placeholder="Info" required />
        <span id="check-e"></span>
        </div>
        <div class="form-group">
        <input type="text" class="form-control" name="code" placeholder="Code" required />
        <span id="check-e"></span>
        </div>
        <div class="form-group">
        <select name="category" class="form-control" id="category"> <option value=''>Select Category<?php $part->selectCategory(); ?></option></select>
        <span id="check-e"></span>
        </div>
        <hr />
        <center><button type="submit" name="btninsert" class="btn btn-default">ADD</button></center>
    </form>
    </div>
</body>
</html>

这就是我的班级的样子:

public function insertPart($image,$name,$manufacture,$category,$code,$info)
{
    try
    {
        $stmt = $this->conn->prepare("INSERT INTO partcatalogue(image,name,manufacture,category,code,info) VALUES(:image, :name, :manufacture, :category, :code, :info)");
        $stmt->bindparam(":image", $image);                             
        $stmt->bindparam(":name", $name);
        $stmt->bindparam(":manufacture", $manufacture);
        $stmt->bindparam(":category", $category);
        $stmt->bindparam(":code", $code);
        $stmt->bindparam(":info", $info);

        $stmt->execute();   
        return $stmt;   
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
    }               
}
图像

中的问题:图像

你想在这里做什么?

$target_file = $target_dir . basename(isset($_FILES["UploadImage"]["name"]));

isset()返回布尔响应。 因此,您的文件名将永远是"真"或"假"(或"1"或"0"(。 如果要使用实际名称值,请删除isset()

$target_file = $target_dir . basename($_FILES["UploadImage"]["name"]);

相关内容

最新更新