Solano Solano - 7 months ago 9
PHP Question

PHP, uploaded image url is not stored in database. Where am I going wrong?

I have created a form for an admin to upload product details. All the fields are stored correctly except for the image file path. Can someone tell me where I am going wrong?

the image is stored in the images folder but the path is not stored in the database.

Form:

<form action="form.php" method="post" enctype="multipart/form-data" id="inventoryList" name="inventoryList">
<table width="90%" border="0" cellspacing="0" cellpadding="6">
<tr><td width="20%" align="right"><h3>Products Table</h3></td></tr>
<!-- item_code -->
<tr>
<td width="20%" align="right">Item Code</td>
<td width="80%"><label>
<input name="item_code" type="text" id="item_code" size="20" />
</label></td>
</tr>
<!-- item_name -->
<tr>
<td width="20%" align="right">Product Name</td>
<td width="80%"><label>
<input name="item_name" type="text" id="item_name" size="64" />
</label></td>
</tr>
<!-- brand_name -->
<tr>
<td width="20%" align="right">Brand Name</td>
<td width="80%"><label>
<input name="brand_name" type="text" id="brand_name" size="20" />
</label></td>
</tr>
<!-- model_number -->
<tr>
<td width="20%" align="right">Model Number</td>
<td width="80%"><label>
<input name="model_number" type="text" id="model_number" size="20" />
</label></td>
</tr>
<!-- weight -->
<tr>
<td width="20%" align="right">Weight</td>
<td width="80%"><label>
<input name="weight" type="text" id="weight" size="20" />
</label></td>
</tr>
<!-- dimension -->
<tr>
<td width="20%" align="right">Dimension</td>
<td width="80%"><label>
<input name="dimension" type="text" id="dimension" size="20" />
</label></td>
</tr>
<!-- description -->
<tr>
<td align="right">Product Description</td>
<td><label>
<textarea name="description" id="description" cols="64" rows="5"></textarea>
</label></td>
</tr>
<tr>
<!-- category -->
<td align="right">Category</td>
<td><label>
<select name="category" id="category">
<option value="CellPhone">Smart Phone</option>
<option value="Laptop">Laptop</option>
</select>
</label></td>
</tr>
<!-- quantity -->
<tr>
<td width="20%" align="right">Quantity</td>
<td width="80%"><label>
<input name="quantity" type="text" id="quantity" size="20" />
</label></td>
</tr>
<tr>
<!-- price -->
<td align="right">Product Price &euro;</td>
<td><label>
<input name="price" type="text" id="price" size="12" />
</label></td>
</tr>
<!-- image -->
<tr>
<td align="right">Product Image</td>
<td><label>
<input type="file" name="imagename" id="imagename" />
</label></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><label>
<input type="submit" name="submit" id="submit" value="Add This Item Now" />
</label></td>
</tr>
</table>




PHP Script:

if(isset($_POST['submit'])){
$random_name = rand();
$sql1="INSERT INTO products
(item_code, item_name, brand_name, model_number, weight, dimension, description, category, quantity, price, imagename)
VALUES
('$_POST[item_code]','$_POST[item_name]','$_POST[brand_name]',
'$_POST[model_number]', '$_POST[weight]', '$_POST[dimension]', '$_POST[description]','$_POST[category]',
'$_POST[quantity]','$_POST[price]', '$_POST[imagename]')";

mysqli_query($connect,$sql1);

move_uploaded_file($_FILES['imagename']['tmp_name'], "../images/$random_name.jpg");
header("location: inventory.php");
exit();
}


Image showing missing file path:

enter image description here

Answer

$_POST[imagename] does not exist at all because the image is not in the $_POST Variable but in the $_FILES Variable.

So, you have to do this a little differently like so:

    <?php
        if(isset($_POST['submit'])) {
            $random_name = rand();
            $sql1        = "INSERT INTO products
                        (item_code, item_name, brand_name, model_number, weight, dimension, description, category, quantity, price, imagename) 
                        VALUES 
                        ('$_POST[item_code]','$_POST[item_name]','$_POST[brand_name]',
                        '$_POST[model_number]', '$_POST[weight]', '$_POST[dimension]', '$_POST[description]','$_POST[category]', 
                        '$_POST[quantity]','$_POST[price]', ";  //'$_POST[imagename]')

            //FIRST UPLOAD THE IMAGE AND IF THAT WORKS; THEN ADD THE IMAGE URL TO YOUR QUERY:
            if(move_uploaded_file($_FILES['imagename']['tmp_name'], "../images/$random_name.jpg")){ 
                // APPEND THE IMAGE URL TO THE QUERY & EXECUTE IT
                $sql1   .=  "../images/{$random_name}.jpg" . ")"; 
                mysqli_query($connect, $sql1);          
            }
            header("location: inventory.php");
            exit();
        }