Hi All,
On posting a GET to this script, I take those values, along with the image, and upload as mysql blob.
On adding an additional variable to the url i get an error message. I can echo out the get variables to that page so i know they have been captured
Your help would be appreciated
The URL is: http://........./index.php?id=1& p=1
The error is: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null
and the code is
On posting a GET to this script, I take those values, along with the image, and upload as mysql blob.
On adding an additional variable to the url i get an error message. I can echo out the get variables to that page so i know they have been captured
Your help would be appreciated
The URL is: http://........./index.php?id=1& p=1
The error is: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null
and the code is
Code:
<?php
/*** check if a file was submitted ***/
if(!isset($_FILES['userfile']))
{
echo '<p>Please select a file</p>';
}
else
{
try {
upload();
/*** give praise and thanks to the php gods ***/
echo '<p>Thank you for submitting</p>';
}
catch(Exception $e)
{
echo '<h4>'.$e->getMessage().'</h4>';
}
}
?>
<?php
/**
*
* the upload function
*
* @access public
*
* @return void
*
*/
function upload(){
/*** check if a file was uploaded ***/
if(is_uploaded_file($_FILES['userfile']['tmp_name']) && getimagesize($_FILES['userfile']['tmp_name']) != false)
{
/*** get the image info. ***/
$size = getimagesize($_FILES['userfile']['tmp_name']);
/*** assign our variables ***/
echo $user = $_REQUEST['id'];
echo $owner = $_REQUEST['p'];
echo $type = $size['mime'];
$imgfp = fopen($_FILES['userfile']['tmp_name'], 'rb');
$size = $size[3];
$name = $_FILES['userfile']['name'];
$maxsize = 99999999;
/*** check the file is less than the maximum file size ***/
if($_FILES['userfile']['size'] < $maxsize )
{
/*** connect to db ***/
$dbh = new PDO("mysql:host=localhost;dbname=db", 'un', 'pw');
/*** set the error mode ***/
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/*** our sql query ***/
$stmt = $dbh->prepare("INSERT INTO testblob (user_id, item_id, image_type ,image, image_size, image_name) VALUES (?, ?, ? ,?, ?, ?)");
/*** bind the params ***/
$stmt->bindParam(1, $user);
$stmt->bindParam(2, $owner);
$stmt->bindParam(3, $type);
$stmt->bindParam(4, $imgfp, PDO::PARAM_LOB);
$stmt->bindParam(5, $size);
$stmt->bindParam(6, $name);
/*** execute the query ***/
$stmt->execute();
}
else
{
/*** throw an exception is image is not of type ***/
throw new Exception("File Size Error");
}
}
else
{
// if the file is not less than the maximum allowed, print an error
throw new Exception("Unsupported Image Format!");
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head><title>File Upload To Database</title></head>
<body>
<h2>Please Choose a File and click Submit</h2>
<form enctype="multipart/form-data" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="99999999" />
<div><input name="userfile" type="file" /></div>
<div><input type="submit" value="Submit" /></div>
</form>
</body></html>
Comment