Reputation: 7267
here i am trying to upload image to directory and add the path in database. when i fill form and click submit the image gets uploaded to directory but the file extension doesn't getting saved in database.
for example if i upload image by name mobile-image-small-1.png
the file name in database will be just mobile-image-small-1
and it is causing image not to display on web page.what is the problem here
i am using mysql database
here is my php code
// Escape user inputs for security
$product_name = mysqli_real_escape_string($link, $_POST['product_name']);
$product_category = mysqli_real_escape_string($link, $_POST['product_category']);
$product_price = mysqli_real_escape_string($link, $_POST['product_price']);
$pro_url = mysqli_real_escape_string($link, $_POST['pro_url']);
$co_owners = mysqli_real_escape_string($link, $_POST['co_owners']);
// attempt insert query execution
// close connection
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
if($_POST)
{
if($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
if(file_exists("uploaded_images/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
if(move_uploaded_file($_FILES["file"]["tmp_name"],"uploaded_images/" . $_FILES["file"]["name"]))
{
$sql = "INSERT INTO product_list (product_name, product_category, product_price,product_referrence_URL,product_co_owners,product_image_url) VALUES ('$product_name', '$product_category', '$product_price', '$pro_url', '$co_owners', 'files/uploaded_images/".$_FILES['file']['name']."')";
if(mysqli_query($link, $sql)){
header("Location: ../index.php");
}else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
if(mysqli_query($sql))
{
echo "Stored in: " . "uploaded_images/" . $_FILES["file"]["name"];
}
else
{
echo 'Unable to store';
}
}
}
}
}
mysqli_close($link);
?>
here is my form
<form class="form-horizontal" method="post" action="files/insert.php" role="form" enctype="multipart/form-data">
<div class="form-group">
<label class="control-label col-sm-2" for="product_name">Product Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="product_name" id="product_name" placeholder="Iphone 5c" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Product Category</label>
<div class="col-sm-10">
<select class="btn-btn-primary form-control" name="product_category">
<option>Mobile</option>
<option>Television</option>
<option>Printer</option>
<option>Watch</option>
<option>Monitor</option>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="product-pic">Upload your profile picture</label>
<div class="col-sm-10">
<input type="file" class="form-control" name="file" id="file" />
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="product_price">Product Price</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="product_price" id="product_price" placeholder="Rs.36,000" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pro_url">Reference URL</label>
<div class="col-sm-10">
<input type="URL" class="form-control" name="pro_url" id="pro_url" placeholder="http://www.amazon.com" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="co_owners">Co-owners</label>
<div class="col-sm-10">
<select class="btn-btn-primary form-control" name="co_owners">
<option>Select no. owners</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">Add product</button>
</div>
</div>
</form>
Upvotes: 1
Views: 1516
Reputation: 193
I think you can use the type field to get this value
$_FILE['file']['type']
Check out the documentation here
Upvotes: 0
Reputation: 22532
As per discussion your database length is 30 and type is varchar
and your image length is greater the 30. Thats the reason you half data updated to your database.
So increase the length of your column up to 100 it will take length uo to 100 length.
Upvotes: 1