I'm building a mock website to teach myself PHP.
Right now I'm trying to learn how to upload a pdf file to my db, display it on webpage, delete it, and rename it. I've gotten as far as renaming the file, but am stuck with the logic and how to actually do it.
I basically have a Browse & Upload button at the top of my page. After uploading, it displays the pdf file name below in a URL fashion, which I can click and download. Then to the right I have the two buttons (Delete/Rename).
What I would like to do is click the RENAME button and the URL is converted to a <input type=text box> with the name of the pdf file inside, while at the same time a new SAVE button being presented to right of the two buttons. Then I would like to rename the pdf file and click the SAVE button and then have the SAVE button go away/hide/disappear.
If making the SAVE button appear and disappear is overly complicated, I wouldn't mind it being there, but maybe subdued until the RENAME button is clicked and it's needed.
I know it can be done, I just don't have a clue how to make it work.
Would be grateful if anyone could help.
Here is the code I have that works for everything except the RENAME:
Code: Select all
<?php
// ----------------------------------------------------------------------------------------------------------
// Connect to server then db and return error if not connected.
// ----------------------------------------------------------------------------------------------------------
include ("../_includes/dbconnect.php");
// ----------------------------------------------------------------------------------------------------------
// DELETE Button code:
// When DELETE button is presses, let's delete the record it's associated with. Code being placed on top of
// 'display record' code, so we don't have to refresh the page on DELETE with a 'HEADER' command. HEADER
// command left in place for future example.
// ----------------------------------------------------------------------------------------------------------
$doc_id = $_POST["doc_id"];
if ($_POST['delete'])
{
$sql = "DELETE FROM documents WHERE id = " . $_POST['doc_id'];
mysql_query($sql,$con) or die("query: $query<br>" . mysql_error());
}
// ----------------------------------------------------------------------------------------------------------
// Pull all pdf document records by 'id' and sort ascending
// Display PDF documents by Name/URL>
// Provide buttons to Rename and Delete
// ----------------------------------------------------------------------------------------------------------
$query = "SELECT id, name FROM documents ORDER BY name ASC";
$result = mysql_query($query,$con) or die('Error, query failed');
while(list($id, $name) = mysql_fetch_array($result))
{
?>
<form action="" method="post">
<table>
<tr>
<td width="300"><a href="../modules/documents.php?id=<? echo $id; ?>"><u><? echo $name; ?></u></a></td>
<td><input type="submit" value="Rename" /></td>
<td><input type="submit" name="delete" value="Delete" /></td>
<td><input type="hidden" name="doc_id" value="<? echo $id; ?>" /></td>
</tr>
</table>
</form>
<?
}
// ----------------------------------------------------------------------------------------------------------
// Close database connection
// ----------------------------------------------------------------------------------------------------------
mysql_close($con);
?>
Thank you in advance...