/    Sign up×
Community /Pin to ProfileBookmark

Upload image at same time as updating database

Please ignore – I have worked out the problem

Hi there

I have a php script that successfully will allow me to upload an image the images folder on my server.

[code]

<?
//print_r($_POST);

unset($imagename);

if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;

if(!isset($_FILES[‘image_file’]))
$error[“image_file”] = “An image was not found.”;

$imagename = basename($_FILES[‘image_file’][‘name’]);
echo $imagename;

if(empty($imagename))
$error[“imagename”] = “The name of the image was not found.”;

if(empty($error))
{
$newimage = “../images/” . $imagename;
echo ‘<img src=”‘.$newimage.'” />’;

$result = @move_uploaded_file($_FILES[‘image_file’][‘tmp_name’], $newimage);
if(empty($result))
$error[“result”] = “There was an error moving the uploaded file.”;
}

?>

<form method=”POST” enctype=”multipart/form-data” name=”image_upload_form” action=”<?$_SERVER[“PHP_SELF”];?>”>
<p><input type=”file” name=”image_file” size=”20″></p>
<p><input type=”submit” value=”Upload Image” name=”action”></p>
</form>

<?
if(is_array($error))
{
while(list($key, $val) = each($error))
{
echo $val;
echo “<br>n”;
}
}
?>

[/code]

And I also have a php page that successfully updates a database with a new record.

I have tried to combine the two so when adding a new record, it allows you to select an image and upload it and store the filename in the database with the other details of the new record.

My code is adding the new record but is not adding any data for the imagename to the database or uploading the image.

Here is my php code:

[code]

<?php

if (isset($source)) {

if ($source == ‘add_musician2.htm’) {

include “config.php”;
$db = mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db ($db_name) or die (“Cannot connect to database”);

// ——————–
// Deal with image file
// ——————–

unset($imagename);

if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;

if(!isset($_FILES[‘image_file’]))
$error[“image_file”] = “An image was not found.”;

$imagename = basename($_FILES[‘image_file’][‘name’]);
echo $imagename;

if(empty($imagename))
$error[“imagename”] = “The name of the image was not found.”;

if(empty($error))
{
$newimage = “../images/” . $imagename;
echo ‘<img src=”‘.$newimage.'” />’;

$result = @move_uploaded_file($_FILES[‘image_file’][‘tmp_name’], $newimage);
if(empty($result))
$error[“result”] = “There was an error moving the uploaded file.”;
}

$name = $_POST[‘bandname’];

if(isset ($_POST[‘brass’])) {
$brass = 1;
} else {
$brass = 0;

}

if(isset ($_POST[‘earlymusic’])) {
$earlymusic = 1;
} else {
$earlymusic = 0;

}

if(isset ($_POST[‘jazz’])) {
$jazz = 1;
} else {
$jazz = 0;

}

if(isset ($_POST[‘keyboards’])) {
$keyboards = 1;
} else {
$keyboards = 0;

}

if(isset ($_POST[‘percussion’])) {
$percussion = 1;
} else {
$percussion = 0;

}

if(isset ($_POST[‘rockandpop’])) {
$rockandpop = 1;
} else {
$rockandpop = 0;

}

if(isset ($_POST[‘strings’])) {
$strings = 1;
} else {
$strings = 0;

}

if(isset ($_POST[‘traditional’])) {
$traditional = 1;
} else {
$traditional = 0;

}

if(isset ($_POST[‘vocal’])) {
$vocal = 1;
} else {
$vocal = 0;

}

if(isset ($_POST[‘woodwind’])) {
$woodwind = 1;
} else {
$woodwind = 0;

}

if(isset ($_POST[‘world’])) {
$world = 1;
} else {
$world = 0;

}

$website = $_POST[‘link’];
$description = $_POST[“description”];

$query = “INSERT INTO musicianstb(name, brass, earlymusic, jazz, keyboards, percussion, rockandpop, strings,

traditional, vocal, woodwind, world, website, description, imagename)

VALUES(‘$name’,’$brass’,’$earlymusic’,’$jazz’,’$keyboards’,’$percussion’,’$rockandpop’,’$strings’,’$traditional’,’$vocal’,’$woodwind’,

‘$world’,’$website’,’$description’,’$imagename’)”;
$result = mysql_query($query);

if (!$result) {
die(‘Invalid query: ‘ . mysql_error());
}

echo “New Musician added”;
echo “</br>”;
echo “</br>”;
echo “<br>Return to admin page <a href=http://www.superact.org.uk/admin

target=_self><b>http://www.superact.org.uk/admin</b></a>”;
echo “</br>”;

mysql_close($db);

} else {
echo ‘<p><b>You have accessed this page inappropriately!</b></p>’;
}

} else {

echo ‘<p><b>You have accessed this page inappropriately!</b></p>’;
}

?>

[/code]

Any help much appreciated

Paul

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@chuckylefrekauthorSep 20.2006 — Ahh I have sussed the problem after going through it line by line

I had not included

enctype="multipart/form-data"

Seems to work now - sorry for wasting anyones time

Paul
×

Success!

Help @chuckylefrek 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 4.30,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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