Page 1 of 1

Error in insertion

Posted: Wed Jun 09, 2010 8:16 am
by tamilmani
My database connection is SQL Server .I have created one Table name as Webimages . Database field is image_content - datatype is image. When i am use below query to upload the images into sql server . I am getting error message .

Code: Select all

Connection established. Row insertion failed. Array ( [0] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 0 [code] => 0 [2] => [Microsoft][SQL Server Native Client 10.0]Syntax error, permission violation, or other nonspecific error [message] => [Microsoft][SQL Server Native Client 10.0]Syntax error, permission violation, or other nonspecific error ) ) 

Code: Select all

<?php
$uid = "username";
$pwd = "password";
$serverName = "servername";

$connectionInfo = array("UID" => $uid, "PWD" => $pwd, "Database"=>"databasename");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn )
{
     echo "Connection established.\n";
}
else
{
     echo "Connection could not be established.\n";
     die( print_r( sqlsrv_errors(), true));
}
  
if($_POST['submit']){
	move_uploaded_file($_FILES['image']['tmp_name'],"latest.img");
        $instr = fopen("latest.img","rb");
        $image = addslashes(fread($instr,filesize("latest.img")));
	  
$query = "INSERT INTO Webimages(image_content)values('$image')";
      $stmt = sqlsrv_prepare($conn,$query);
	  
 if( $stmt )
{
     echo "Row successfully inserted.\n";
}
else
{
     echo "Row insertion failed.\n";
     die( print_r( sqlsrv_errors(), true));
}
}
  
?>
<form method="post" action="" enctype="multipart/form-data">
<input type="file" name="image" id="image"  />
<input type="submit" name="submit" value="Submit"  />
</form>
Any Suggestion Please ??????

Re: Error in insertion

Posted: Wed Jun 09, 2010 9:37 am
by AbraCadaver
If I remember correctly though, SQL Server doesn't use slashes as escape characters. What is the column type of `image_content`?

Re: Error in insertion

Posted: Wed Jun 09, 2010 10:08 am
by tamilmani
This is my table structure

Code: Select all


id	int	no	4	10   	0    	no	(n/a)	(n/a)	NULL
image_content	image	no	16	     	     	yes	(n/a)	(n/a)	NULL

Re: Error in insertion

Posted: Wed Jun 09, 2010 10:26 am
by AbraCadaver
What does this do:

Code: Select all

$query = "INSERT INTO Webimages (image_content) values ('1')";
How about this:

Code: Select all

$image = file_get_contents("latest.img");
$query = "INSERT INTO Webimages (image_content) values ('$image')";

Re: Error in insertion

Posted: Wed Jun 09, 2010 10:42 am
by tamilmani
When i run the below query . The values are inserted into database. But values are in some hexa decimal format like (0x31)
So now my image_content values 0x31

Code: Select all

$query  = "INSERT INTO Webimages (image_content) values ('1')";
I run the second query . Still i am getting same error message .

Re: Error in insertion

Posted: Wed Jun 09, 2010 11:15 am
by AbraCadaver
Can you not use MySQL? It is so much easier :-)

If not, try this:

Code: Select all

$data = unpack("H*hex", file_get_contents("latest.img"));
$image = "0x".$data['hex'];
$query = "INSERT INTO Webimages (image_content) values ($image)";

Re: Error in insertion

Posted: Wed Jun 09, 2010 11:54 am
by tamilmani
Now i am able to insert into database . Below is my sample code.

Code: Select all

$tmpFileName = $_FILES['image']['tmp_name'];
$data = file_get_contents($tmpFileName);
$arr = unpack("H*hex", $data);
$Hexdata = "0x".$arr['hex'];
INSERT INTO Webimages (image_content) values ($Hexdata )";
View image code. When i used below code .Images are not loading . Just getting blank image . How i get the image from DB ? Any thing i need to change in my query ????

Code: Select all

$d_img = $row['image_content']; //This is coming from database.
$db_img = pack("H*".strlen($db_img), $db_img);
header("Content-type: image/jpeg");
echo $db_img; 

Re: Error in insertion

Posted: Wed Jun 09, 2010 1:34 pm
by AbraCadaver
It may not make sense (in my opinion SQL Server sucks) but just do this:

Code: Select all

header("Content-type: image/jpeg");
$row['image_content'];