Forum Moderators: coopster

Message Too Old, No Replies

upload image, won't save path to db?

         

generic

6:17 am on Nov 4, 2005 (gmt 0)

10+ Year Member



I have a script that accepts an uploaded image and saves it to a dir, then "should" put the path/filename into a db along with some other details from the form.. PHP essentially looks like this:

$target_path = "/var/www/html/artists/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

$image_path = $_FILES['uploadedfile']['name'];

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded<br>\n";
}

if($submit)
{//begin of if($submit).
// Set global variables
$bandname = $_POST['bandname'];
$member1 = $_POST['member1'];
$member2 = $_POST['member2'];
$member3 = $_POST['member3'];
$member4 = $_POST['member4'];
$member5 = $_POST['member5'];
$member6 = $_POST['member6'];
$member7 = $_POST['member7'];
$member8 = $_POST['member8'];
$description = $_POST['description'];
$website_url = $_POST['website_url'];
$image_path = $_POST['target_path'];

//add automatic line breaks
$description = ereg_replace(13,"<p>",$description);

//run the query which adds the data gathered from the form into the database
$result = mysql_query("INSERT INTO artists (bandname, dtime, member1, member2, member3, member4, member5, member6, member7, member8, description, website_url, image_path)
VALUES ('$bandname',NOW(),'$member1','$member2','$member3','$member4','$member5','$member6','$member7','$member8','$description','$website_url','$target_path')",$connect);
//print success message.
echo "<b>Artist added successfully!<br>You'll be redirected to the control panel after (4) seconds, or click <a href=\"index.php\">click here</a> to go there now.\n";
echo "<meta http-equiv=Refresh content=4;url=index.php>";
}//end of if($submit)

Can anyone tell me why the image uploads, and it saves the path to the db, but without the filename attached? I've been staring at this for too long :(

Thanks in advance for the extra eyes,

gen

Steerpike

6:27 am on Nov 4, 2005 (gmt 0)

10+ Year Member



because:
$image_path = $_POST['target_path'];

doesn't actually give you a value.

The value you want is:

$image_path = $_FILES['uploadedfile']['name'];

(which you actually have up the top, but you're overwriting it in the submit check)

You could also make it:

$image_path = $target_path;

and submit that and that should give you the entire path to the file, although I personally think it's best just to hold the filename in the database, not the entire path and the entire path may change and then you have to go and change every entry in your database.

Steerpike

6:31 am on Nov 4, 2005 (gmt 0)

10+ Year Member



Hmmm, actually, after re-reading your code I may be wrong.
I'd put some echo statements in there to see what is actually being set. Are you sure a file is uploading successfully? It's possible that the upload is failing and therefore you don't have anything to submit.

generic

6:39 am on Nov 4, 2005 (gmt 0)

10+ Year Member



Ok, thanks for the reply. I'm pretty new to this stuff really so bear with me.

I tried switching to:

$image_path = $_FILES['uploadedfile']['name'];

and it didn't work? At this point, if it's better to use straight filename rather than full path, that's how I'd rather do it anyway.

generic

6:41 am on Nov 4, 2005 (gmt 0)

10+ Year Member



also, I'm actually manually checking for the new file in FTP each time so I know it's uploading the file ok.

dmmh

6:51 am on Nov 4, 2005 (gmt 0)

10+ Year Member



do you even have a hidden field named 'target_path' in the file upload form?
I dont think so, wouldnt be wise anyway and I think this is why it fails

dmmh

6:53 am on Nov 4, 2005 (gmt 0)

10+ Year Member



also, do a mysql_real_escape_string($_POST['element']) before you add anything into the DB.....

$image_path=mysql_real_escape_string($_POST['target_path']);

generic

6:56 am on Nov 4, 2005 (gmt 0)

10+ Year Member



Ok, revised the code a bit. It's entering hard-code values into the db like:

$image_entry = "hello";

but nothing as a variable oddly enough?

$target_path = "/var/www/html/artists/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

$image_path = $_FILES['uploadedfile']['name'];

$image_name = $target_path . ($_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded<br>\n";
}

if($submit)
{//begin of if($submit).
// Set global variables
$bandname = $_POST['bandname'];
$member1 = $_POST['member1'];
$member2 = $_POST['member2'];
$member3 = $_POST['member3'];
$member4 = $_POST['member4'];
$member5 = $_POST['member5'];
$member6 = $_POST['member6'];
$member7 = $_POST['member7'];
$member8 = $_POST['member8'];
$description = $_POST['description'];
$website_url = $_POST['website_url'];
$image_entry = $_FILES['uploadedfile']['name'];

//add automatic line breaks
$description = ereg_replace(13,"<p>",$description);

//run the query which adds the data gathered from the form into the database
$result = mysql_query("INSERT INTO artists (bandname, dtime, member1, member2, member3, member4, member5, member6, member7, member8, description, website_url, image_path)
VALUES ('$bandname',NOW(),'$member1','$member2','$member3','$member4','$member5','$member6','$member7','$member8','$description','$website_url','$image_entry')",$connect);
//print success message.
echo "<b>Artist added successfully!<br>You'll be redirected to the control panel after (4) seconds, or click <a href=\"index.php\">click here</a> to go there now.\n";
echo "<meta http-equiv=Refresh content=4;url=index.php>";//sucks, I know
}

dmmh

6:58 pm on Nov 4, 2005 (gmt 0)

10+ Year Member



try this:

if($submit){
//begin of if($submit).
// Set global variables
$bandname = $_POST['bandname'];
$member1 = $_POST['member1'];
$member2 = $_POST['member2'];
$member3 = $_POST['member3'];
$member4 = $_POST['member4'];
$member5 = $_POST['member5'];
$member6 = $_POST['member6'];
$member7 = $_POST['member7'];
$member8 = $_POST['member8'];
$description = $_POST['description'];
$website_url = $_POST['website_url'];

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded<br>\n";
$image_entry = $_FILES['uploadedfile']['name'];
}}

just a hunch though. also, does it actually echo the filename when you inside the second loop above?

generic

7:35 pm on Nov 4, 2005 (gmt 0)

10+ Year Member



Thanks for the reply.

Ok, first off, yes, it was echoing the filename, just not entering it. Secondly,I tried the code revision and it didn't work. No upload, no echoing the filename.

I'm sure it's close I just don't know what is going on here....

dmmh

8:37 pm on Nov 4, 2005 (gmt 0)

10+ Year Member



then change:

if($submit){

to:

if(isset($_POST['submit'])){

ofcourse, this asumes you have a submit button like:

<input type="submit" name="submit" value="do it!">

where did you get the variable $submit from? was it initialised in a part of the script you didnt show us like $submit = $_POST['submit']? If it wasnt, thats the flaw right there (most likely)

generic

9:49 pm on Nov 4, 2005 (gmt 0)

10+ Year Member



I'm so confused - I should stick to graphic design ;)

Ok, in all it's glory, I'll present everything between the body tags - may be relevant - probably is:

===================================

<body>
<?php

$target_path = "/var/www/html/artists/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

$image_path = $_FILES['uploadedfile']['name'];
$image_path = mysql_real_escape_string($_POST['target_path']);

$image_name = $target_path . ($_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded<br>\n";
}

if($submit)
{//begin of if($submit).
// Set global variables
$bandname = $_POST['bandname'];
$member1 = $_POST['member1'];
$member2 = $_POST['member2'];
$member3 = $_POST['member3'];
$member4 = $_POST['member4'];
$member5 = $_POST['member5'];
$member6 = $_POST['member6'];
$member7 = $_POST['member7'];
$member8 = $_POST['member8'];
$description = $_POST['description'];
$website_url = $_POST['website_url'];
$image_entry = $_FILES['uploadedfile']['name'];

//add automatic line breaks
$description = ereg_replace(13,"<p>",$description);

//run the query which adds the data gathered from the form into the database
$result = mysql_query("INSERT INTO artists (bandname, dtime, member1, member2, member3, member4, member5, member6, member7, member8, description, website_url, image_path)
VALUES ('$bandname',NOW(),'$member1','$member2','$member3','$member4','$member5','$member6','$member7','$member8','$description','$website_url','$image_entry')",$connect);
//print success message.
echo "<b>Artist added successfully!<br>You'll be redirected to the control panel after (4) seconds, or click <a href=\"index.php\">click here</a> to go there now.\n";
echo "<meta http-equiv=Refresh content=4;url=index.php>";
}//end of if($submit).

// If the form has not been submitted, display it.
else
{//begin of else

?>

<br>
<h3>::Add Artist </h3>
<form method="post" action="<?php echo $PHP_SELF?>" enctype="multipart/form-data">
<p>Band Name:<br>
<input name="bandname" size="40" maxlength="255">
<br>
<br>
<select name="memberCount" onchange="javascript:showMembers(this.selectedIndex); this.selectedIndex = 0;">
<option>Members in band</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
</select>
<br>
<br>
<input type="text" id="member1" name="member1" style="visibility:hidden;">
<br>
<input type="text" id="member2" name="member2" style="visibility:hidden;">
<br>
<input type="text" id="member3" name="member3" style="visibility:hidden;">
<br>
<input type="text" id="member4" name="member4" style="visibility:hidden;">
<br>
<input type="text" id="member5" name="member5" style="visibility:hidden;">
<br>
<input type="text" id="member6" name="member6" style="visibility:hidden;">
<br>
<input type="text" id="member7" name="member7" style="visibility:hidden;">
<br>
<input type="text" id="member8" name="member8" style="visibility:hidden;">
<br>
<br>
Description:<br>
<textarea name="description" rows="7" cols="30"></textarea>
<br>
<br>
Website URL: (with [)...] <br>
<input name="website_url" value="http://" size="40" maxlength="255">
<br>
<br>
<input type="submit" name="submit" value="Add Artist >>">
<br>
<a href="index.php"><-- Go Back</a> </p>
</p>
</form>
<?php
}//end of else
?>
</body>

=============================

Does this shed any light? Also, I really appreciate your time and effort here. I'm trying really hard to learn more. Thanks a mill.

gen

dmmh

3:12 pm on Nov 5, 2005 (gmt 0)

10+ Year Member



<body>
<?
/*
it makes more sense (coding/ thinking process wise) to first put the code that will be executed if no form was submitted yet at the top of the script,
as this is the default state of the script/ page, if you know what I mean. So......*/
if (!isset($_POST['submit'])){?>
<br>
<h3>::Add Artist </h3>
<form method="post" action="<? echo $PHP_SELF?>" enctype="multipart/form-data">
<p>Band Name:<br>
<input name="bandname" size="40" maxlength="255">
<?
//quick question, where is the actual file upload input field? I dont see anything like <input type="file" name="uploadedfile">.....
//I think this is what is missing :p?>
<br>
<br>
<select name="memberCount" onchange="javascript:showMembers(this.selectedIndex); this.selectedIndex = 0;">
<option>Members in band</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
</select>
<br>
<br>
<input type="text" id="member1" name="member1" style="visibility:hidden;">
<br>
<input type="text" id="member2" name="member2" style="visibility:hidden;">
<br>
<input type="text" id="member3" name="member3" style="visibility:hidden;">
<br>
<input type="text" id="member4" name="member4" style="visibility:hidden;">
<br>
<input type="text" id="member5" name="member5" style="visibility:hidden;">
<br>
<input type="text" id="member6" name="member6" style="visibility:hidden;">
<br>
<input type="text" id="member7" name="member7" style="visibility:hidden;">
<br>
<input type="text" id="member8" name="member8" style="visibility:hidden;">
<br>
<br>
Description:<br>
<textarea name="description" rows="7" cols="30"></textarea>
<br>
<br>
Website URL: (with [)...] <br>
<input name="website_url" value="http://" size="40" maxlength="255">
<br>
<br>
<input type="submit" name="submit" value="Add Artist >>">
<br>
<a href="index.php"><-- Go Back</a> </p>
</p>
</form>
<?php
}//end of else

if(isset($_POST['submit'])){
// begin of if($submit)! YOU NEVER INITIALISED $submit anywhere, so this might be a reason why nothing is inserted into the database, the script doesnt know what $submit is, so you need to change it to $_POST['submit'], which is what I did. If it works ok, do you have register_globals on? Still, change the code to reflect like I did

$target_path = "/var/www/html/artists/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

$image_path = $_FILES['uploadedfile']['name'];
//$image_path = mysql_real_escape_string($_POST['target_path']); this will always be empty, might as well remove it

$image_name = $target_path . ($_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)){
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded<br>\n";
$image_entry = mysql_real_escape_string($_FILES['uploadedfile']['name']);
}
// Set global variables
$bandname = mysql_real_escape_string($_POST['bandname']);
$member1 = mysql_real_escape_string($_POST['member1']);
$member2 = mysql_real_escape_string($_POST['member2']);
$member3 = mysql_real_escape_string($_POST['member3']);
$member4 = mysql_real_escape_string($_POST['member4']);
$member5 = mysql_real_escape_string($_POST['member5']);
$member6 = mysql_real_escape_string($_POST['member6']);
$member7 = mysql_real_escape_string($_POST['member7']);
$member8 = mysql_real_escape_string($_POST['member8']);
$description = mysql_real_escape_string($_POST['description']);
$website_url = mysql_real_escape_string($_POST['website_url']);

//add automatic line breaks
$description = ereg_replace(13,"<p>",$description);

//run the query which adds the data gathered from the form into the database
$result = mysql_query("INSERT INTO artists (bandname, dtime, member1, member2, member3, member4, member5, member6, member7, member8, description, website_url, image_path)
VALUES ('$bandname',NOW(),'$member1','$member2','$member3','$member4','$member5','$member6','$member7','$member8','$description','$website_url','$image_entry')",$connect);
//print success message.
echo "<b>Artist added successfully!<br>You'll be redirected to the control panel after (4) seconds, or click <a href=\"index.php\">click here</a> to go there now.\n";
echo "<meta http-equiv=Refresh content=4;url=index.php>";
}//end of if($isset($_POST['submit']))
?>
</body>

generic

7:27 pm on Nov 5, 2005 (gmt 0)

10+ Year Member



Hmm.. No luck with the images still. Did I not mention that I had a file upload form before this script?

page 1 - user clicks "add new band"
page 2 - user uses file upload and uploads file
page 3 - user adds details (as per this script)

In order to proceed with this script as step 3, the user has to go through step 2 (which is a basic upload form)

----uploadimages.php (step 2)----
<form enctype="multipart/form-data" action="add_artist.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

Step 3, the problem script above, is where I'm stuck.

generic

7:34 pm on Nov 5, 2005 (gmt 0)

10+ Year Member



Hmm.. No luck with the images still. Did I not mention that I had a file upload form before this script?

page 1 - user clicks "add new band"
page 2 - user uses file upload and uploads file
page 3 - user adds details (as per this script)

In order to proceed with this script as step 3, the user has to go through step 2 (which is a basic upload form)

----uploadimages.php (step 2)----
<form enctype="multipart/form-data" action="add_artist.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

Step 3, the problem script above, is where I'm stuck. It was actually working fine, except for not saving the image name in the db, that's all. It would save hard-coded values, just no variables ie:

//global variables
...
$image_entry = $_FILES['uploadedfile']['name']; //doesn't work

$image_entry = "string will save to db"; //does work

It's kinda weird.

dmmh

5:34 pm on Nov 6, 2005 (gmt 0)

10+ Year Member



<form enctype="multipart/form-data" action="add_artist.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" name="submit" value="Upload File" />
</form>

you forgot the name of the submit, very important!

see if it helps

dmmh

6:17 pm on Nov 6, 2005 (gmt 0)

10+ Year Member



think I found it

page 1 - user clicks "add new band"
page 2 - user uses file upload and uploads file
page 3 - user adds details (as per this script)

the third script doesnt have enough 'if' statements to cover for all scenario's, basically.

You need to check if a file upload was done first and if it was succesfull, display the band details submission form.
Then, if this form was submitted, insert it into the database. The way you have set up the form right now, the filename/ path will never ever be inserted into the database, because the $_FILES array is empty when the band members details are submitted (the $_FILES array was processed already in another loop and will be empty in the other one)

See what I mean? You could do 2 things to fix this:
Make page 2 for entering band members details AND include the file upload form, which will allow you to insert all details into the database at once, or change your database, which will be far more versatile and is by farthe best option. More work now, less in the future ;-)

I will explain a bit further.
Currently you have a table for everytrhing band related, but you only store band names and do not asign a unique id to each band. What happens if you have to rename something? Or if there are bands with duplicate names? You will never be able to select their details, because there will be duplicate results in the queries

I would make 2 tables for this, at least:

TABLE BANDS
----------
FIELDS:

bid (INT, 6, autoincrement, PRIMARY)// band id
band_name (VARCHAR, 80) //name of band
band_url (VARCHAR, 200) // url of website
band_description (VARCHAR, 200) // description of band
band_img (VARCHAR, 200) // image of band perhaps here, best in seperate table though

TABLE BAND_MEMBERS
-------------------
bid (INT, 6, PRIMARY)
bmid (INT, 6, autoincrement, INDEX) //band member id
band_member_name (VARCHAR, 100) //name of band member)

it is always, well almost always, good practice to make a seperate table for stuff that is bound to appear in tables more then once, like band members names (a band member can be in multiple bands cant they?)
but they will have only have one website (most likely), one description and one name. They could have multiple images though, so you could make a seprate table for this too

the second table contains the id of the band, so you can easily look up in which bands the members plays by comparing the 2 id's

if you change your database to reflect my suggestions, which I and I think everyone else here would recommend, you will also be able to update a field more easily.

this will have impact on the way your script/ forms are set up

I would either make it a 2 page submission process so you can leave your DB the way it is setup now, or change it to this below, which would require youto change the DB to what I suggested :

page 1: user clicks 'add band'
page 2: first you query for the maximum bid (band id) currently in the DB from the 'BANDS' table and increment this with 1, asign it to a variable like $max_bid, user enters band members details and if submitted, their details will be entered into the second AND the first table (2 queries, 1 for the bands common details, the second for the the members, each member will have is own unique id in the second table (BAND_MEMBERS)). Carry the $max_bid value to the third page via a hidden input field named say 'bid' <input type="hidden" value="<? echo $max_bid;?>" name="bid">
page 3: we now know the band's id via the hidden input field, so we can put our file upload form here instead of before on page 2. Then, instead of, using a INSERT query, you can use a UPDATE query (UPDATE BANDS SET band_img="'.mysql_real_escape_string($_FILES['uploadedfile']['name'])."' WHERE bid = "'.mysql_real_escape_string($_POST['bid']).'"

either use a UPDATE query like above, or better yet, make a seperate table to store a bands image names or urls :)

any questions, just ask me :)

dmmh

10:07 pm on Nov 7, 2005 (gmt 0)

10+ Year Member



discovered a minor error

TABLE BAND_MEMBERS
-------------------
bmid (INT, 6, autoincrement) //band member id
bid (INT, 6)
band_member_name (VARCHAR, 100) //name of band member)

generic

3:00 am on Nov 8, 2005 (gmt 0)

10+ Year Member



Hi. I fixed the name problem in the form and it fixed the upload issue (I can't believe I missed that...) Generally, I'm seeing a pile of bad code so I think I'm going to try a total rewrite from scratch - too many leftover strings with no use.

In any event, I'm dropping the band members now too so it should simplify the script a bit. I'm going to use this thread as a reference and post whatever I come up with as soon as it arrives (shortly I hope). Thanks for your help - I'll post again asap.

generic

4:06 am on Nov 8, 2005 (gmt 0)

10+ Year Member



Wow, *ashamed* I forgot about file permissions... tsk tsk. Anyway, I'm cooking with napalm now thanks to your code and a few slight edits:

add_artist.php
===============================
<body>
<?

if (!isset($_POST['submit'])){?>
<br>
<h3>::Add Artist </h3>
<form method="post" action="<? echo $PHP_SELF?>" enctype="multipart/form-data">
<p>Band Name:<br>
<input name="bandname" size="40" maxlength="255">
<br>
<br>
Choose a file to upload:<br>
<input name="uploadedfile" type="file" />
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<br>
<br>
Description:<br>
<textarea name="description" rows="7" cols="30"></textarea>
<br>
<br>
Website URL: (with [)...] <br>
<input name="website_url" value="http://" size="40" maxlength="255">
<br>
<br>
<input type="submit" name="submit" value="Add Artist >>">
<br>
<a href="index.php"><-- Go Back</a> </p>
</p>
</form>
<?php
}//end of else
if(isset($_POST['submit'])){ // begin of if($submit)

$target_path = "/var/www/html/artists/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)){
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded<br>\n";
$image_entry = mysql_real_escape_string($_FILES['uploadedfile']['name']);
}

// Set global variables
$bandname = mysql_real_escape_string($_POST['bandname']);
$description = mysql_real_escape_string($_POST['description']);
$website_url = mysql_real_escape_string($_POST['website_url']);

//add automatic line breaks
$description = ereg_replace(13,"<p>",$description);

//run the query which adds the data gathered from the form into the database
$result = mysql_query("INSERT INTO artists (bandname, dtime, description, website_url, image_path)
VALUES ('$bandname',NOW(),'$description','$website_url','$image_entry')",$connect);
//print success message.
echo "<b>Artist added successfully!<br>You'll be redirected to the control panel after (4) seconds, or click <a href=\"index.php\">click here</a> to go there now.\n";
echo "<meta http-equiv=Refresh content=4;url=index.php>";
}//end of if($isset($_POST['submit']))
?>
</body>
=================

And it works, thanks for your help. If you're interested in following the thread a bit more, I could really use some advice on letting the user edit the image (as in, insert a new image in place of the old one)...

I have an edit_artist.php that allows me to edit the form fields. Would it be best to have an upload form field on the edit page itself or should I link the user to a seperate edit_image.php type of script?

Thanks for the feedback.

dmmh

8:17 pm on Nov 9, 2005 (gmt 0)

10+ Year Member



do you want the users to be able to edit the field? Isnt that kinda dangerous? who says they wont upload tubgirl or some nice goatse? hehe
I would do it on the same page and simply deny access to the rest of the form's field, based on the user's access level (admin, mod, member etc etc)

generic

9:57 pm on Nov 9, 2005 (gmt 0)

10+ Year Member



Well in truth, there will be only user who will be using the add/edit functions of this site, so I'm not worried about that. Frankly, there just isn't enough traffic to worry about user levels, besides, the only person signing is is the single admin user for the site.

dmmh

10:21 pm on Nov 9, 2005 (gmt 0)

10+ Year Member



ah, then Id just make it in the same form :)

generic

11:12 pm on Nov 9, 2005 (gmt 0)

10+ Year Member



At the risk of sounding ungrateful, any chance you might want to peek at the edit code and troubleshoot it with me?

Either way, I'm learning exponentially here and I really appreciate it. (although the reference to 'tubgirl' I could have lived without ;)

Lemme know and if you're up to it, I'll post up the edit code.

Thanks!

dmmh

7:09 am on Nov 10, 2005 (gmt 0)

10+ Year Member



Ill look into it later ok, gtg 2 work :(

generic

7:28 am on Nov 10, 2005 (gmt 0)

10+ Year Member



Cool, thanks for keeping the help flowing. And really, take your time, I just appreciate the excellent help at all :)

Here's what I have for the edit part of the app. It's all in one form and it works great, the problem being that I don't know how to make it update the image _ONLY_ if the file field is populated in the form. Otherwise, it should assume no changes are required and use the current image entry in the db. I've tried all the obvious...

<input type="file" value="<?php $image_path;?>

...type of situations but nothing has worked. It's not necessary to the script, but it'd be better than having to suck the bandwidth to reupload a picture for every little edit. Let's face it, bands like to switch members... a lot. Anyway, yeah.. here's the code to edit a band.

edit_artist.php
=============================
<body>
<?php
include("../config.php");

if($submit)
{

$target_path = "/var/www/html/artists/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)){
echo "The file <strong>". basename( $_FILES['uploadedfile']['name']). "</strong> has been uploaded<br><br>\n";
chmod("$target_path", 0777);
$image_entry = mysql_real_escape_string($_FILES['uploadedfile']['name']);
}

$bandname = mysql_real_escape_string($_POST['bandname']);
$description = mysql_real_escape_string($_POST['description']);
$website_url = mysql_real_escape_string($_POST['website_url']);

//add automatic line breaks
$description = ereg_replace(13,"<p>",$description);

//get ride of slashes
$description = stripslashes($description);
$bandname = stripslashes($bandname);

$result = mysql_query("UPDATE artists SET bandname='$bandname', description='$description', website_url='$website_url', image_path='$image_entry' WHERE artistid='$artistid' ",$connect);

echo "<b>Artist updated successfully!<br>You'll be redirected to the control panel after (4) seconds, or click <a href=\"index.php\">click here</a> to go there now.\n";
echo "<meta http-equiv=Refresh content=4;url=index.php>";

} elseif($artistid) {

$result = mysql_query("SELECT * FROM artists WHERE artistid='$artistid' ",$connect);
while($myrow = mysql_fetch_assoc($result))
{
$bandname = $myrow["bandname"];
$description = $myrow["description"];
$website_url = $myrow["website_url"];
$image_path = $myrow["image_path"];
?>
<br>
<h3>::Edit Artist</h3>
<form enctype="multipart/form-data" action="<?php echo $PHP_SELF?>" method="POST">
<p>Please be careful not to delete any line break or paragraph break tags shown as <strong>&lt;p&gt;</strong> or <strong>&lt;br&gt;</strong>.<br>
These are paragraph formatting tags that have been dynamically generated by this software.</p>
<p>
<input type="hidden" name="artistid" value="<? echo $myrow['artistid']?>">

<p>Band Name:<br>
<input name="bandname" size="40" maxlength="255" value="<?php echo $bandname;?>">
<br>
<br>
<p>Artist Image:<br>
<br>
<img src="../../artists/<?php echo $image_path;?>" title="<?php echo $bandname;?>">
<br>
<br>
Image File:<br />
<input name="uploadedfile" type="file" value="<?php echo $myrow['$image_path'];?>">/ / WHAT THE HELL?
<br>
<br>
Description:<br>
<textarea name="description" rows="7" cols="30"><?php echo $description;?></textarea>
<br>
<br>
Website URL: (with [)...] <br>
<input name="website_url" size="40" maxlength="255" value="<?php echo $website_url;?>">
<br>
<br>
<input type="submit" name="submit" value="Edit Artist >>">
<br>
<a href="index.php"><-- Go Back</a> </p>
</p>
</form>
<?
}//end of while loop

}//end else
?>

===========================

You'll notice alot of the same old cruddy code, but it works for this purpose . . . except for the image upload part... what do people do for this? It seems to be a big issue.

Thanks.

dmmh

6:01 pm on Nov 12, 2005 (gmt 0)

10+ Year Member



try this:

in the form:

<input type="file" name="uploadedfile">

then in the processing part of the script, do it like this:

if (!empty($_FILES['uploadedfile'])){
//file upload processing here, only runs if someone chose another img
now we need to check if a file already exists for the band

$target_path = "/var/www/html/artists/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if (file_exists($target_path) and!is_dir($target_path)){
//if file exists and file is not a directory...
unlink($target_path);
}

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)){
echo "The file <strong>". basename( $_FILES['uploadedfile']['name']). "</strong> has been uploaded<br><br>\n";
chmod("$target_path", 0777);
$image_entry = mysql_real_escape_string($_FILES['uploadedfile']['name']);
}
}

:)

generic

9:17 pm on Nov 12, 2005 (gmt 0)

10+ Year Member



Hmm, no luck. It still submits the changes + new image, but it's not 'disregarding' an empty field - it's entering the contents (as in, nothing) into the db, overwriting any current data in the process. :(

dmmh

10:08 am on Nov 13, 2005 (gmt 0)

10+ Year Member



stupid, sorry :)

change

if (!empty($_FILES['uploadedfile'])){

to

if (!empty($_FILES['uploadedfile']['name'])){

dmmh

10:12 am on Nov 13, 2005 (gmt 0)

10+ Year Member



also, you are updating the image_entry field in the query, regardless if there was a new image submitted or not

if there is no image submitted, you shouldnt update that field.

so you should put a seperate query inside the code I posted above, the one that executes only if a new image was submitted, to update the field.

and a seperate one to edit the bands other details, outside that loop

;)

This 35 message thread spans 2 pages: 35