我正在尝试使用php prepared statement编辑/更新我的产品页面

zazmityj  于 2023-01-29  发布在  PHP
关注(0)|答案(1)|浏览(122)

我有问题,试图编辑/更新我的产品页面,其中有多个图像使用php预准备语句.如果我只有一个图像在图像字段,它会更新和取消链接的老图像成功,但由于我已经添加了4(四)更多的图像,更新这些图像是给我坚韧的时间.请有人能纠正我在哪里我错了?
下面是插入页面和编辑页面
下面是我用来插入产品到数据库的代码

<?php
    include 'admin_config.php';

    if (isset($_POST['view'])) {

      $id           = $_POST['id'];
      $title        = $_POST['title'];
      $make         = $_POST['make'];
      $model        = $_POST['model'];
      $price        = $_POST['price'];
      $loc          = $_POST['loc'];
      $yr           = $_POST['yr'];
      $condi        = $_POST['condi'];
      $trans        = $_POST['trans'];
      $mileage      = $_POST['mileage'];
      $isfeatured   = $_POST['isfeatured'];
      $wheel        = $_POST['wheel'];
      $details      = $_POST['details'];

      $photo=$_FILES['image']['name'];
      $upload="uploads/".$photo;

      $photo2=$_FILES['image2']['name'];
      $upload2="uploads/".$photo2;

      $photo3=$_FILES['image3']['name'];
      $upload3="uploads/".$photo3;

      $photo4=$_FILES['image4']['name'];
      $upload4="uploads/".$photo4;

      $photo5=$_FILES['image5']['name'];
      $upload5="uploads/".$photo5;

      // form validation: ensure that the form is correctly filled
      $sql = "INSERT INTO vehicle     (id,title,make,model,price,loc,yr,condi,trans,mileage,isfeatured,wheel,details,photo,photo2,photo3,photo4,photo5) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    $stmt=$conn->prepare($sql);
    $stmt->bind_param("isssssisssisssssss",$id,$title,$make,$model,$price,$loc,$yr,$condi,$trans,
    $mileage,$isfeatured,$wheel,$details,$upload,$upload2,$upload3,$upload4,$upload5);       
    $stmt->execute();
    move_uploaded_file($_FILES['image']['tmp_name'], $upload);
    move_uploaded_file($_FILES['image2']['tmp_name'], $upload2);
    move_uploaded_file($_FILES['image3']['tmp_name'], $upload3);
    move_uploaded_file($_FILES['image4']['tmp_name'], $upload4);
    move_uploaded_file($_FILES['image5']['tmp_name'], $upload5);

    $_SESSION['message'] = "Vehicle Added Successfully!"; 
   }

   ?>

    <section class="section">
      <div class="row">

        <div class="col-lg-12">

          <?php include ('message.php'); ?>

            <div class="card">
              <div class="card-body">
                <a href="add_property.php" class="btn btn-primary m-3" width="200px" >Add New Vehicle</a>
                <hr>

                <!-- No Labels Form -->
                  <form action="./add_vehicle.php" method="POST" class="row g-3" enctype="multipart/form-data">

                    <input type="hidden" name="id" value="<?php echo $row['id']; ?>">

                    <div class="col-md-6">
                        <label for="title" class="form-label">Title</label>
                        <input type="text" name="title" class="form-control">
                    </div>

                    <div class="col-md-6">
                        <label for="make" class="form-label">Make</label>
                        <input type="text" name="make" class="form-control">
                    </div>

                    <div class="col-md-6">
                        <label for="model" class="form-label">Model</label>
                        <input type="text" name="model" class="form-control">
                    </div>

                    <div class="col-md-6">
                        <label for="price" class="form-label">Price</label>
                        <input type="text" name="price" class="form-control">
                    </div>

                    <div class="col-md-6">
                        <label for="loc" class="form-label">Location</label>
                        <input type="text" name="loc" class="form-control">
                    </div>

                    <div class="col-md-6">
                        <label for="yr" class="form-label">Year</label>
                        <input type="text" name="yr" class="form-control">
                    </div>

                    <div class="col-md-6">
                        <label for="condi" class="form-label">Condition</label>
                        <select id="inputState" name="condi" class="form-select">
                            <option selected disabled value="">Select Condition</option>
                            <option value="New">New</option>
                            <option value="Foreign Used">Foreign Used</option>
                            <option value="Nigeria Used">Nigeria Used</option>
                        </select>
                    </div>

                    <div class="col-md-6">
                        <label for="trans" class="form-label">Transmission</label>
                        <select id="inputState" name="trans" class="form-select">
                            <option selected disabled value="">Select Transmission</option>
                            <option value="Automatic">Automatic</option>
                            <option value="Manual">Manual</option>
                        </select>
                    </div>

                    <div class="col-md-6">
                        <label for="mileage" class="form-label">Mileage</label>
                        <input type="text" name="mileage" class="form-control">
                    </div>

                    <div class="col-md-6">
                        <label for="isfeatured" class="form-label">isFeatured</label>
                        <select id="inputState" name="isfeatured" class="form-select">
                            <option selected disabled value="">Select isFeature</option>
                            <option value="1">Yes</option>
                            <option value="0">No</option>
                        </select>
                    </div>

                    <div class="col-md-6">
                        <label for="wheel" class="form-label">Wheel</label>
                        <input type="text" name="wheel" class="form-control">
                    </div>

                    <div class="col-md-12">
                        <label for="details" class="form-label">Description</label>
                        <textarea id="default" class="form-control" rows="8" name="details"></textarea>
                    </div><br><br>

                    <div class="col-md-6 justify-content-center">
                        <label for="customFile" class="form-label">Photo</label>
                        <input type="file" name="image" class="custom-file">
                    </div><br><br>

                    <div class="col-md-6 justify-content-center">
                        <label for="customFile" class="form-label">Photo2</label>
                        <input type="file" name="image2" class="custom-file">
                    </div><br><br>

                    <div class="col-md-6 justify-content-center">
                        <label for="customFile" class="form-label">Photo3</label>
                        <input type="file" name="image3" class="custom-file">
                    </div><br><br>

                    <div class="col-md-6 justify-content-center">
                        <label for="customFile" class="form-label">Photo4</label>
                        <input type="file" name="image4" class="custom-file">
                    </div><br><br>

                    <div class="col-md-6 justify-content-center">
                        <label for="customFile" class="form-label">Photo5</label>
                        <input type="file" name="image5" class="custom-file">
                    </div><br><br>

                    <div class="text-center">
                        <input type="submit" name="view" class="btn btn-primary btn-block" value="Add Product">
                        <input type="reset" name="clear" class="btn btn-secondary btn-block" value="Reset Product">
                    </div>
                  </form>
                  <!-- End No Labels Form -->

              </div>
            </div>

        </div>

      </div>
    </section>

  </main><!-- End #main -->

