Forum Moderators: coopster

Message Too Old, No Replies

PHP updating pages from forms

Can somebody possibly enlighten me on the following

         

realitybytes

5:01 am on Apr 17, 2005 (gmt 0)

10+ Year Member



I guess the best place is to start witht he code so here goes, slightly stripped of HTML.

It starts here with an if else include works fine, the else is a couple of links for login in or registering.

<?php
if (isset($loginAuth) && $loginAuth == 'Pass' && $cookiedel!== 'yes') {
include 'W:\WS\www\templates\ratedetect.php';
}
else {
include 'W:\WS\www\templates\loginrate.php';
}
?>

rate detect

<?php
$sql = "SELECT user_id FROM r4p_users WHERE username = '$authName'";
$result = mysql_query($sql);
if (!$result) {
echo('A database error occurred');
}
$row = mysql_fetch_assoc($result);
$user_id = $row['user_id'];
$tb_memRate = 'r4p_'.$page_name;
$sql = "SELECT user_id FROM $tb_memRate WHERE user_id = '$user_id'";
$result = mysql_query($sql);
if (!$result) {
echo('A database error occurred');
}
if (mysql_num_rows($result) == 0) {
include 'W:\WS\www\templates\new_rate.php';
}
else {
include 'W:\WS\www\templates\mem_rate.php';
}
?>

mem rate is an include that displays the entry already submitted int he database, new_rate is as follows stipped form with radio buttons input.

<?php
if (isset($_POST['rate'])) {
$tb_memRate = 'r4p_'.$page_name;
$myRate = $_POST['myRate'];
$sql = "INSERT INTO $tb_memRate SET RID = '$RID', user_id = '$user_id', Mem_Rate = '$myRate', Sub_Date = NOW()";
$result = mysql_query($sql);
if (!$result) {
echo('A database error occurred');
}
$sql = "SELECT COUNT(*) AS num_votes, MIN(Mem_Rate) AS Mem_min, MAX(Mem_Rate) AS Mem_max, AVG(Mem_Rate) AS Mem_avg FROM $tb_memRate";
$result = mysql_query($sql);
if (!$result) {
echo('A database error occurred');
}
$row = mysql_fetch_array($result);
$num_votes = $row['num_votes'];
$Mem_min = $row['Mem_min'];
$Mem_max = $row['Mem_max'];
$Mem_avg = round($row['Mem_avg']);
$sql = "UPDATE r4p_reviews SET Mem_RateAVG = '$Mem_avg', Mem_Hi = '$Mem_max', Mem_Lo = '$Mem_min', Mem_Total = $num_votes WHERE LinkName = '$page_name'";
$result = mysql_query($sql);
if (!$result) {
echo('A database error occurred' . mysql_error());
}
}
?>

The script itself works fine however, when it is first submitted, the page reloads but the page still contains all the original information prior to the form submission it is not until a futher page refresh will the new stats be updated and the person is taken to mem_rate.

Also here is the displaying of the above which is added near the top of the page,

<?php
$sql = "SELECT RID, SiteName, r4p_Rating, Mem_RateAVG, Mem_Hi, Mem_Lo, Mem_Total, date_format(Rev_Date,'%d %b %Y %l:%i%p') as Rev_Date, date_format(Up_Date,'%d %b %Y %l:%i%p') as Up_Date FROM r4p_reviews WHERE LinkName = '$page_name'";
$result = mysql_query($sql);
if (!$result) {
echo('A database error occurred');
}
$row = mysql_fetch_array($result);
$RID = $row['RID'];
$SiteName = $row['SiteName'];
$r4p_Rating = $row['r4p_Rating'];
$Mem_RateAVG = $row['Mem_RateAVG'];
$Mem_Hi = $row['Mem_Hi'];
$Mem_Lo = $row['Mem_Lo'];
$Mem_Total = $row['Mem_Total'];
$Rev_Date = $row['Rev_Date'];
$Up_Date = $row['Up_Date'];

if ($Mem_RateAVG == NULL) {
$Mem_RateAVG = '-';
}
if ($Mem_Hi == NULL) {
$Mem_Hi = '-';
}
if ($Mem_Lo == NULL) {
$Mem_Lo = '-';
}
if ($Mem_Total == NULL) {
$Mem_Total = '-';
}
if ($Up_Date == NULL) {
$Up_Date = '-';
}
?>
I guess I am wrong to assume because this was done server side that the browser should recieve a new page, and all the database queries again after performing all the actions from the form.

I keep running in to this same problem, is there any reason and possible sollution to overcome this?

jd01

7:40 am on Apr 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you though of forcing a refresh by using
header("Pragma: no-cache"); or
header("Cache-Control: no-store, no-cache, must-revalidate");

The documentation is here:
[php.net...]

Justin

realitybytes

8:36 am on Apr 17, 2005 (gmt 0)

10+ Year Member



Thanks for that link have found a work around

page header

if (isset($_POST['rate'])) {
include_once 'W:\WS\www\templates\head_load.php';
}

head load contains

<?php
header("location: " . $_SERVER['PHP_SELF']);
?>

worked first go....

Question, is the original problem a browser issue, server issue, or PHP?
I assumed that the form location posting to the same page would execute the page over once again, obviously this is not the case, and if anybody is aware of a reason why I would be interested. The only reason I can possibly think of is that it is to save on resources, only a guess though.