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 -
<?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>
-
This is the paradox i dont understand.
On page load, i echo out the two values, 1 from session and 1 from get. The values exist.
On form submit, the content of P nolonger shows. and it all happens on 1 page.
there is nothing paradox in there. the URI you send the form values to simply does not contain the GET parameters ($_SERVER['PHP_SELF'] does only contain the file name) you’d either use $_SERVER['REQUEST_URI'] or append the GET using $_SERVER['QUERY_STRING'].
14 4457
what data type is user_id in your DB?
The data type is an INT, the same as owner.
The wierd thing is if it totally remove the user_id it all works fine
how about passing user_id with PDO::PARAM_INT ?
PS. if the statement is executed only once, $stmt->bindValue() should suffice.
I have amended my code to echo out the variables at each stage. The issue is that the P variable is assigned by GET when the form is displayed, but on clicking submit, its value is lost.
"Inside" and "outside" echo 1, but after submit the following is shown
top u1
top p
inside u1
inside p
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'item_id' cannot be null
outside u1
outside p
here is my amended code -
<?php
-
session_start();
-
echo "top u";
-
echo $_SESSION['user_id'];
-
echo "<br>";
-
echo "top p";
-
echo $_GET['p'];
-
/*** 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
-
echo "<br>";
-
echo "outside u";
-
echo $_SESSION['user_id'];
-
echo "<br>";
-
echo "outside p";
-
-
echo $user_id = $_GET['p'];
-
;
-
/**
-
*
-
* the upload function
-
*
-
* @access public
-
*
-
* @return void
-
*
-
*/
-
function upload(){
-
echo "<br>";
-
echo "inside u";
-
echo $_SESSION['user_id'];
-
echo "<br>";
-
echo "inside p";
-
$owner_id = $_GET['p'];
-
/*** 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 ***/
-
$owner_id = $_REQUEST['id'];
-
$user_id = $_SESSION['user_id'];
-
$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_id, PDO::PARAM_INT);
-
$stmt->bindParam(2, $owner_id, PDO::PARAM_INT);
-
$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>
-
I have amended my code to echo out the variables at each stage. The issue is that the P variable is assigned by GET when the form is displayed, but on clicking submit, its value is lost.
if these values don’t show up in the form’s action attribute, they are never there from the beginning.
This is the paradox i dont understand.
On page load, i echo out the two values, 1 from session and 1 from get. The values exist.
On form submit, the content of P nolonger shows. and it all happens on 1 page.
I have 3 echo's. top, outside and inside. The outside and inside refer to the upload(). When isset occurs its losing that value.
The screen output before selecting the file is:
top u1
top p1
Please select a file
outside u1
outside p1
This is the paradox i dont understand.
On page load, i echo out the two values, 1 from session and 1 from get. The values exist.
On form submit, the content of P nolonger shows. and it all happens on 1 page.
there is nothing paradox in there. the URI you send the form values to simply does not contain the GET parameters ($_SERVER['PHP_SELF'] does only contain the file name) you’d either use $_SERVER['REQUEST_URI'] or append the GET using $_SERVER['QUERY_STRING'].
I have been into the database and set both values to allow NULL.
So now my problem lies with the P variable not passing into the function.
the url on the upload page looks like this.... http://..................../index.php?p=1
sorry if I’m sounding rude, but did you understand, what I tried to tell you the last 2 posts?
sorry i didnt refresh, just seen them now.
Ok, so the session variable is fine as its global.
Please could you help me with the syntax and how i could amend it?
I appreciate your time
Hi,
I have finally managed to get it working!! Took a while and a lot of scrap paper.
I was calling the url information in the wrong place. I moved it down to the form and then added two hidden elements that posted the variable to SELF then carried on as normal.
Thank you very much for your patience and help with this
- <form enctype="multipart/form-data" action="<?php echo htmlentities($_SERVER['REQUEST_URI']);?>" method="post">
didn’t work?
Sign in to post your reply or Sign up for a free account.
Similar topics
by: JL |
last post by:
Platform: Linux Red Hat RHEL 3 (and red hat 9)
Installed MySQL from source. As a matter of fact, installed all LAMPS
from source, and the mysql socket file was arranged in a place other
than...
|
by: Lenz Grimmer |
last post by:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,
MySQL 4.0.14, a new version of the popular Open Source/Free Software
Database, has been released. It is now available in source and binary...
|
by: Kirk Soodhalter |
last post by:
Hi,
This started as a phpmyadmin problem, but has somehow morphed into a
mysql problem. I don't know how to fix it. I am posting the
conversation from a php newsgroup since it started there. ...
|
by: Plymouth Acclaim |
last post by:
Hi guys,
We have a problem with Dual AMD64 Opteron/MySQL 4.0.18/Mandrake 10
for a very high volume site. We are evaluating the performance on our
new server AMD64 and it seems it's slow compared...
|
by: Alex Hunsley |
last post by:
I am trying to install the DBD::mysql perl module. However, it claims I
need mysql.h:
cpan> install DBD::mysql
CPAN: Storable loaded ok
Going to read /home/alex/.cpan/Metadata
Database was...
|
by: smsabu2002 |
last post by:
Hi,
I am facing the build problem while installing the DBD-MySql perl
module (ver 2.9008) using both GCC and CC compilers in HP-UX machine.
For the Build using GCC, the compiler error is...
|
by: jrs_14618 |
last post by:
Hello All,
This post is essentially a reply a previous post/thread
here on this mailing.database.myodbc group titled:
MySQL 4.0, FULL-TEXT Indexing and Search Arabic Data, Unicode
I was...
|
by: alf |
last post by:
Hi,
is it possible that due to OS crash or mysql itself crash or some e.g.
SCSI failure to lose all the data stored in the table (let's say million
of 1KB rows). In other words what is the worst...
|
by: alex |
last post by:
I've converted a latin1 database I have to utf8. The process has been:
# mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore
--skip-set-charset mydb mydb.sql
# iconv -f...
|
by: Caffeneide |
last post by:
I'm using a php script which performs three xml queries to other three
servers to retrieve a set of ids and after I do a query to mysql of
the kind
SELECT * FROM table WHERE id IN ('set of ids');...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
|
by: ezappsrUS |
last post by:
Hi,
I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
| |