这是我的编辑页

<?php

include 'admin_config.php';

$update=false;
$id           = '';
$title        = '';
$make         = '';
$model        = '';
$price        = '';
$loc          = '';
$yr           = '';
$condi        = '';
$trans        = '';
$mileage      = '';
$isfeatured   = '';
$wheel        = '';
$details      = '';
$photo        = '';
$photo2        = '';
$photo3        = '';
$photo4        = '';
$photo5        = '';

if(isset($_GET['id'])) {
 $id=$_GET['id'];

$sql="SELECT * FROM vehicle WHERE id=?";
$stmt=$conn->prepare($sql);
$stmt->bind_param("i", $id);
$stmt->execute();
$result=$stmt->get_result();
$row=$result->fetch_assoc();

$id           = $row['id'];
$title        = $row['title'];
$make         = $row['make'];
$model        = $row['model'];
$price        = $row['price'];
$loc          = $row['loc'];
$yr           = $row['yr'];
$condi        = $row['condi'];
$trans        = $row['trans'];
$mileage      = $row['mileage'];
$isfeatured   = $row['isfeatured'];
$wheel        = $row['wheel'];
$details      = $row['details'];
$photo        = $row['photo'];  
$photo2        = $row['photo2']; 
$photo3        = $row['photo3']; 
$photo4        = $row['photo4']; 
$photo5        = $row['photo5'];  

}

