My code adds atm data to mysql database and add image to the folder called pictures, so im actually just need help here to display that picture throught mysql query(i mean row data and image from folder).
And how wrong is save image name to database or i shold save image id somehow instead of image name ? Ofc saving image name i shold to if statement and check are the name exist ^^.
Code: Select all
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method="POST" action="supp_send.php" enctype="multipart/form-data">
<table>
<h2>Enter new product information<H2>
<tr>
<td>Id (auto-incremented)<input type="text" name="XmlId"><?php//Auto generate product id for new product toing it later?></td>
</tr>
<tr>
<td>SupplierId<input type="text" name="SupplierId"> </td><?php//Auto add supplier id , Toing it later?>
</tr>
<tr>
<td>Category (Admin set Category)<input type="text" name="CategoryId"><?php//Admin enter Category id from dropdown list or smth?></td>
</tr>
<tr>
<td>Product name</td><td><input type="text" name="ProductName" maxlength="100" ></td>
</tr>
<tr>
<td>Product description </br> </td><td><textarea rows="4" cols="40" name="ProductDescription" maxlength="225" ></textarea></td>
</tr>
<tr>
<td>Additional information for administrator </br> </td><td><textarea rows="4" cols="40" name="AdditionalInformation" maxlength="225" ></textarea></td>
</tr>
<tr>
<td>Unit Price (tykki hind)</td><td> <input type="text" name="UnitPrice" ></td>
</tr>
<tr>
<td>Discount price (allahindlus)</td><td> <input type="text" name="DiscountPrice" ></td>
</tr>
<tr>
<td>Product sell timer </td><td><input type="text" name="ProductSellTimer" ></td>
</tr>
<tr>
<td>Quantity(mittu myykiks)</td><td><input type="text" name="Quantity" ></td>
</tr>
<tr>
<td>Sell quantity(mitmes on alahindluses)</td><td> <input type="text" name="SellQuantity" ></td>
</tr>
<tr>
<td>Sell quantity price (allahindluse hind)</td><td> <input type="text" name="SellQuantityPrice" ></td>
</tr>
<tr>
<td>Image</td><td> <input type="file" accept="image/gif, image/jpeg, image/png" name="image" ></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Send"></td>
</tr>
</table>
</form>
<?php
// connection to database
$connection= mysql_connect("localhost","root"," ") or die(mysql_error());
mysql_select_db(" ") or die(mysql_error());
if (isset($_POST['submit'])){
//save form image row data to image variable
$image = $_FILES['image']['name'];
echo $image_name = $_FILES['image']['name'];//image name
echo $image_type = $_FILES['image']['type'];//image type
echo $image_size = $_FILES['image']['size']; //image size
echo $image_tmp_name = $_FILES['image']['tmp_name'];//image tempetory file name
//Get other form row data
$SupplierId = $_POST['SupplierId'];
$CategoryId = $_POST['CategoryId'];
$ProductName = $_POST['ProductName'];
$ProductDescription = $_POST['ProductDescription'];
$AdditionalInformation = $_POST['AdditionalInformation'];
$UnitPrice = $_POST['UnitPrice'];
$DiscountPrice = $_POST['DiscountPrice'];
$ProductSellTimer = $_POST['ProductSellTimer'];
$Quantity = $_POST['Quantity'];
$SellQuantity = $_POST['SellQuantity'];
$SellQuantityPrice = $_POST['SellQuantityPrice'];
//Checking are the image variable is null
if($image =='')
{
echo"No image selected";
exit();
}
else
{
//Moving image to picture folder
move_uploaded_file($image_tmp_name,"pictures/$image_name");
echo"Image uploaded successfully <br>";
echo"<img src='pictures/$image_name'>";
//saving other row data and IMAGE NAME to the database
$result = mysql_query("INSERT INTO preproduct (XmlId,SupplierId,CategoryId,image,ProductName,ProductDescription,
AdditionalInformation,UnitPrice,DiscountPrice,ProductSellTimer,Quantity,SellQuantity,SellQuantityPrice)
VALUES
(default,'$SupplierId','$CategoryId','$image','$ProductName','$ProductDescription','$AdditionalInformation',
'$UnitPrice','$DiscountPrice','$ProductSellTimer','$Quantity','$SellQuantity','$SellQuantityPrice')",$connection);
//if query did fail
if(!$result)
{
die("Database query failed: ". mysql_error());
exit();
}
print"<br>";
print "Thank you, your file has been uploaded.";
}
mysql_close($connection);
}
?>
</body>
</html>