Jump to content

Recommended Posts

Hi guys, this is my first post here but I am not a newbie :P

 

What am I trying to do?

 

I am trying to upload an Image to a server along with a record set of the image's data.

 

 

How am I going to do it?

 

Connection to a MyISAM MySQL database via siteroot/includes/database/connect.php.

 

Fields in database:

 

sID (PK autoindex)

txtPerson                - submitters name (allow null)

txtComments           - comments about the image

imgMainpic              - for the picture

 

A form on siteroot/submit.php

action="includes/submit/submit.php".

 

on includes/submit/submit.php


#######################################################
#© AscIId 2007 This file MUST NOT be distrubuted or copied in ANY WAY AT ALL #
#                                                                                                           #
#This file submits data to the database                                                         #
########################################################


#####         /////     connect to db      \\\\\

includeonce ("../database/connect.php");

#####         /////     retreve data from form     \\\\\

#Person Name From Form
$txtPerson = $_POST["txtPerson"]
  
#Comments from Form
$txtComments = $_POST["txtComments"]

#Image from form
$imgMainpic = $_POST["imgMainpic"]

#more fields to added here later (not listed here because of security)

############ <image stuff goes here> ##############

$query = "INSERT INTO tblSightings VALUES('NULL',$txtPerson, $txtComments, $imgMainpic, $blah)";

$result = mysql_query($query);

if(mysql_affected_rows() > 0){
if($debug==true){
echo ("Record Uploaded");
}
}
}
else {
if($debug==true){
echo ("Record Upload Failed");
}
die ("There was an error in submission, please try again later   " . $mysql_error() ;
}

 

What i need help with is uploading the image to the database. Could this be done by changing the image to a binary BLOB? Please give me some code examples :) I would find that really helpful.

 

AscIId

Link to comment
https://forums.phpfreaks.com/topic/54494-uploading-picture-to-an-sql-server/
Share on other sites

I wrote this out while gabeg was replying, too, so I might as well still post it....

 

Maybe I've always gone about this incorrectly, but instead of burdening the database with your image files, I would just move the uploaded file into a folder with a name that corresponds to a database entry for its information. Example:

 

<?php
///after user submits entry and all entries are validated

@mysql_query("INSERT INTO tblSightings VALUES('NULL',$txtPerson, $txtComments, $blah)");
//run your query checks, then get an id if it worked
$photoName = @mysql_insert_id();

//use the insert ID as the photo name in a specific folder on your site
$newDest = '/path/to/special/folder/'.$photoName;
move_uploaded_file($_FILES["uploadImg"]["tmp_name"],$newDest);

?>

 

Just a quick example; I hope you get the gist, anyway.

yea, that would be helpful :P

 

it can connect to the DB, no problem... however when i do

 

@mysql_query ("INSERT INTO tblSightings VALUES('NULL', $blah,$blah,$postComments,$blah)");

 

it does not affect any rows in the table... without the image script, i need this to work before the image script can be used :S

 

???

 

The @ supresses errors, which is why it is not working because it is throwing an error

 

@mysql_query ("INSERT INTO tblSightings VALUES(NULL, '$blah','$blah','$postComments','$blah')") OR DIE(mysql_error());

 

NULL should not be encapsulated in single quotes. Which chances are is where your error comes from that and not having single quotes around the values.

Weird man, have you tried listing the column names out?

 

mysql_query ("INSERT INTO tblSightings (`col1`, `col1`, `col3) VALUES(NULL, '$blah','$blah','$postComments','$blah')") OR DIE(mysql_error());

 

And seeing if that works?

 

Also have you verified in your database that no rows are being entered? Mysql_affected may only apply to update/selects I am unsure.

i would suggest you save the file via the filesystem (ie. as an actual image file) and store the FILENAME in the database.  in this case, it's a matter of setting up its filename, ensuring it's unique, moving the temp file to its final location (move_uploaded_file()), and inserting its entry into the database.

 

have a look in the manual regarding handling file uploads; there's an entire section on it.  should give you some good background.  if you wanted to save the file as its binary format, you'd need a BLOB column in your table.  i've yet to see a situation where this is preferable, though.

FORGET THE IMAGE UPLOAD, i am still trying to upload the text that should go with the image

 

adding some records to the SQL database....

 

if i type into my form "blah" in all the fields, (not including the upload image field) and submit the data the data does not appear in the database (no rows added) even though there are no errors thrown.

 

FORGET THE IMAGE UPLOAD, i am still trying to upload the text that should go with the image

 

adding some records to the SQL database....

 

if i type into my form "blah" in all the fields, (not including the upload image field) and submit the data the data does not appear in the database (no rows added) even though there are no errors thrown.

 

 

attach your file or files please!



$file = $_Post["carImage"];
$blah=  $_Post["blah"];
$postComments = $_POST["postComments"]; 


####################
# connect.php
####################

$hostname_mshe = "localhost";
$database_mshe =  //removed
$username_mshe = //removed
$password_mshe = //removed
$mshame = mysql_connect($hostname_mshe, $username_mshe, $password_mshe) or die ("Unable to connect to DB");


##########
#submit.php
##########
mysql_select_db("mshe", $mshe);


$query = ("INSERT INTO `tblSightings` ( `sID` , `blah` , `blah` , `postComments` ) VALUES ( NULL, '$blah','$blah')" OR DIE("Somehing went wrong   " . mysql_error()));

#$carImage = @mysql_insert_id();

//use the insert ID as the photo name in a specific folder on your site
#$newDest = 'xxxxxxxxxxxxxxxxxx/uploadpics/'.$carImage;
#move_uploaded_file($_FILES["uploadImg"]["tmp_name"],$newDest);

$result = mysql_query($query);

if(mysql_affected_rows() > 0){

echo "Image has been inserted succesfully";
}
else {
echo "Image can not be inserted check your submission";
}

##############
#disconnect.php
##############
mysql_close ($mshe);

 

 

that is my code

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.