if (isset($_POST['update'])) {

$id           = $_POST['id'];
$title        = $_POST['title'];
$make         = $_POST['make'];
$model        = $_POST['model'];
$price        = $_POST['price'];
$loc          = $_POST['loc'];
$yr           = $_POST['yr'];
$condi        = $_POST['condi'];
$trans        = $_POST['trans'];
$mileage      = $_POST['mileage'];
$isfeatured   = $_POST['isfeatured'];
$wheel        = $_POST['wheel'];
$details      = $_POST['details'];
$oldimage     = $_POST['oldimage'];
$oldimage2     = $_POST['oldimage2'];
$oldimage3     = $_POST['oldimage3'];
$oldimage4     = $_POST['oldimage4'];
$oldimage5     = $_POST['oldimage5'];

if(isset($_FILES['image']['name'])&&($_FILES['image']['name']!="")){
 $newimage="uploads/".$_FILES['image']['name'];
 unlink($oldimage);
 move_uploaded_file($_FILES['image']['tmp_name'], $newimage);
 } 
if(isset($_FILES['image2']['name2'])&&($_FILES['image2']['name2']!="")){
 $newimage2="uploads/".$_FILES['image2']['name2'];
 unlink($oldimage2);
 move_uploaded_file($_FILES['image2']['tmp_name2'], $newimage2);
 } 
if(isset($_FILES['image3']['name3'])&&($_FILES['image3']['name3']!="")){
 $newimage3="uploads/".$_FILES['image3']['name3'];
 unlink($oldimage3);
 move_uploaded_file($_FILES['image']['tmp_name'], $newimage3);
 } 
if(isset($_FILES['image4']['name4'])&&($_FILES['image4']['name4']!="")){
 $newimage4="uploads/".$_FILES['image4']['name4'];
 unlink($oldimage4);
 move_uploaded_file($_FILES['image4']['tmp_name4'], $newimage4);
 } 
if(isset($_FILES['image5']['name5'])&&($_FILES['image5']['name5']!="")){
 $newimage5="uploads/".$_FILES['image5']['name5'];
 unlink($oldimage);
 move_uploaded_file($_FILES['image5']['tmp_name5'], $newimage5);
 } 
 else{
   $newimage=$oldimage;
   $newimage2=$oldimage2;
   $newimage3=$oldimage3;
   $newimage4=$oldimage4;
   $newimage5=$oldimage5;
  }

 // form validation: ensure that the form is correctly filled
 $sql = "UPDATE vehicle SET title=?,make=?,model=?,price=?,loc=?,yr=?,condi=?,trans=?,mileage=?,isfeatured=?,
wheel=?,details=?,photo=?,photo2=?,photo3=?,photo4=?,photo5=? WHERE id=?";
  $stmt=$conn->prepare($sql);
  $stmt
>bind_param("sssssisssisssssssi",$title,$make,$model,$price,$loc,$yr,$condi,$trans,$mileage,$isfeatured,
$wheel,$details,$upload,$upload2,$upload3,$upload4,$upload5,$id);       
  $stmt->execute();

  $_SESSION['message'] = "Vehicle Updated Successfully!"; 

  }

 ?>

