【问题标题】:Renaming image to product id with php [closed]使用php将图像重命名为产品ID [关闭]
【发布时间】: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();
}
?>

【问题讨论】:

    标签: php mysql image rename


    【解决方案1】:

    您忘记将$link 从您的mysqli_query 提供给mysqli_insert_id。 所以下面一行:

    $pid = mysqli_insert_id($pid);
    

    必须是:

    $pid = mysqli_insert_id($link);
    

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

    【讨论】:

    • 干杯,永远怀念这些愚蠢的错误!
    • @user3411002:不客气。由于这个问题最不可能再给这个平台上的任何人带来任何价值,你可以考虑删除它,因为它只是一个拼写错误。
    • 无法删除。不会让我哈哈标记它。
    • @user3411002 标记自己的问题是一种非常高级的自我批评:D
    • 我喜欢批评自己,帮助我提高:D
    猜你喜欢
    • 2015-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    • 2017-10-22
    • 2018-05-13
    • 1970-01-01
    相关资源
    最近更新 更多