/    Sign up×
Community /Pin to ProfileBookmark

File upload and mySQL uploading

I’m working on an upload file that will allow me to upload a file to “images/’ directory, and at the same time, put in info into the database ‘images’ name, location (taken from the file upload), month taken, and rel. link.
I want all these to be text so I can pull then into hmtl code, e.g <img> tags, or hyperlinks.

What I’ve done is create a form which has name, month, link and file upload in it. The file upload goes to images/ , and the name, month, link and location of the file upload goes to a database along with and automated ID.

This is the html form:

[code=html]<form method=”post” action=”upload.php” enctype=”multipart/form-data”>
Name: (100 characters)<br>
<input type=”text” name=”name_given” size=”80″ maxlength=”60″>
<br>Month:<br>
<input type=”text” name=”month” size=”30″>
<br>Link:<br>
<input type=”text” name=”link” size=”30″>
<input type=”hidden” name=”MAX_FILE_SIZE” value=”1500000″>
<br>File to upload:<br>
<input type=”file” name=”userfile” size=”40″>
<p><input type=”submit” name=”submit” value=”Submit”>
</form>
[/code]

This is my php code so far:

[code=php]<?php
mysql_connect(“******”,”******”,”*****”) or die(mysql_error());
mysql_select_db(“*************”) or die(mysql_error());
$uploaddir = ‘images/’;

$uploadfile = $uploaddir.$_FILES[‘userfile’][‘name’];
$userfile =$_FILES[‘userfile’][‘name’];

$name=$_POST[‘name_given’];
$month=$_POST[‘month’];
$link=$_POST[‘link’];

$result = MYSQL_QUERY(“INSERT INTO `images` (‘name’,’location’,’month’,’link’) “. “VALUES (‘$name’,’$uploadfile’,’$month’,’$link’)”);
$id= mysql_insert_id();

if (move_uploaded_file($_FILES[‘userfile’][‘tmp_name’],$uploadfile)){

print “<p>File ID: <b>$id</b><br>”;
print “Name: <b>$name</b><br>”;
print “Location: <b>$uploadfile</b><br>”;
print “Month: <b>$month</b><br>”;
print “Featured Link: <b>$link</b><br></p>”;
echo ‘<center>The file <strong>”‘.$_FILES[‘userfile’][‘name’].'”</strong> successfully uploaded to <a href=”‘.$uploadfile.'” target=”_blank”>link</a>’;

}
else {
print “<center><font color=red><strong>Something is wrong, please try again!</font>”;

}
?>

[/code]

When I use it, the image gets uploaded fine, but the mySQL data doesn’t go anywhere.

It shows as:
id: 0
name: [name of picture given]
month: [month field]
link: [link field]
file got uploaded successfully to [link]

When I go and check the database, it’s not go anything, “0 rows”

Can I please have some help on finding out what’s wrong?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@Hells-TearsauthorJan 04.2009 — Spoke too soon.

Data shows as successful, and details appear. But when I check the database, it's not there..
Copy linkTweet thisAlerts:
@chazzyJan 04.2009 — [code=php]
$result = MYSQL_QUERY("INSERT INTO images ('name','location','month','link') ". "VALUES ('$name','$uploadfile','$month','$link')") or die("I has an error in my query ".mysql_error());
[/code]


The column names should have ` around them not '
Copy linkTweet thisAlerts:
@Hells-TearsauthorJan 04.2009 — Cheers for the help, now I've actually got a return, but not a good one:

[CODE]I has an error in my query You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''name','location','month','link') VALUES ('Lacanau Ocean #1','images/001.jpg','A' at line 1[/CODE]
Copy linkTweet thisAlerts:
@chazzyJan 04.2009 — I don't think you read my post.

[code=php]
$result = MYSQL_QUERY("INSERT INTO images ('name','location','month','link') ". "VALUES ('$name','$uploadfile','$month','$link')") or die("I has an error in my query ".mysql_error());
[/code]


The column names should have around them not '[/QUOTE]</QUOTE>

Your query should be.

<CODE>&lt;i&gt;
&lt;/i&gt;INSERT INTO </code></span>images<span><code> (</code></span>name<span><code>,</code></span>location<span><code>,</code></span>month<span><code>,</code></span>link`) VALUES ('$name','$uploadfile','$month','$link')
×

Success!

Help @Hells-Tears spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...