View Single Post
Old 06-04-2004, 11:40 AM   #9 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,530
sde is on a distinguished road
first off, i highly recommend that you do NOT name your tables with a month/year like you have in your update query.

the better thing to do is simply add a 'time' field to your blog table for each entry.

the code example below does not reflect that change, but here is how i think your page should be setup.

note: this assumes that the entryid variable was sent from the previous page with the POST method. If you use the GET method ( in the url ) you will need to modify it a bit.

PHP Code:
<?
// connect & select
$lnk mysql_connect('mysqladdress''login''password') or die (mysql_error());
mysql_select_db('dbname',$lnk);

// handle update
// here we test for the post variable action.  this is better than
// testing for $submit because if the user presses 'enter' instead of clicking
// the button 'submit', then the variable 'submit' would not get sent. 
// see the form below to see the hidden variable 'action'
if ($_POST['action']=="update") {
  
$updquery "UPDATE may2004 SET title='".$_POST['newtitle']."',entry='".$_POST['newentry']."' 
                WHERE id='"
.$_POST['entryid']."'";
  
mysql_query($updquery) or die(mysql_error());
  echo(
"<p>Your entry has been updated.</p>");
}

// define sql
$sql "SELECT title,entry from table where entryid='".$_POST['entryid']."'";
// execute query
$result mysql_query($query_title) or die(mysql_error());

// since the entryid field is unique, and it is only possible to have
// one result, then we can use 'if' instead of 'while' to assign the 
// result row to the array variable $row
if($row mysql_fetch_array($result)){
  
  
// notice the 2 hiden variables we have in the form.
  // 'action' and 'entryid'
  
echo "<form method=\"post\" action=\"".$PHP_SELF."\">
  <input type=\"hidden\" name=\"action\" value=\"update\">
  <input type=\"hidden\" name=\"entryid\" value=\""
.$_POST['entryid']."\">
  
  <input type=\"text\" name=\"newtitle\" value=\""
.stripslashes($row['title']."\">
  <br />
  <textarea name=\"newentry\" rows=\"10\" cols=\"70\">
  "
.stripslashes($row['entry']."
  </textarea>
  <br />
  <input type=\"Submit\" name=\"submitentry\" value=\"submit\">
  </form>"
;
} else {
  
// this will show if the entry id is not valid
  
echo "invalid id';
}
?>
__________________
Mike
sde is offline   Reply With Quote