Please help me corect my code. I want to insert image into mysql with php but it can't. my code wrote as below:

Table:
CREATE TABLE `TblClothes` (
`ClothesID` int(11) NOT NULL auto_increment,
`ClothesName` varchar(100) NOT NULL,
`Description` varchar(800) NOT NULL,
`Colour` varchar(100) NOT NULL,
`CSize` varchar(100) NOT NULL,
`Price` float NOT NULL,
`Category` varchar(200) NOT NULL,
`Picture` mediumblob NOT NULL,
PRIMARY KEY  (`ClothesID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
===========================================================
Upload.htm
<html>
<head>
<title>Test PHP</title>
</head>


<body>
<div id="Layer1" style="position:absolute; width:445px; height:275px; z-index:1; left: 432px; top: 126px; background-color: #CCCCCC; layer-background-color: #CCCCCC; border: 1px none #000000;">
<form enctype="multipart/form-data" action="upload.php" method="post">
<table width="389" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td width="79" height="24"><span class="style7">&nbsp;Name : </span></td>
<td width="13"><div align="center"><span class="style9">*</span></div></td>
<td width="297" height="24"><input name="form_cname" type="text" id="form_cname" width="150">
</td>
</tr>
<tr>
<td height="24"><span class="style7">&nbsp;Colour : </span></td>
<td><div align="center"></div></td>
<td height="24"><span class="style15"><input name="form_colour" type="text" width="150">
</span></td>
</tr>
<tr>
<td height="24"><span class="style7">&nbsp;Size :</span></td>
<td><div align="center"></div></td>
<td height="24"><input name="form_size" type="text" width="150">
</td>
</tr>
<tr>
<td height="24"><span class="style7">&nbsp;Price :</span></td>
<td><div align="center"><span class="style9">*</span></div></td>
<td height="24"><input name="form_price" type="text" value="0" width="150">
</td>
</tr>
<tr>
<td height="24"><span class="style7">&nbsp;Category :</span></td>
<td><div align="center"><span class="style9">*</span></div></td>
<td height="24"><select name="form_category">
<option value="Accessories">Accessories</option>
<option value="Casual Day Wear">Casual Day Wear</option>
<option value="Formal Evening Wear">Formal Evening Wear</option>
</select>
</td>
</tr>
<tr>
<td height="24" valign="top"><span class="style7">&nbsp;Description :</span></td>
<td><div align="center"></div></td>
<td height="24"><textarea name="form_description" cols="40" rows="4"></textarea></td>
</tr>
<tr>
<td height="24"><span class="style7">&nbsp;Picture :</span></td>
<td><div align="center"><span class="style9">*</span></div></td>
<td height="24"><input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="form_picture" type="file"></td>
</tr>
<tr valign="bottom">
<td height="30">&nbsp;</td>
<td height="30" colspan="2"><span class="style9">*</span><span class="style11"> Required to fill up.</span></td>
</tr>
<tr valign="bottom">
<td height="30">&nbsp;</td>
<td height="30">&nbsp;</td>
<td height="30"><p>
<input type="submit" value=" Upload ">
</p>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
===========================================================
Upload.php
<?php
$username = "hanum3_admin";
$password = "admin";
$hostname = "localhost";


$cname = $_POST;
$ccolour = $_POST;
$csize = $_POST;
$cprice = $_POST;
$ccategory = $_POST;
$cdescription = $_POST;
//$cpicture = $_POST;


if ($cname ==''){
//$strerror = "Please entry name!";
header ('location: http://www.kambuja-designs.com/uploaderror.php?errortitle=Error!&errormsg=Please entry name!');
}


if ($cprice=="0"){
//$errortitle = "Error!";
//$strerror = "Please entry price bigger than zero!";
header ('location: http://www.kambuja-designs.com/uploaderror.php?errortitle=Error!&errormsg=Please entry price bigger than zero!');
}


if ($ccategory ==''){
//$strerror = "Please select category!";
header ('location: http://www.kambuja-designs.com/uploaderror.php?errortitle=Error!&errormsg=Please select category!');
}


$cname = str_replace("'","''",$cname);
if ($ccolour ==''){
$ccolour = " ";
}
if ($csize ==''){
$csize =" ";
}
if ($cdescription ==''){
$cdescription =" ";
} else {
$cdescription = str_replace("'","''",$cdescription);
}


if (isset($_FILES)){
@list(,,$imgtype,) = getimagesize($_FILES,);
if ($imgtype == 3)
$ect = "png";
elseif ($imgtype == 2)
$ext = "jpeg";
elseif ($imgtype == 1)
$ext ="gif";
else
header ('location: http://www.kambuja-designs.com/uploaderror.php?errortitle=Error!&errormsg=File not support!');


$content = file_get_contents($_FILES);
$content = mysql_real_escape_string($content);
}


$dbh = mysql_connect($hostname, $username, $password) or die("Could not connect to server");
$selected = mysql_select_db("hanum3_Kambuja",$dbh) or die("Could not connect to server");
if (mysql_query("Insert into TblClothes(ClothesName,Description,Colour,CSize,Price,Category,Picture) values('".$cname."','".$cdescription."','".$ccolour."','".$ccsize."',".$cprice.",'".$ccategory."','".$content."')")) {
header ('location: http://www.kambuja-designs.com/uploaderror.php?errortitle=Upload Successfull!&errormsg= ');
} else {
header ('location: http://www.kambuja-designs.com/uploaderror.php?errortitle=Upload Unsuccessfull!&errormsg=Please check your filed again!');
}
mysql_close($dbh);
?>
===========================================================
uploaderror.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Test PHP</title>


</head>


<body>


<div id="Layer1" style="position:absolute; width:445px; height:73px; z-index:1; left: 432px; top: 126px; background-color: #CCCCCC; layer-background-color: #CCCCCC; border: 1px none #000000;">
<form action="upload.htm">
<table width="439" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="8">&nbsp;</td>
<td width="431"><span class="style9">
<?
$errortitle = $_REQUEST["errortitle"];
if ($errortitle ==''){
echo " ";
} else {
echo $errortitle;
}
?>
</span></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><span class="style10">
<?
$errormsg = $_REQUEST["errormsg"];
if ($errormsg ==''){
echo " ";
} else {
echo $errormsg;
}
?>
</span></td>
</tr>
<tr valign="bottom">
<td height="30" colspan="2"><p align="center">
<input type="submit" value="  Back  ">
</p></td>
</tr>
</table>
</form>
</div>
<table width="770" height="444" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="770" height="444">&nbsp;</td>
</tr>
</table>
</body>
</html>

Thanks in advance

Edited 3 Years Ago by happygeek: fixed formatting

why are you trying to store them in the database?

its better to store them in a folder on the server and put the path to the file in the database.

please make use of [ co de ][ /co de ] when posting code snips.. makes it easier on the eyes for reading and finding problems.

I will share some of my own code but you need to try and figure it on your own:

The upload form: 
<form enctype="multipart/form-data" action="upload.php" method="post" name="upload_file"> 
<input type="hidden" name="MAX_FILE_SIZE" value="5000000"> 
<input name="userfile" type="file"> 
<input type="submit" value="upload" name="file_uploaded"> 
</form> 

upload.php: 
<?php 
// file upload - in php 4.1.0 or later, $_FILES should be used instead of $HTTP_POST_FILES. 

if (is_uploaded_file($_FILES['userfile']['tmp_name'])) 
{ 
//upload 
$file_realname = $_FILES['userfile']['name']; 

//PUT in FILDER 
copy($_FILES['userfile']['tmp_name'], "path/to/folder/goes/here/$file_realname"); 
} 
else 
{ 
echo "<b><font color=red>No file uploaded.</font></b><BR>No file available or file too big to upload."; 
} 
?> 

$file_realname can be saved in database.

i worked on ur code as kkeith suggested try to insert image path into database imade changes according to that and it worked for me here it is

<?php
$username = "hanum3_admin";
$password = "admin";
$hostname = "localhost";


$cname = $_POST;
$ccolour = $_POST;
$csize = $_POST;
$cprice = $_POST;
$ccategory = $_POST;
$cdescription = $_POST;
//$cpicture = $_POST;


if ($cname ==''){
//$strerror = "Please entry name!";
header ('location: http://www.kambuja-designs.com/uploa...rrormsg=Please entry name!');
}


if ($cprice=="0"){
//$errortitle = "Error!";
//$strerror = "Please entry price bigger than zero!";
header ('location: http://www.kambuja-designs.com/uploa...rrormsg=Please entry price bigger than zero!');
}


if ($ccategory ==''){
//$strerror = "Please select category!";
header ('location: http://www.kambuja-designs.com/uploa...rrormsg=Please select category!');
}


$cname = str_replace("'","''",$cname);
if ($ccolour ==''){
$ccolour = " ";
}
if ($csize ==''){
$csize =" ";
}
if ($cdescription ==''){
$cdescription =" ";
} else {
$cdescription = str_replace("'","''",$cdescription);
}


if (isset($_FILES)){
if(($HTTP_POST_FILES["form_picture"]["size"] )>1)
{
$pro_img=substr($HTTP_POST_FILES["form_picture"],0,strpos($HTTP_POST_FILES["form_picture"],'.'));
$commonname="pro_".time();
$fullname=$commonname.strstr($HTTP_POST_FILES["form_picture"],'.');
$pro_imgpath="uplodedimages/".$fullname;
move_uploaded_file($HTTP_POST_FILES["form_picture"],$pro_imgpath);
}
else
header ('location: http://www.kambuja-designs.com/uploa...&errormsg=File not support!');


$content = file_get_contents($_FILES);
$content = mysql_real_escape_string($content);
}


$dbh = mysql_connect($hostname, $username, $password) or die("Could not connect to server");
$selected = mysql_select_db("hanum3_Kambuja",$dbh) or die("Could not connect to server");
if (mysql_query("Insert into TblClothes(ClothesName,Description,Colour,CSize,Price,Category,Picture) values('".$cname."','".$cdescription."','".$ccolour."','".$csize."',".$cprice.",'".$ccategory."','".$pro_imgpath."')")) {
//header ('location: http://www.kambuja-designs.com/uploa...ortitle=Upload Successfull!&errormsg= ');
} else {
//header ('location: http://www.kambuja-designs.com/uploa...ortitle=Upload Unsuccessfull!&errormsg=Please check your filed again!');
}
mysql_close($dbh);
?>

simply copy and paste it i am sure you will get it
dont forget to create a folder uplodedimages in ur server

Edited 3 Years Ago by happygeek: fixed formatting

Try this code, it will work better...

But we can insert image into a folder and That path will be saved into database table..

check it once..

<?


include('connection.php');


if($_SERVER=='POST') {


$dupcnt=getdata("products","count(*)","prcode='".$_POST."'");
$dupcnt2=getdata("products","count(*)","prname='".$_POST."' && catid='".$_POST."'");
if($dupcnt>0 && $dupcnt2>0){
$err="Product With this Name and code already existed.";
} else if($dupcnt>0){
$err="Product With this code already existed.";
} else if($dupcnt2>0){
$err="Product With this name already existed.";
} else {
if($HTTP_POST_FILES["prlimage"]["size"] >1) {    // for  large image 1
$ext = substr($HTTP_POST_FILES,strpos($HTTP_POST_FILES,'.')+1);
$ext_arr=array("jpeg","ping","jpg","gif","bmp");
//if (in_array($ext,$ext_arr)) {
$prlimage=substr($HTTP_POST_FILES,0,strpos($HTTP_POST_FILES,'.'));
$prlimage.=time();
$prlimage.=strstr($HTTP_POST_FILES,'.');
$prlimage="../productimages/".$prlimage;
if(!move_uploaded_file($HTTP_POST_FILES,$prlimage)) { $prlimage=""; }
chmod($prlimage,0777);
} else {
$prlimage="";
}
/*}  else {
$err="Please Upload image only ";
}*/
if($HTTP_POST_FILES["prsimage"]["size"] >1) {    // for  small image 1
$prsimage=substr($HTTP_POST_FILES,0,strpos($HTTP_POST_FILES,'.'));
$ext = substr($HTTP_POST_FILES,strpos($HTTP_POST_FILES,'.')+1);
$ext_arr=array(".jpeg",".ping",".jpg",".gif",".bmp");
//if (in_array($ext,$ext_arr)) {
$prsimage.=time();
$prsimage.=strstr($HTTP_POST_FILES,'.');
$prsimage="../productimages/small/".$prsimage;
if(!move_uploaded_file($HTTP_POST_FILES,$prsimage)) { $prsimage=""; }
chmod($prsimage,0777);
$image_attribs = getimagesize($prsimage);
$width=$image_attribs[0];
$height=$image_attribs[1];
if($width > 75){
@unlink($prsimage);
$err="Please Upload small image of size 75 x 75 ";
}
} else {
$prsimage="";
}
/*} else {
$err="Please Upload image only ";
}*/


if(!empty($_POST)) $prhome='1'; else $prhome='0';
if(!isset($err)) { //add`sub_catid`,'".$_POST."', `prstock`,'".$_POST."',
$qur="insert into `products` (`catid`,  `prname`, `prcode`, `prsdesc`, `prldesc`, `prsimg`, `prlimg`, `prhot`,  `prstatus`, `currdate`) values ('".$_POST."', '".$_POST."', '".$_POST."', '".$_POST."', '".$_POST."', '".$prsimage."', '".$prlimage."', '".$prhome."', '1', now())";
$res= mysql_query( $qur ) ;
$id=mysql_insert_id();
echo'<script language="javascript">window.location.href="success.php'";</script>';
}
} //end for else of product count
} // post



?>

Thanks,
Shanti.

Edited 3 Years Ago by happygeek: fixed formatting

Comments
nice job Shanti

nice Shanti, works on my test server no problems.

make sure to create the new files sugested in the code above:
connection.php
success.php

also dont forget to create a folder on your server: productimages and productimages/small

depending on where your folder is you may need to edit this line:

$prsimage="../productimages/small/".$prsimage;

as well as:

$prlimage="../productimages/".$prlimage;

Shanti very nice job, simplistic and best of all it is workable however when helping someone it is best to use
*/ footnotes /* to explain what you are doing so the coder can get a good understanding of the code, particularly if the coder is beginner or novice.
other than that nice job shanti and the op should be able to solve his issue with what you gave him.

swatisinha.339, go post your advertising elsewhere please. I have reported all 18 of your posts to the mods and hope for an admin to delete your posts and ban your IP so you can not create another account here again.

This article has been dead for over six months. Start a new discussion instead.