function edit() { global $dbc; global $databasename; //Connect and select database if ($dbc) { if (!$databasename) { die('
Could not select the database
'); } } else { die ('Could not connect to MySQL
'); } if (isset ($_POST['submit'])) //If the form was submitted { //Current time/date originally NOW() $rightnow = date('l dS \of F Y g:i:s A'); //Add slashs to entry to make sure it doesn't mess with the mysql $entry = nl2br ($_POST['entry']); $entry = mysql_real_escape_string ($entry); //Define the query $query = "UPDATE blog_entries SET title='{$_POST['title']}', entry='$entry', date_updated='$rightnow' WHERE pid={$_POST['id']}"; $r = mysql_query ($query); //Execute the query //Report on the result if (mysql_affected_rows() == 1) { print 'The blog entry has been updated
'; } else { print "Could not update the entry because " . mysql_error() . ". The query was $query";
}
}
else //If the update form was not submitted
{
if (isset ($_GET['id'])) //If a post is specified
{
if (is_numeric ($_GET['id'])) //Check if the entry wanted is a number
{
$query = "SELECT * FROM blog_entries WHERE pid={$_GET['id']} LIMIT 1";
if ($r = mysql_query ($query)) //Run the query
{
$row = mysql_fetch_array ($r); //Retrieve the information
//Make the form
$theentry = "";
$theentry = $row['entry'];
$theentry = str_replace("
", "\n", $theentry);//Odd bug here, no idea why
print '
Could not retrieve the entry."; } } } else //If there is no id specified, display the posts { //Define the query to select the blog entries $query = 'SELECT * FROM blog_entries ORDER BY date_posted DESC'; if ($r = mysql_query ($query)) //Run the query { //Retrieve and print every record while ($row = mysql_fetch_array ($r)) { print "
{$row['title']} (Posted by 'insert something here') Edit Delete
\n"; } } else //Query didn't run { die ('Could not retrieve the data'); } //End of Query IF. mysql_close(); //Close the database connection } } }