Error in insertion

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tamilmani
Forum Commoner
Posts: 39
Joined: Tue Apr 01, 2008 2:53 am

Error in insertion

Post 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 ??????
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Error in insertion

Post by AbraCadaver »

If I remember correctly though, SQL Server doesn't use slashes as escape characters. What is the column type of `image_content`?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
tamilmani
Forum Commoner
Posts: 39
Joined: Tue Apr 01, 2008 2:53 am

Re: Error in insertion

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Error in insertion

Post 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')";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
tamilmani
Forum Commoner
Posts: 39
Joined: Tue Apr 01, 2008 2:53 am

Re: Error in insertion

Post 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 .
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Error in insertion

Post 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)";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
tamilmani
Forum Commoner
Posts: 39
Joined: Tue Apr 01, 2008 2:53 am

Re: Error in insertion

Post 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; 
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Error in insertion

Post 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'];
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply