使用php将图像重命名为产品ID

时间:2014-04-22 14:33:53

标签: php mysql image rename

我尝试使用php将图像重命名为产品ID。

此代码目前会保存图像,但不会将文件重命名为id.jpg。 1.jpg 2.jpg 3.jpg etcetc而不仅仅是.jpg

我如何根据身份证明得到它。

<?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();
}
?>

1 个答案:

答案 0 :(得分:2)

您忘了将$link中的mysqli_query提交给mysqli_insert_id。 所以以下一行:

$pid = mysqli_insert_id($pid);

必须是:

$pid = mysqli_insert_id($link);

检查您的PHP错误设置。 您应该已经看到了一个适当的错误,因为您已经为期望mysqli链接的方法提供了一个未定义的变量。

相关问题