Copy link to clipboard
Copied
Hello All,
Please could someone kindly review my code and advice where I am wrong. Below is a code on how to show an alert message after updating a record but it seems not to work. I use similar code for insert and its working well but on Update its not showing any message
Below is the UPDATE code
<?php
if(isset($_POST['submit']))
{
$ass_id=$_POST['ass_id'];
$txpaid=$_POST['txpaid'];
$editedby=$_POST['editedby'];
$dateedited=$_POST['dateedited'];
mysql_select_db ($database_DB,$DB);
$qur="UPDATE tbl_assess SET txpaid='$txpaid', editedby='$editedby', dateedited='$dateedited' WHERE ass_id=$ass_id";
$retval = mysql_query($qur,$CRIRS_DB) or die(mysql_error());
if ($retval == true) {
$result='<div class="alert alert-success">Your assessment was successfully posted</div>';
}else {
$result='<div class="alert alert-danger">Sorry there was an error posting your assessment. Please try again</div>';
}
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
?>
Here is the code above my form that shows the message
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
Your review tips is highly appreciated...
Thank you in anticipation.
Mike
Start a php SESSION by inserting session_start(); BEFORE anything else on your page:
<?php
session_start();
?>
Obviously you then have a connection to your database.
Below is the code to execute once the 'submit' button has been clicked: (include the correct variable id name for your $_POST - $***_id=$_POST['***_id']; and your sql query WHERE ***_id=$***_id in the code below)
<?php
if(isset($_POST['submit'])) {
$***_id=$_POST['***_id'];
$txpaid=$_POST['txpaid'];
$editedby=$_POST['editedby'];
$dateedited=$_
...Copy link to clipboard
Copied
You need to store the '$result' message in a php $_SESSION variable because you are returning to the same page once the form update button is clicked and the $result variable wont be set until you click the form update button again. Everytime the page reloads php resets itself to zero unless you store information in session variables.
Give me a shout in the forum if you want a code example using a $_SESSION
Copy link to clipboard
Copied
Dear osgood_,
I really appreciate your response.
Please I will need some code example on how to use the $_SESSION.
Thank you so much....
Mike
Copy link to clipboard
Copied
Start a php SESSION by inserting session_start(); BEFORE anything else on your page:
<?php
session_start();
?>
Obviously you then have a connection to your database.
Below is the code to execute once the 'submit' button has been clicked: (include the correct variable id name for your $_POST - $***_id=$_POST['***_id']; and your sql query WHERE ***_id=$***_id in the code below)
<?php
if(isset($_POST['submit'])) {
$***_id=$_POST['***_id'];
$txpaid=$_POST['txpaid'];
$editedby=$_POST['editedby'];
$dateedited=$_POST['dateedited'];
$qur="UPDATE tbl_assess SET txpaid='$txpaid', editedby='$editedby', dateedited='$dateedited' WHERE ***_id=$***_id";
mysql_select_db($database_DB, $DB);
$retval = mysql_query($qur, $DB);
if($retval) {
$_SESSION['result'] = '<div class="alert alert-success">Your assessment was successfully posted</div>';
}else {
$_SESSION['result'] = '<div class="alert alert-danger">Sorry there was an error posting your assessment. Please try again</div>';
}
mysql_close($DB);
}
?>
Insert the below in the page where you want your 'result' message to appear:
<?php
if(isset($_SESSION['result'])) {
echo $_SESSION['result'];
unset($_SESSION["result"]);
}
?>
The other way to do this is use Ajax to process the form and send the response 'data' from the php processing page back to the form page.
Copy link to clipboard
Copied
Dear osgood_​,
Thank you so much for the answer.
It worked perfectly well although I had to remove this closing like "mysql_close($DB);" due to the "Warning: mysql_query(): 3 is not a valid MySQL-Link resource......" it was generating. Besides this, everything is OK.
Thank you so much.
Mike
Copy link to clipboard
Copied
there was question, that seams to have been removed ?....