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());
Inspiring
April 6, 2010

Hi,

Thanks for that code. I dont think it works.

Also what does


$mailbox_id = "-1"; do?