I'm trying to upload an image into an SQL server using a simple form however I'm being faced with the following warning, which also prevents me from uploading the image:
Warning: odbc_exec(): in C:\wamp\www\inputform\index2.php on line 10
Line 10: $sql_run = odbc_exec($con, $sql);
The code is:
<?php
require ('connect.inc.php');
if(ISSET($_POST['submit'])){
$imagename = $_FILES["image"]["name"];
$imagedata = file_get_contents($_FILES["image"]["tmp_name"]);
echo $imagename." ".$imagedata;
$sql = "INSERT INTO test.dbo.images (imageid, imagedata) VALUES('','$imagedata')";
$sql_run = odbc_exec($con, $sql);
echo "Query with the following details has been executed: <br>".$imagename;
}
else{
echo 'Is not set';
}
?>
<form action="index2.php" method="POST" enctype="multipart/form-data">
Image: <input type="file" name="image"><br>
<input type="submit" value="Submit" name="submit">
</form>
The file connect.inc.php consists of the code below:
<?php
$serverName="TESTSERV\SQLEXPRESS";
$dsn='odbc-test';
$user = 'user';
$password = 'password';
$db = 'test';
if(!$con = odbc_connect($dsn, $user, $password)){
echo "Not Connected";
}
?>
Thanks in advance. J
The query looks fine so the only logical explanation is that the datatypes you are trying to insert are not same. Make sure that imageid
and imagedata
datatypes are varchar
EDIT: Make sure that the datatypes are same. here is the updated answer.
<?php
require ('connect.inc.php');
if(ISSET($_POST['submit'])){
$imagename = $_FILES["image"]["name"];
$imagedata = (binary)file_get_contents($_FILES["image"]["tmp_name"]);
echo $imagename." ".$imagedata;
$sql = "INSERT INTO test.dbo.images (imageid, imagedata) VALUES('','.$imagedata.')";
$sql_run = odbc_exec($con, $sql);
echo "Query with the following details has been executed: <br>".$imagename;
}
else{
echo 'Is not set';
}
?>
note: $imagedata = (binary)file_get_contents($_FILES["image"]["tmp_name"]);