Jump to content

Archived

This topic is now archived and is closed to further replies.

seandon01

Creating a mini admin section PHP / SQL

Recommended Posts

i am creating a section that allows the user to add / delete / modify records in the database.. so far ive worked out the add and delete pages, but the modify page is giving me nothing but trouble. so the modify page has a form with fields that are populated with the current data by the record chosen (this works fine).
what i dont know how to do is - suppose that they only want to modify the text field, and not change any of the files, how to only insert that data, or if they only want to update one of the files, or both of the files.. I hope this makes sense.. Thank you so much in advance.. any advice is helpful.
[code]
<?
$modify_artist=mysql_query("SELECT * FROM artists WHERE id = $_GET[artist_id]");
   $modify_results = mysql_fetch_array($modify_artist); ?>
<form action="action_modify.php" method="post" enctype="multipart/form-data" name="modify_record" id="modify_record">
<input name="action" type="hidden" value="modify" />
<input name="artist_id" type="hidden" value="<? echo $modify_results[id]; ?>" />

  <p>
    Artist Name
    <input name="artistname" type="text" id="artistname" value="<? echo $modify_results[artist_name]; ?>" />
</p>
  <p>Artist Bio </p>
  <p>
    <textarea name="artistbio" cols="50" rows="10" id="artistbio"><? echo $modify_results[artist_bio]; ?>
</textarea>
  </p>
  <p>
    Artist Image
    <input name="artistimage" type="file" id="artistimage" />
  </p>
  <p>
    Track Name
    <input name="trackname" type="text" id="trackname" value="<? echo $modify_results[track_name] ?>" />
    Track File (mp3 format)
    <input name="mp3_file" type="file" id="mp3_file" />
  </p>
  <p>Burn Lounge Link
    <input name="burnlink" type="text" id="burnlink" value="<? echo $modify_results[burn_link]; ?>" />
  </p>
  <p>
    Official Site Link
    <input name="officiallink" type="text" id="officiallink" value="<? echo $modify_results[official_link]; ?>" />
  </p>
  <p>
    Myspace Link
    <input name="myspacelink" type="text" id="myspacelink" value="<? echo $modify_results[official_link]; ?>" />
  </p>
  <p>
    <input type="submit" name="Submit" value="Submit" />
  </p>
</form>
Current Image: <br />
<? echo "<img src=\"../../images/artists/$modify_results[artist_image].jpg\">"; ?>[/code]

Share this post


Link to post
Share on other sites
The short answer is: don't. But I'll explain. Basically, if you have filled out the data already with the values currently in the database. Then when you run an UPDATE query on all the form data, only the ones that the user actually changed will be updated, the rest will stay the same because they weren't changed and so are just exactly what the database had already.

Share this post


Link to post
Share on other sites
[!--quoteo(post=385198:date=Jun 17 2006, 07:09 PM:name=Fyorl)--][div class=\'quotetop\']QUOTE(Fyorl @ Jun 17 2006, 07:09 PM) [snapback]385198[/snapback][/div][div class=\'quotemain\'][!--quotec--]
The short answer is: don't. But I'll explain. Basically, if you have filled out the data already with the values currently in the database. Then when you run an UPDATE query on all the form data, only the ones that the user actually changed will be updated, the rest will stay the same because they weren't changed and so are just exactly what the database had already.
[/quote]
Thanks for the quick reply... what you said makes perfect sense, but if you notice there are two file fields in my form, those are not auto populated with the current file, so therefore when updating, it enters 0 value doesnt it? Thanks again.. this forum is amazing

Share this post


Link to post
Share on other sites
Files can be accessed using the $_FILES variable. All you need to do is check whether $_FILES['artistimage'] exists or not and if it does, upload the file (I assume that's what you want or you wouldn't use the 'file' type) and add it to the update query. I don't think file info can be accessed with $_POST which was probably what was givin you the headache.

Share this post


Link to post
Share on other sites
[!--quoteo(post=385202:date=Jun 17 2006, 07:20 PM:name=Fyorl)--][div class=\'quotetop\']QUOTE(Fyorl @ Jun 17 2006, 07:20 PM) [snapback]385202[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Files can be accessed using the $_FILES variable. All you need to do is check whether $_FILES['artistimage'] exists or not and if it does, upload the file (I assume that's what you want or you wouldn't use the 'file' type) and add it to the update query. I don't think file info can be accessed with $_POST which was probably what was givin you the headache.
[/quote]
Thats exactly what i am having trouble with.. checking if only the artistimage file is set, then adding that to the update query, checking if only the mp3_file is set and adding that to the query, and checking if both are set and adding that to the query.. i would think to use IF statements to do this.. but im having trouble with the syntax.. (as you can tell im failry new with php)

Share this post


Link to post
Share on other sites
What do you want to add to the database? If you want the actual files to be uploaded then all you really need to do with the database is update the path to the uploaded file. Something like this maybe:
[code]$uploaddir = '/home/servername/public_html/uploads/';
$query = "
UPDATE `table`
SET
`blah1`='$variable',
`field_from_postdata`='$value'
";
if(isset($_FILES['artistimage']))
{
// Do any format and/or size checking here
$fname = $uploaddir . basename($_FILES['artistimage']['name']);
if(move_uploaded_file($_FILES['artistimage']['tmp_name'], $fname))
{
// Upload succeeded
$query .= ", `artistimage`='$fname'";
}
else
{
// Upload failed
}
}

if(isset($_FILES['mp3_file']))
{
// Do the same as artistimage
}

$query .= "
WHERE `user_id`='$id'
";
mysql_query($query);[/code]Basically just assemble a query updating all the form data except the files. Then check whether each file exists and if so upload it and stick an extra update on the end of the query. That way it will work if none, 1 or both are present.

Share this post


Link to post
Share on other sites
Thank you for taking the time to help me.. i really appreciate it.. I'll give this a shot and post the results.. Thanks again!!!!!!!!!

Share this post


Link to post
Share on other sites
Glad I could help. I'm going to bed now cos it's 4AM so PM or email me if you get stuck and I'll reply when I get up sometime in the afternoon

Share this post


Link to post
Share on other sites

×

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.