UPDATE Display on Reload

Hi all, I have a form which is for the user to update their details. When they press ‘Update’, this does indeed update the database and page refreshes but refreshes with still the old data. I need to refresh again for it to show the latest updated record. Any ideas as to what I can do to conquer this?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php
require('auth.php');
require('includes/head.php');
?>
<body>
<?php
include('includes/header.php');
require('includes/connection.php');
	$url = $_GET['id'];
		$query = mysql_query("SELECT * FROM customers WHERE customer_ref='$url'");
			$res = mysql_fetch_array($query);
				if (isset($_POST['update'])) {
					$update = mysql_query("UPDATE customers SET c_dob='$_POST[cdob]' WHERE customer_ref='$url'");
					}
?>
<div id="wrap">
<div id="mainWrap">
<form action="<?=$_SERVER['PHP_SELF'].'?id='.$url?>" method="post" id="createorder">
<div class="headerTitles">Edit Customer Details</div>
<!-- BOF Left Col -->
<div id="leftCol">
<span class="fontField bold">Title </span>
<select class="textField" <?=$disabled?>  name="title">
  <option value="NULL">Choose a Title</option>
  <option value="Mr" <?php if ($res['c_title']=="Mr"){ echo "selected=\\"selected\\"";} ?>>Mr</option>
  <option value="Mrs" <?php if ($res['c_title']=="Mrs"){ echo "selected=\\"selected\\"";} ?>>Mrs</option>
  <option value="Miss" <?php if ($res['c_title']=="Miss"){ echo "selected=\\"selected\\"";} ?>>Miss</option>
  <option value="Ms" <?php if ($res['c_title']=="Ms"){ echo "selected=\\"selected\\"";} ?>>Ms</option>
  <option value="Dr" <?php if ($res['c_title']=="Dr"){ echo "selected=\\"selected\\"";} ?>>Dr.</option>
  <option value="Rev" <?php if ($res['c_title']=="Rev"){ echo "selected=\\"selected\\"";} ?>>Rev</option>
</select>
<span class="fontField bold lower">First Name </span>
<input type="text" name="cfirstname" <?=$disabled?> value="<?php echo $res['c_firstname'] ?>" class="<?php if(isset($alert[1])){echo $alert[1];} else {echo "textField";} ?>" />

<span class="fontField bold lower">Last Name </span>
<input type="text" name="clastname" <?=$disabled?> value="<?php echo $res['c_lastname'] ?>" class="<?php if(isset($alert[2])){echo $alert[2];} else {echo "textField";} ?>" />

<span class="fontField bold lower">Address </span>
<textarea name="caddress" <?=$disabled?> rows="6" cols="0" class="<?php if(isset($alert[3])){echo $alert[3];} else {echo "textField";} ?>"><?php echo $res['c_address'] ?></textarea>

<span class="fontField bold lower">Contact Number</span>
<input type="text" <?=$disabled?> name="ccontactnumber" value="<?php echo $res['c_number'] ?>" class="textField" />

<span class="fontField bold lower">Email Address</span>
<input type="text" <?=$disabled?> name="cemail" value="<?php echo $res['c_email'] ?>" class="textField" />
</div>
<!-- EOF Left Col -->

<!-- BOF Right Col -->
<div id="rightCol">
<span class="fontField bold">Registration Date</span>
<input type="text" name="cname" value="<?php echo $res['c_regdate'] ?>" class="textField" disabled="disabled" />

<span class="fontField bold lower">Customer ID Number</span>
<input type="text" name="custnotes" value="<?php echo $res['customer_ref'] ?>" class="textField" disabled="disabled" />

<span class="fontField bold lower">Customer Notes</span>
<textarea name="cnotes" <?=$disabled?> class="textField" rows="6" cols="0"><?php echo $res['c_notes'] ?></textarea>

<span class="fontField bold lower">Date of Birth</span>
<input type="text" name="cdob" <?=$disabled?> value="<?=$res['c_dob']?>" class="textField" />
</div>
<div class="headerTitles"><input type="submit" value="Update" name="update" id="update" /></div>
</form>
</body>
</html>

Just perform a PRG, it should help you out.

So, check if the form is being submitted, if so update then redirect the user back to the same page using header. If not, render the form.

As I don’t know exactly what is going on, something like…


<?php
  require('auth.php');
  require('includes/connection.php');
  
  if('POST' === $_SERVER['REQUEST_METHOD']){
    #update record
    #if successfull, redirect back to this page
    header('Location: http://www.example.org/update.php', true, 302);
    exit;
  }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php
  require('includes/head.php');
?>
<body>
<?php
  include('includes/header.php');
  $url = $_GET['id'];
  $query = mysql_query("SELECT * FROM customers WHERE customer_ref='$url'");
  $res = mysql_fetch_array($query);
