Forum Moderators: coopster
$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
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.
$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
}
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?
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)
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
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>
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.
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.
you forgot the name of the submit, very important!
see if it helps
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 :)
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.
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.
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!
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><p></strong> or <strong><br></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.
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']);
}
}
:)
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
;)