Skip to main content
Inspiring
March 30, 2010
Answered

update table on page entry

  • March 30, 2010
  • 1 reply
  • 721 views

hi,

What is the best way to update a table on page entry?

I have a record I need to add a value of 1 to once it is loaded (this is not a counter)

By default the value is 0 added by mysql. I want to update it for when the user has viewed that record when the message.php page is loaded for the id of that page.

I have done this so far, but I want to submit it without having a button on the page.

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "update_message_status")) {
  $updateSQL = sprintf("UPDATE mailbox SET viewed=%s WHERE mailbox_id=%s",
                       GetSQLValueString($_POST['viewed'], "text"),
                       GetSQLValueString($_POST['mailbox_id'], "int"));

  mysql_select_db($database_db, $db);
  $Result1 = mysql_query($updateSQL, $db) or die(mysql_error());

  $updateGoTo = "message.php?id='".$row_messagesList['mailbox_id']."'";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}

<form action="<?php echo $editFormAction; ?>" method="POST" name="update_message_status">
<input name="mailbox_id" type="hidden" value="<?php echo $row_messagesList['mailbox_id']; ?>" />
<input name="viewed" type="hidden" value="1" />
<input type="hidden" name="MM_update" value="update_message_status" />
</form>

Thanks

This topic has been closed for replies.
Correct answer

scrap your whole code and put something like this:

mysql_select_db($database_db, $db);
$mailbox_id = "-1";
if (isset($_row_messagesList['mailbox_id'])) {
$mailbox_id = $_row_messagesList['mailbox_id'];
}
$updateSQL = sprintf("UPDATE mailbox SET viewed= '1' WHERE mailbox_id=%s", GetSQLValueString($mailbox_id, "int"));
$Result1 = mysql_query($updateSQL, $db) or die(mysql_error());

1 reply

Inspiring
April 1, 2010

I cannot find a better way to do this, I have used body onload but it keeps refreshing the page.

Has anyone fixed this problem before?

Correct answer
April 5, 2010

scrap your whole code and put something like this:

mysql_select_db($database_db, $db);
$mailbox_id = "-1";
if (isset($_row_messagesList['mailbox_id'])) {
$mailbox_id = $_row_messagesList['mailbox_id'];
}
$updateSQL = sprintf("UPDATE mailbox SET viewed= '1' WHERE mailbox_id=%s", GetSQLValueString($mailbox_id, "int"));
$Result1 = mysql_query($updateSQL, $db) or die(mysql_error());
April 6, 2010

I made these changes and it started working. Also where I had the code it would not submit it so I took it out and put it above any other insert, select, update code and it worked.

$getMsg = $_GET['id'];

mysql_select_db($database_db, $db);
//$mailbox_id = "-1";
if (isset($getMsg)) {
$mailbox_id = $getMsg;
}
$updateSQL = sprintf("UPDATE mailbox SET viewed= '1' WHERE mailbox_id=%s", GetSQLValueString($mailbox_id, "int"));
$Result1 = mysql_query($updateSQL, $db) or die(mysql_error());

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}


Something to consider:

What is to prevent anyone from entering any id into the URL string to update the table as viewed = 1 when the person the message intended for didn't actually read the message?

You should probably have something like where mailbox_id = URL parameter AND to_id = session variable in your UPDATE query so that only the person that's logged in can update their respective message_viewed status. I'm sure you already have something else in place to prevent others from viewing the messages that aren't intended for them but in case you don't it's a serious consideration.