?>
<div id="wrap">
<div id="mainWrap">
<form action="<?=$_SERVER['PHP_SELF'].'?id='.$url?>" method="post" id="createorder">
<div class="headerTitles">Edit Customer Details</div>
<!-- BOF Left Col -->
<div id="leftCol">
<span class="fontField bold">Title </span>
<select class="textField" <?=$disabled?>  name="title">
  <option value="NULL">Choose a Title</option>
  <option value="Mr" <?php if ($res['c_title']=="Mr"){ echo "selected=\\"selected\\"";} ?>>Mr</option>
  <option value="Mrs" <?php if ($res['c_title']=="Mrs"){ echo "selected=\\"selected\\"";} ?>>Mrs</option>
  <option value="Miss" <?php if ($res['c_title']=="Miss"){ echo "selected=\\"selected\\"";} ?>>Miss</option>
  <option value="Ms" <?php if ($res['c_title']=="Ms"){ echo "selected=\\"selected\\"";} ?>>Ms</option>
  <option value="Dr" <?php if ($res['c_title']=="Dr"){ echo "selected=\\"selected\\"";} ?>>Dr.</option>
  <option value="Rev" <?php if ($res['c_title']=="Rev"){ echo "selected=\\"selected\\"";} ?>>Rev</option>
</select>
<span class="fontField bold lower">First Name </span>
<input type="text" name="cfirstname" <?=$disabled?> value="<?php echo $res['c_firstname'] ?>" class="<?php if(isset($alert[1])){echo $alert[1];} else {echo "textField";} ?>" />
<span class="fontField bold lower">Last Name </span>
<input type="text" name="clastname" <?=$disabled?> value="<?php echo $res['c_lastname'] ?>" class="<?php if(isset($alert[2])){echo $alert[2];} else {echo "textField";} ?>" />
<span class="fontField bold lower">Address </span>
<textarea name="caddress" <?=$disabled?> rows="6" cols="0" class="<?php if(isset($alert[3])){echo $alert[3];} else {echo "textField";} ?>"><?php echo $res['c_address'] ?></textarea>
<span class="fontField bold lower">Contact Number</span>
<input type="text" <?=$disabled?> name="ccontactnumber" value="<?php echo $res['c_number'] ?>" class="textField" />
<span class="fontField bold lower">Email Address</span>
<input type="text" <?=$disabled?> name="cemail" value="<?php echo $res['c_email'] ?>" class="textField" />
</div>
<!-- EOF Left Col -->
<!-- BOF Right Col -->
<div id="rightCol">
<span class="fontField bold">Registration Date</span>
<input type="text" name="cname" value="<?php echo $res['c_regdate'] ?>" class="textField" disabled="disabled" />
<span class="fontField bold lower">Customer ID Number</span>
<input type="text" name="custnotes" value="<?php echo $res['customer_ref'] ?>" class="textField" disabled="disabled" />
<span class="fontField bold lower">Customer Notes</span>
<textarea name="cnotes" <?=$disabled?> class="textField" rows="6" cols="0"><?php echo $res['c_notes'] ?></textarea>
<span class="fontField bold lower">Date of Birth</span>
<input type="text" name="cdob" <?=$disabled?> value="<?=$res['c_dob']?>" class="textField" />
</div>
<div class="headerTitles"><input type="submit" value="Update" name="update" id="update" /></div>
</form>
</body>
</html>

Use header() to redirect the user once the database has been updated


if (isset($_POST['update'])) { 
                    $update = mysql_query("UPDATE customers SET c_dob='$_POST[cdob]' WHERE customer_ref='$url'"); 

header("Location: whateverthispageiscalled.php");
exit();
                    }

Off Topic:

Hoy, that’s what I said! :stuck_out_tongue:

Here’s a simpler example.


<?php
function getUser($id){
  return array(
    'id'        => 3,
    'username'  => 'AnthonySterling'
  );
}

$user = getUser($_GET['id']);

#if we're updating
if('POST' === $_SERVER['REQUEST_METHOD']){
  #update
  $sql = sprintf(
    "UPDATE user SET username = '%s' WHERE id = %d LIMIT 1;",
    $user['username'],
    $user['id']
  );
  #redirect
  header('Location: http://www.example.org/update.php', true, 302);
  exit;
}

?>
<html>
  <head>
    <title>Demo</title>
  </head>
  <body>
    <form action="update.php?id=<?php echo $user['id']; ?>" method="post">
      <input type="text" name="username" value="<?php echo $user['username']; ?>" />
      <input type="hidden" name="id" value="<?php echo $user['id']; ?>" />
      <input type="submit" value="update" />
    </form>
  </body>
</html>

Great minds Anthony, great minds :smiley:

… but fools seldom differ.

Hey chaps, the header doesn’t seem to want to redirect to another page. I have an exit() underneath. When I submit the update, the background image loads but that’s all which I’m presuming is because of the exit. Any ideas why the header isn’t working?

Just so we’re clear, you’re not redirecting to another page, it’s the same one. :slight_smile:

Check your logic/flow, if the exit is begin reached for a standard ‘get’ request, it’s wrong.

Hey, no I want to redirect the same page, have the fields disabled (which is working perfectly) so the user can see their changes. But it’s refreshing with the same results until you load it once more and then changes to the data the user has just entered.

Can you post your modified code?

Hey dude, I feel like a right spanner, I just need to move the following piece of code below the database update, so now it’s all good. Cheers for help buds :smiley:

$query = mysql_query("SELECT * FROM customers WHERE customer_ref='$url'");
			$res = mysql_fetch_array($query);