Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        5年前关闭。
                                                                                            
                
        
我试图使用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();
}
?>

最佳答案

您忘记了将$link从您的mysqli_query交给mysqli_insert_id
所以下面这行:

$pid = mysqli_insert_id($pid);


一定是:

$pid = mysqli_insert_id($link);


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

10-04 21:47
查看更多