<section class="section">
  <div class="row">

    <div class="col-lg-12">

      <?php include ('message.php'); ?>

        <div class="card">
          <div class="card-body">
            <a href="edit_vehicle.php" class="btn btn-primary m-3" width="200px" >Add New Vehicle</a>
            <hr>

              <!-- No Labels Form -->
              <form action="./edit_vehicle.php" method="POST" class="row g-3" enctype="multipart/form-data">

                <input type="hidden" name="id" value="<?php echo $row['id']; ?>">

                <div class="col-md-6">
                    <label for="title" class="form-label">Title</label>
                    <input type="text" name="title" class="form-control" value="<?=$title; ?>">
                </div>

                <div class="col-md-6">
                    <label for="make" class="form-label">Make</label>
                    <input type="text" name="make" class="form-control" value="<?=$make; ?>">
                </div>

                <div class="col-md-6">
                    <label for="model" class="form-label">Model</label>
                    <input type="text" name="model" class="form-control" value="<?=$model; ?>">
                </div>

                <div class="col-md-6">
                    <label for="price" class="form-label">Price</label>
                    <input type="text" name="price" class="form-control" value="<?=$price; ?>">
                </div>

                <div class="col-md-6">
                    <label for="loc" class="form-label">Location</label>
                    <input type="text" name="loc" class="form-control" value="<?=$loc; ?>">
                </div>

                <div class="col-md-6">
                    <label for="yr" class="form-label">Year</label>
                    <input type="text" name="yr" class="form-control" value="<?=$yr; ?>">
                </div>

                <div class="col-md-6">
                    <label for="condi" class="form-label">Condition</label>
                    <input type="text" name="condi" class="form-control" value="<?=$condi; ?>">
                </div>

                <div class="col-md-6">
                    <label for="trans" class="form-label">Transmission</label>
                    <input type="text" name="trans" class="form-control" value="<?=$trans; ?>">
                </div>

                <div class="col-md-6">
                    <label for="mileage" class="form-label">Mileage</label>
                    <input type="text" name="mileage" class="form-control" value="<?=$mileage; ?>">
                </div>

                <div class="col-md-6">
                    <label for="isfeatured" class="form-label">isFeatured</label>
                    <input type="text" name="isfeatured" class="form-control" value="<?=$isfeatured; ?>">
                </div>

                <div class="col-md-6">
                    <label for="wheel" class="form-label">Wheel</label>
                    <input type="text" name="wheel" class="form-control" value="<?=$wheel; ?>">
                </div>

                <div class="col-md-12">
                    <label for="details" class="form-label">Description</label>
                    <textarea id="default" class="form-control" rows="8"><?=$details; ?></textarea>
                </div><br><br>

                <div class="col-md-6 justify-content-center">
                    <input type="hidden" name="oldimage" value="<?= $photo; ?>">
                    <input type="file" name="image" class="custom-file">
                    <img src="<?= $photo; ?>" width="150" class="img-thumbnail">
                </div><br><br>

                <div class="col-md-6 justify-content-center">
                    <input type="hidden" name="oldimage2" value="<?= $photo2; ?>">
                    <input type="file" name="image2" class="custom-file">
                    <img src="<?= $photo2; ?>" width="150" class="img-thumbnail">
                </div><br><br>

                <div class="col-md-6 justify-content-center">
                    <input type="hidden" name="oldimage3" value="<?= $photo3; ?>">
                    <input type="file" name="image3" class="custom-file">
                    <img src="<?= $photo3; ?>" width="150" class="img-thumbnail">
                </div><br><br>

                <div class="col-md-6 justify-content-center">
                    <input type="hidden" name="oldimage4" value="<?= $photo4; ?>">
                    <input type="file" name="image4" class="custom-file">
                    <img src="<?= $photo4; ?>" width="150" class="img-thumbnail">
                </div><br><br>

                <div class="col-md-6 justify-content-center">
                    <input type="hidden" name="oldimage5" value="<?= $photo5; ?>">
                    <input type="file" name="image5" class="custom-file">
                    <img src="<?= $photo5; ?>" width="150" class="img-thumbnail">
                </div><br><br>

                <div class="text-center">
                    <input type="submit" name="update" class="btn btn-primary btn-block" value="Update Vehicle">
                    <input type="reset" name="clear" class="btn btn-secondary btn-block" value="Reset Vehicle">
                </div>
              </form>
              <!-- End No Labels Form -->
          </div>
        </div>

    </div>

  </div>
</section>
avwztpqn

avwztpqn1#

else块仅连接到image5的最后一个if块。
映像的每个if语句都应该有自己的else,以便将相应的$newimageX设置为$oldimageX

if(isset($_FILES['image']['name'])&&($_FILES['image']['name']!="")){
    $newimage="uploads/".$_FILES['image']['name'];
    unlink($oldimage);
    move_uploaded_file($_FILES['image']['tmp_name'], $newimage);
} else {
    $newimage = $oldimage;
}

if(isset($_FILES['image2']['name'])&&($_FILES['image2']['name']!="")){
    $newimage2="uploads/".$_FILES['image2']['name'];
    unlink($oldimage2);
    move_uploaded_file($_FILES['image2']['tmp_name'], $newimage2);
} else {
    $newimage2 = $oldimage2;
}

if(isset($_FILES['image3']['name'])&&($_FILES['image3']['name']!="")){
    $newimage3="uploads/".$_FILES['image3']['name'];
    unlink($oldimage3);
    move_uploaded_file($_FILES['image']['tmp_name'], $newimage3);
} else {
    $newimage3 = $oldimage3;
}

if(isset($_FILES['image4']['name'])&&($_FILES['image4']['name']!="")){
    $newimage4="uploads/".$_FILES['image4']['name'];
    unlink($oldimage4);
    move_uploaded_file($_FILES['image4']['tmp_name'], $newimage4);
} else {
    $newimage4 = $oldimage4;
}

if(isset($_FILES['image5']['name'])&&($_FILES['image5']['name']!="")){
    $newimage5="uploads/".$_FILES['image5']['name'];
    unlink($oldimage);
    move_uploaded_file($_FILES['image5']['tmp_name'], $newimage5);
} else {
    $newimage5 = $oldimage5;
}

相关问题