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 4532
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: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |