Insert/update query on SQLite database with PDO works on local, but fails silently online [duplicate]
I have noticed the following very strange behaviour. I insert/update data from a webform into an SQL database with PDO. I have a number of different pages, some of which contain image upload fields.
Now for the strange thing: I use the following method to insert/update the database:
// For one of the pages with an image upload (works perfectly on localhost as well as on a server):
// For a new insert
$query = "INSERT INTO ZCATEGORIES (ZIMAGE, ZIMAGETYPE, ZNAME, ZCOLOR) VALUES ('?', 'image/png', 'newCat', '#F78A9FF')";
// When there is already something in the db under the id Z_PK
$query = "UPDATE ZCATEGORIES SET ZIMAGE='?', ZIMAGETYPE='image/png', ZNAME='newCat', ZCOLOR='#F78A9FF', WHERE Z_PK='1'";
$sth = $db->prepare($query); // $query is a correctly formatted SQL query string, I have checked that thoroughly
// Image validation is done earlier on the page
if (is_uploaded_file($_FILES[$fieldname]['tmp_name'])
$img = file_get_contents($_FILES[$fieldname]['tmp_name']);
$sth->bindParam(1, $img, PDO::PARAM_LOB);
}
$db->beginTransaction();
$sth->execute();
$db->commit();
$sth->closeCursor();
The following causes problems:
// For one of the pages without an image upload (works on localhost but NOT on a server):
// For a new insert
$query = "INSERT INTO ZCUSTOM (ZFONT, ZTEXTCOLOR, ZBUTTONCOLOR, ZBUTTONTEXTCOLOR) VALUES ('Monaco', '#FF0000', '#1CFF49', '#F78A9FF')";
// When there is already something in the db
$query = "UPDATE ZCUSTOM SET ZFONT='Monaco', ZTEXTCOLOR='#FF0000', ZBUTTONCOLOR='#1CFF49', ZBUTTONTEXTCOLOR='#F78A9FF'";
$sth = $db->prepare($query); // $query is a correctly formatted SQL query string, I have checked that thoroughly
$db->beginTransaction();
$sth->execute();
$db->com开发者_JS百科mit();
$sth->closeCursor();
But this DOES work again on both localhost and server
// For one of the pages without an image upload (works both on localhost and on a server):
// For a new insert
$query = "INSERT INTO ZCUSTOM (ZFONT, ZTEXTCOLOR, ZBUTTONCOLOR, ZBUTTONTEXTCOLOR) VALUES ('Monaco', '#FF0000', '#1CFF49', '#F78A9FF')";
// When there is already something in the db
$query = "UPDATE ZCUSTOM SET ZFONT='Monaco', ZTEXTCOLOR='#FF0000', ZBUTTONCOLOR='#1CFF49', ZBUTTONTEXTCOLOR='#F78A9FF'";
$db->query($query);
Now the above method works perfectly on my localhost. Both the pages which contain text input only and the pages which contain text as well as an image get stored in the database successfuly. However the problem arises when trying to insert/update the database on a webserver. Then for some reason, the form submit on the pages containing text and image data still works like expected. However, nothing gets inserted into the db on the pages only containing text data...
After numerous hours of desperately trying out different things, I decided to exchange the whole prepare, beginTransaction, execute... procedure on these troublesome pages with a simple
$db->query($query);
and much to my surprise now it works fine online! Is there something I'm missing here? I don't understand why this particular problem has anything to do with trying things out locally or via a webserver... Why should it all work flawlessly on my localhost, but cause problems online? And bizarly, only for ceratin pages? I've checked that both the db and the folder it's in are both readable and writeable. I'm on mac OSX 10.6.7 and am running PHP version 5.3.4
If anyone has an idea what might cause the problems, then please share. I would be very grateful.
I've checked that both the db and the folder it's in are both readable and writeable.
I managed to solve a similar problem by making the entire path to the database (at least, from your home directory) readable and executable, in addition to making the folder the DB is in readable and writeable.
(Note: I'm talking about changing permissions on the server, not on your local machine.)
For example, if your site lives in
~/Sites/mysite
and your database is located at
~/Sites/mysite/db/main.db
you could use the following commands in BASH to set the appropriate permissions:
$ chmod a+xr ~/Sites
$ chmod a+xr ~/Sites/mysite
$ chmod -R a+xrw ~/Sites/mysite/db/
Hope this helps.
精彩评论