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
what does


$mailbox_id = "-1"; do?

$mailbox_id = "-1"; sets the default value of the variable to -1 so that if no value is set for $mailbox_id then the default value will trigger the query and update a "ghost" table row. If you look over the code it should make sense of how to accomplish the task of updating a table upon page entry.

"I don't think it works" doesn't help much. What problems are you having where it doesn't work? Do you still have the other query setup to determine the $_row_messagesList['mailbox_id'];?


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']);
}