图像未使用 ID 名称保存

Posted

技术标签:

【中文标题】图像未使用 ID 名称保存【英文标题】:Image not saving with id name 【发布时间】:2017-07-02 23:22:04 【问题描述】:

大家好,我已经创建了一个系统,通过该系统,从管理面板上传的任何图片都会根据 ID 保存在特定文件夹中! 但是当我进行一些更改时,我的 php 网站没有保存带有 id 名称的图片,也没有存储超过 1 张图片。

<?php 
// This file is www.developphp.com curriculum material
// Written by Adam Khoury January 01, 2011
// http://www.youtube.com/view_play_list?p=442E340A42191003

// Connect to the mysql database  
include "connect.php"; 

?>
<?php 
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php 
// Delete Item Question to Admin, and Delete Product if they choose
if (isset($_GET['deleteid'])) 
  echo 'Do you really want to delete product with ID of ' . $_GET['deleteid'] . '? <a href="inventory_list.php?yesdelete=' . $_GET['deleteid'] . '">Yes</a> | <a href="inventory_list.php">No</a>';
  exit();

if (isset($_GET['yesdelete'])) 
  // remove item from system and delete its picture
  // delete from database
  $id_to_delete = $_GET['yesdelete'];
  $sql = mysqli_query($conn,"DELETE FROM products WHERE id='$id_to_delete' LIMIT 1") or die (mysql_error());
  // unlink the image from server
  // Remove The Pic -------------------------------------------
    $pictodelete = ("../inventory_images/$id_to_delete.jpg");
    if (file_exists($pictodelete)) 
              unlink($pictodelete);
    
  header("location: inventory_list.php"); 
    exit();

?>
<?php 
// Parse the form data and add inventory item to the system
if (isset($_POST['product_name'])) 

    $product_name = mysqli_real_escape_string($_POST['product_name']);
  $price = mysqli_real_escape_string($_POST['price']);
  $category = mysqli_real_escape_string($_POST['category']);
  $subcategory = mysqli_real_escape_string($_POST['subcategory']);
  $details = mysqli_real_escape_string($_POST['details']);
  // See if that product name is an identical match to another product in the system
  $sql = mysqli_query($conn,"SELECT id FROM products WHERE product_name='$product_name' LIMIT 1");
  $productMatch = mysql_num_rows($sql); // count the output amount
    if ($productMatch > 0) 
    echo 'Sorry you tried to place a duplicate "Product Name" into the system, <a href="inventory_list.php">click here</a>';
    exit();
  
  // Add this product into the database now
  $sql = mysqli_query($conn,"INSERT INTO products (product_name, price, details, category, subcategory, date_added) 
        VALUES('$product_name','$price','$details','$category','$subcategory',now())") or die (mysql_error());
     $pid = mysql_insert_id();
  // Place image in the folder 
  $newname = "$pid.jpg";
  move_uploaded_file( $_FILES['fileField']['tmp_name'], "../inventory_images/$newname");
  header("location: inventory_list.php"); 
    exit();

?>
<?php 
// This block grabs the whole list for viewing
$product_list = "";
$sql = mysqli_query($conn,"SELECT * FROM products ORDER BY date_added DESC");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) 
  while($row = mysql_fetch_array($sql)) 
             $id = $row["id"];
       $product_name = $row["product_name"];
       $price = $row["price"];
       $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
       $product_list .= "Product ID: $id - <strong>$product_name</strong> - $$price - <em>Added $date_added</em> &nbsp; &nbsp; &nbsp; <a href='inventory_edit.php?pid=$id'>edit</a> &bull; <a href='inventory_list.php?deleteid=$id'>delete</a><br />";
    
 else 
  $product_list = "You have no products listed in your store yet";

?>
<!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>Inventory List</title>
<link rel="stylesheet" href="../style/style.css" type="text/css" media="screen" />
</head>

<body>
<div align="center" id="mainWrapper">
  <?php include_once("../template_header.php");?>
  <div id="pageContent"><br />
    <div align="right" style="margin-right:32px;"><a href="inventory_list.php#inventoryForm">+ Add New Inventory Item</a></div>
<div align="left" style="margin-left:24px;">
      <h2>Inventory list</h2>
      <?php echo $product_list; ?>
    </div>
    <hr />
    <a name="inventoryForm" id="inventoryForm"></a>
    <h3>
    &darr; Add New Inventory Item Form &darr;
    </h3>
    <form action="inventory_list.php" enctype="multipart/form-data" name="myForm" id="myform" method="post">
    <table  border="0" cellspacing="0" cellpadding="6">
      <tr>
        <td  align="right">Product Name</td>
        <td ><label>
          <input name="product_name" type="text" id="product_name" size="64" />
        </label></td>
      </tr>
      <tr>
        <td align="right">Product Price</td>
        <td><label>
          $
          <input name="price" type="text" id="price" size="12" />
        </label></td>
      </tr>
      <tr>
        <td align="right">Category</td>
        <td><label>
          <select name="category" id="category">
          <option value="Clothing">Clothing</option>
          </select>
        </label></td>
      </tr>
      <tr>
        <td align="right">Subcategory</td>
        <td><select name="subcategory" id="subcategory">
        <option value=""></option>
          <option value="Hats">Hats</option>
          <option value="Pants">Pants</option>
          <option value="Shirts">Shirts</option>
          </select></td>
      </tr>
      <tr>
        <td align="right">Product Details</td>
        <td><label>
          <textarea name="details" id="details" cols="64" rows="5"></textarea>
        </label></td>
      </tr>
      <tr>
        <td align="right">Product Image</td>
        <td><label>
          <input type="file" name="fileField" id="fileField" />
        </label></td>
      </tr>      
      <tr>
        <td>&nbsp;</td>
        <td><label>
          <input type="submit" name="button" id="button" value="Add This Item Now" />
        </label></td>
      </tr>
    </table>
    </form>
    <br />
  <br />
  </div>
  <?php include_once("../template_footer.php");?>
</div>
</body>
</html>

【问题讨论】:

你检查过网络服务器错误日志吗? 警告:mysqli_real_escape_string() 需要 2 个参数,1 个在 C:\wamp\www\newest\admin\inventory_list.php 第 40 行 @Difster 中给出 另外,考虑切换到 PDO。 【参考方案1】:

您不能使用同一连接在mysqlimysql 之间切换。 (注意mysql_insert_id)。

mysql_insert_id 更改为mysqli_insert_id($conn)

另外:将所有 mysqli_real_escape_string($_POST...) 更改为 mysqli_real_escape_string($conn, $_POST...)(... = POST 名称)并将 mysql_num_rows 更改为 mysqli_num_rows($conn, $sql)

查看php.net 网站,了解mysqlmysqli 之间的变化概览。

【讨论】:

以上是关于图像未使用 ID 名称保存的主要内容,如果未能解决你的问题,请参考以下文章

应用程序中的图像未更新

可能的未处理承诺拒绝(id:0):错误:权限被拒绝(通过“CameraRoll.saveToCameraRoll()”保存图像时)

如何将图像保存到具有来自先前视图控制器的 id 的单独相册中

在 MPAndroidChart 中将图表保存为图像

图像错误,未加载 S3 图像检索

如何在首选项中存储图像资源/名称