Image Uploading Problem
Posted by: shnkrsv (---.147.178.122.airtelbroadband.in)
Date: October 29, 2011 03:13PM

Am Using WAMP 2.1, Am trying to upload a image in to my database using PHP,
Following is my Code:

<?php

$hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company, if you are with webune leave as localhost
$db_user = "root"; // change to your database password
$db_password = ""; // change to your database password
$database = "demo"; // provide your database name
$db_table = "first"; // leave this as is


$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($database,$db);
?>

<html>
<head>
</head>
<body>
<h2>Insert Operation</h2>
<ul style="list-style-type:none;">
<li><a href="main.php">Home</a></li>
<li><a href="insert.php">Insert</a></li>
<li><a href="update.php">Update</a></li>
<li><a href="delete.php">Delete</a></li>
</ul>





<?php
if (isset($_REQUEST['submit']))
{


$name2=$_POST['name1'];
$url2=$_POST['url1'];
$sprice2=$_POST['sprice1'];
$eprice2=$_POST['sprice1'];
$des=$_POST['desc1'];


//Here Comes the Error Code

$img = addslashes(file_get_contents($_FILES['img1']['tmp_name']));
$image_name = addslashes($_FILES['img1']['tmp_name']);

//Here Comes the Error Code



$sql="INSERT INTO first VALUES('', '$name2','$url2','$sprice2', '$eprice2', '$des', '$img', '$image_name')";

if($result = mysql_query($sql ,$db))
{

print "<script type=\"text/javascript\">";
print "alert('Your Data has successfully inserted')";
print "</script>";
print "<script type=\"text/javascript\">";
print "window.location.href = 'insert.php'";
print "</script>";
}
else
{
echo "ERROR: ".mysql_error();
}
}


?>



<form action="" method="post" enctype="maltipart/form-data">
<table>
<tr>
<td>Name : </td><td><input type="text" name="name1"></td>
</tr>
<tr>
<td>URL : </td><td><input type="text" name="url1"></td>
</tr>
<tr>
<td>Starting Price : </td><td><input type="text" name="sprice1"></td>
</tr>
<tr>
<td>Ending Price : </td><td><input type="text" name="eprice1"></td>
</tr>
<tr>
<td>Description : </td><td><textarea rows="6" cols="40" name="desc1"></textarea></td>
</tr>
<tr><td>Upload An Image:</td><td><input type="file" name="img1" size="20"></td></tr>
<tr>
<td><input type="submit" value="Submit" name="submit"></td>
<td><input type="reset" value="Reset" name="clear"></td>
</table>

</form>

<br />


</body>
</html>



Am not able to insert the Image and its name to by Database, i have tried different options but the result is same please find me a solution.

Options: ReplyQuote
Re: Image Uploading Problem
Posted by: yfastud (Moderator)
Date: October 30, 2011 02:58AM

only save paths of images instead the images themselves

Have fun,

FREE One A Day
FREE Photo
FREE Games
FREE Websites
FREE Portable GPS
FREE WAMP Guides

Options: ReplyQuote
Re: Image Uploading Problem
Posted by: shnkrsv (---.167.178.122.airtelbroadband.in)
Date: October 30, 2011 08:19AM

I have solved the problem with another option, but now am not able to view my image in the page.

Here my code. Please find me a solution.

//insert.php

<?php

$hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company, if you are with webune leave as localhost
$db_user = "root"; // change to your database password
$db_password = ""; // change to your database password
$database = "demo"; // provide your database name
$db_table = "first"; // leave this as is


$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($database,$db);
?>

<html>
<head>
</head>
<body>
<h2>Insert Operation</h2>
<ul style="list-style-type:none;">
<li><a href="main.php">Home</a></li>
<li><a href="insert.php">Insert</a></li>
<li><a href="update.php">Update</a></li>
<li><a href="delete.php">Delete</a></li>
</ul>





<?php
if (isset($_REQUEST['submit']))
{


$name2=$_POST['name1'];
$url2=$_POST['url1'];
$sprice2=$_POST['sprice1'];
$eprice2=$_POST['sprice1'];
$des=$_POST['desc1'];
$img = $_POST['img1'];
$image_name = stripslashes($img);


if(!$img)
echo "This Not an Image";
else
{

$sql="INSERT INTO first VALUES('', '$name2','$url2','$sprice2', '$eprice2', '$des', '$img', '$image_name')";

if(!$result = mysql_query($sql ,$db))
echo "Problem Uploading Image File";
else
{
$lastid=mysql_insert_id();
echo "Image Uploaded Successfully.<p />Your Image :<p /><img src=get.php?id=$lastid>";
}
}

}


?>



<form action="" method="post" enctype="maltipart/form-data">
<table>
<tr>
<td>Name : </td><td><input type="text" name="name1"></td>
</tr>
<tr>
<td>URL : </td><td><input type="text" name="url1"></td>
</tr>
<tr>
<td>Starting Price : </td><td><input type="text" name="sprice1"></td>
</tr>
<tr>
<td>Ending Price : </td><td><input type="text" name="eprice1"></td>
</tr>
<tr>
<td>Description : </td><td><textarea rows="6" cols="40" name="desc1"></textarea></td>
</tr>
<tr><td>Upload An Image:</td><td><input type="file" name="img1" size="20"></td></tr>
<tr>
<td><input type="submit" value="Submit" name="submit"></td>
<td><input type="reset" value="Reset" name="clear"></td>
</table>

</form>

<br />


</body>
</html>


//get.php

<?php

$hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company, if you are with webune leave as localhost
$db_user = "root"; // change to your database password
$db_password = ""; // change to your database password
$database = "demo"; // provide your database name
$db_table = "first"; // leave this as is


$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($database,$db);

$id=addslashes($_REQUEST['id']);

$row=mysql_query("SELECT * FROM $db_table WHERE id=$id"winking smiley;
$image=mysql_fetch_array($row, MYSQL_ASSOC);


header("Content-type : image/png"winking smiley;
echo $image['img'];
mysql_free_result($row);

?>

Image not displaying, but it display its name when i try to view image on right click on it.
Please someone find the solution for my problem.

Options: ReplyQuote
Re: Image Uploading Problem
Posted by: yfastud (Moderator)
Date: October 30, 2011 03:28PM

You did NOT use the proper type to store the image. Google for it, but please do not post here since this is forums for Wampserver, not for coding. And again, if you really store images in your DB, it really affect your server/script performance. Not recommended sad smiley

Have fun,

FREE One A Day
FREE Photo
FREE Games
FREE Websites
FREE Portable GPS
FREE WAMP Guides

Options: ReplyQuote


Sorry, only registered users may post in this forum.