Jump to content


Photo

Creating a mini admin section PHP / SQL


  • Please log in to reply
7 replies to this topic

#1 seandon01

seandon01
  • Members
  • PipPip
  • Member
  • 11 posts
  • LocationSan Francisco

Posted 18 June 2006 - 01:57 AM

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.
<? 
$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\">"; ?>


#2 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 18 June 2006 - 02:09 AM

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.
[table]



Don't worry, the printer fairies will sort it out.

#3 seandon01

seandon01
  • Members
  • PipPip
  • Member
  • 11 posts
  • LocationSan Francisco

Posted 18 June 2006 - 02:13 AM

[!--quoteo(post=385198:date=Jun 17 2006, 07:09 PM:name=Fyorl)--][div class=\'quotetop\']QUOTE(Fyorl @ Jun 17 2006, 07:09 PM) View Post[/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

#4 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 18 June 2006 - 02:20 AM

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.
[table]



Don't worry, the printer fairies will sort it out.

#5 seandon01

seandon01
  • Members
  • PipPip
  • Member
  • 11 posts
  • LocationSan Francisco

Posted 18 June 2006 - 02:26 AM

[!--quoteo(post=385202:date=Jun 17 2006, 07:20 PM:name=Fyorl)--][div class=\'quotetop\']QUOTE(Fyorl @ Jun 17 2006, 07:20 PM) View Post[/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)

#6 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 18 June 2006 - 02:44 AM

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:
$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);
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.
[table]



Don't worry, the printer fairies will sort it out.

#7 seandon01

seandon01
  • Members
  • PipPip
  • Member
  • 11 posts
  • LocationSan Francisco

Posted 18 June 2006 - 03:04 AM

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!!!!!!!!!

#8 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 18 June 2006 - 03:08 AM

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
[table]



Don't worry, the printer fairies will sort it out.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users