【发布时间】:2014-06-07 00:04:03
【问题描述】:
我正在尝试使用 php 将图像重命名为产品 ID。
此代码当前会保存图像,但不会将文件重命名为 id.jpg。 1.jpg 2.jpg 3.jpg etcetc 而只是 .jpg
如何根据ID获取。
<?php
// Parse the form data and add inventory item to the system
if (isset($_POST['product_name'])) {
$product_name = mysql_real_escape_string($_POST['product_name']);
$price = mysql_real_escape_string($_POST['price']);
$category = mysql_real_escape_string($_POST['category']);
$details = mysql_real_escape_string($_POST['details']);
$stock = mysql_real_escape_string($_POST['stock']);
// See if that product name is an identical match to another product in the system
$sql = mysql_query("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($link,"INSERT INTO products (product_name, price, details, category, stock, date_added)
VALUES('$product_name','$price','$details','$category', $stock, now())") or die (mysql_error());
$pid = mysqli_insert_id($pid);
// Place image in the folder
$newname = "$pid.jpg";
move_uploaded_file( $_FILES['fileField']['tmp_name'], "inventory_images/$newname");
header("location: list.php");
exit();
}
?>
【问题讨论】: