Prevent Insert with custom message?
Prevent Insert with custom message?
Hi folks
I am trying to figure out a way to prevent inserting data that would be considered a duplicate of existing data, and provide a clean message letting the user know that they have already entered such information.
I was looking at doing SQL in the hooks for tablename_before_insert to query the DB using the $data from the existing page, but then my only option is to return FALSE to prevent the insert, not give some nice message.
IE I am trying to prevent this from happening:
I am trying to figure out a way to prevent inserting data that would be considered a duplicate of existing data, and provide a clean message letting the user know that they have already entered such information.
I was looking at doing SQL in the hooks for tablename_before_insert to query the DB using the $data from the existing page, but then my only option is to return FALSE to prevent the insert, not give some nice message.
IE I am trying to prevent this from happening:
-
- AppGini Super Hero
- Posts: 85
- Joined: 2014-06-14 03:08
- Location: India
- Contact:
Re: Prevent Insert with custom message?
Hi,
May be you can try this
Querying the database table for the same values given, if id is returned , means there is a record with same values if null is returned ,means there is no record with the given values.
May be you can try this
Code: Select all
$id=sqlValue("select id from <table name > where show='{$data['show']}'' and dog='{$data['dog']}'' and class='{$data['class']}''");
if($id==null)
return true;
else
return false;
Re: Prevent Insert with custom message?
Hi Uday thanks for the reply.
I had tried something like that though what I'd like to be able to do is present a message to the user like "You have already added an entry for this dog" versus just not accepting the input. I cant find where I would be able to put some message to the user that would be helpful for them to know why their entry isnt accepted.
I had tried something like that though what I'd like to be able to do is present a message to the user like "You have already added an entry for this dog" versus just not accepting the input. I cant find where I would be able to put some message to the user that would be helpful for them to know why their entry isnt accepted.
-
- AppGini Super Hero
- Posts: 85
- Joined: 2014-06-14 03:08
- Location: India
- Contact:
Re: Prevent Insert with custom message?
This will work.
Code: Select all
// hooks code
$id=sqlValue("select id from <table name > where show='{$data['show']}'' and dog='{$data['dog']}'' and class='{$data['class']}''");
if($id==null)
{
return true;
}
else
{
$_SESSION['custom_err_msg']= "<Message You want to display>";
return false;
}
// find the following code in incCommon.php
elseif($_REQUEST['record-added-error'] != ''){
$msg = $Translation['Couldn\'t save the new record'];
$class = 'alert-danger';
$fadeout = false;
}
// replace with
elseif($_REQUEST['record-added-error'] != ''){
$msg = $_SESSION['custom_err_msg'];
unset($_SESSION['custom_err_msg']);
$class = 'alert-danger';
$fadeout = false;
}
Re: Prevent Insert with custom message?
Thanks Uday - using a Session variable is a great idea! I am going to give this a shot.
Re: Prevent Insert with custom message?
Hi,
I am trying to set up custom error messages, using the method suggested here with session variables.
But I have a problem, so I am turning to the community.
In a table hook _before_update I check for certain things. If the check fails, I return FALSE (cancel the update) and write a session variable:
In the hooks/header-extras.php I try to use this code to check for the variable, if it exists, output the message (nicely formated).
Unfortunately this fails.
I have been using some log file and it seems the header-extras is used twice for rendering the page. It seems further, that session variable is found in the first run, then unset, and only the second run of header-extras.php is actually used to show output on the page. This results in no message on screen.
my code in hookd/header-extras.php
Has anyone accomplished custom error messages using this (or some other) method?
What am I doing wrong?
Hint: When I do NOT use the unset command, the error is shown - but only my own error message, not the language string. (It makes no difference if I remove the translation string.)
Olaf
I am trying to set up custom error messages, using the method suggested here with session variables.
But I have a problem, so I am turning to the community.
In a table hook _before_update I check for certain things. If the check fails, I return FALSE (cancel the update) and write a session variable:
Code: Select all
$_SESSION['custom_err_msg']= $Translation['Couldn\'t save changes to the record'] . " here my own error message.";
Unfortunately this fails.
I have been using some log file and it seems the header-extras is used twice for rendering the page. It seems further, that session variable is found in the first run, then unset, and only the second run of header-extras.php is actually used to show output on the page. This results in no message on screen.
my code in hookd/header-extras.php
Code: Select all
<?php
if (isset($_SESSION['custom_err_msg'])) {
$customError ='<div class="row"><div id="customErrorMessage" class="alert alert-dismissable alert-danger" style="padding-top: 6px; padding-bottom: 6px;">'.$_SESSION['custom_err_msg'].'</div></div>';
echo $customError;
unset($_SESSION['custom_err_msg']);
}
?>
What am I doing wrong?
Hint: When I do NOT use the unset command, the error is shown - but only my own error message, not the language string. (It makes no difference if I remove the translation string.)
Olaf
Some postings I was involved, you might find useful:
Multi Path Upload (MPU) / dynamic upload folder; SingleEdit - Prevent concurrent edits on records; Field Permissions; Column-Value-Based-Permissions; Custom (error) message; Audit Log; Backup your database; Two Factor Authentication; Block brute force (failed) logins; Add 2nd SAVE CHANGES button
Multi Path Upload (MPU) / dynamic upload folder; SingleEdit - Prevent concurrent edits on records; Field Permissions; Column-Value-Based-Permissions; Custom (error) message; Audit Log; Backup your database; Two Factor Authentication; Block brute force (failed) logins; Add 2nd SAVE CHANGES button
Custom error message with $SESSION does not work
Hi,
I decided to push my request by rephrasing the subject.
Does anyone have a suggestion how to get it (see my question above) to work?
Olaf
I decided to push my request by rephrasing the subject.
Does anyone have a suggestion how to get it (see my question above) to work?
Olaf
Some postings I was involved, you might find useful:
Multi Path Upload (MPU) / dynamic upload folder; SingleEdit - Prevent concurrent edits on records; Field Permissions; Column-Value-Based-Permissions; Custom (error) message; Audit Log; Backup your database; Two Factor Authentication; Block brute force (failed) logins; Add 2nd SAVE CHANGES button
Multi Path Upload (MPU) / dynamic upload folder; SingleEdit - Prevent concurrent edits on records; Field Permissions; Column-Value-Based-Permissions; Custom (error) message; Audit Log; Backup your database; Two Factor Authentication; Block brute force (failed) logins; Add 2nd SAVE CHANGES button
Re: Prevent Insert with custom message?
Hi Olaf,
nice one
.
The issue is, that the header-extras.php gets called twice and hence you delete your Sessionvariable after the first time the header-extras is called. So when it is called the second time, the Session variable does not exist any more and your if statement fails.
you may try:
But you could als try to overwrite the message that is already generated by something like
nice one

The issue is, that the header-extras.php gets called twice and hence you delete your Sessionvariable after the first time the header-extras is called. So when it is called the second time, the Session variable does not exist any more and your if statement fails.
you may try:
Code: Select all
if (isset($_SESSION['custom_err_msg'])) {
$customError ='<div class="row"><div id="customErrorMessage" class="alert alert-dismissable alert-danger" style="padding-top: 6px; padding-bottom: 6px;">'.$_SESSION['custom_err_msg'].'</div></div>';
if (isset($_SESSION['clear_message'])) {
unset($_SESSION['custom_err_msg']);
unset($_SESSION['clear_message']);
}
$_SESSION['clear_message']=1;
}
Code: Select all
if (isset($_SESSION['custom_err_msg'])) {
echo "<script>\$j(function() { \$j('[id^=notification]').text('".$_SESSION['custom_err_msg']."');});</script>";
if (isset($_SESSION['clear_message'])) {
unset($_SESSION['custom_err_msg']);
unset($_SESSION['clear_message']);
}
$_SESSION['clear_message']=1;
}
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.
Re: Prevent Insert with custom message?
Hi pbötcher,
thank you for your answer. I will check this and report back.
Olaf
thank you for your answer. I will check this and report back.
Olaf
Some postings I was involved, you might find useful:
Multi Path Upload (MPU) / dynamic upload folder; SingleEdit - Prevent concurrent edits on records; Field Permissions; Column-Value-Based-Permissions; Custom (error) message; Audit Log; Backup your database; Two Factor Authentication; Block brute force (failed) logins; Add 2nd SAVE CHANGES button
Multi Path Upload (MPU) / dynamic upload folder; SingleEdit - Prevent concurrent edits on records; Field Permissions; Column-Value-Based-Permissions; Custom (error) message; Audit Log; Backup your database; Two Factor Authentication; Block brute force (failed) logins; Add 2nd SAVE CHANGES button
Re: Prevent Insert with custom message?
Hi pbötcher,
for now, neither of your suggestions works (PS: your 1st suggestion was missing an echo $customError , but this is not the problem).
As the session variable is set in a function I am thinking of using session_start() in that function, and again in the header-extras.php - just in case, the session has not been resumed at this time (even though, I am pretty sure I had checked that before.
Found this " force your session_starts everywhere and anywhere you invoke the $_SESSION superglobal." here http://www.codingforums.com/php/207845- ... ction.html maybe it helps to really start the session in every extra file.
If anyone has some other suggestions, they are very welcome.
Have to go. I will test my assumptions tomorrow or the day after and report back.
Olaf
for now, neither of your suggestions works (PS: your 1st suggestion was missing an echo $customError , but this is not the problem).
As the session variable is set in a function I am thinking of using session_start() in that function, and again in the header-extras.php - just in case, the session has not been resumed at this time (even though, I am pretty sure I had checked that before.
Found this " force your session_starts everywhere and anywhere you invoke the $_SESSION superglobal." here http://www.codingforums.com/php/207845- ... ction.html maybe it helps to really start the session in every extra file.
If anyone has some other suggestions, they are very welcome.
Have to go. I will test my assumptions tomorrow or the day after and report back.
Olaf
Some postings I was involved, you might find useful:
Multi Path Upload (MPU) / dynamic upload folder; SingleEdit - Prevent concurrent edits on records; Field Permissions; Column-Value-Based-Permissions; Custom (error) message; Audit Log; Backup your database; Two Factor Authentication; Block brute force (failed) logins; Add 2nd SAVE CHANGES button
Multi Path Upload (MPU) / dynamic upload folder; SingleEdit - Prevent concurrent edits on records; Field Permissions; Column-Value-Based-Permissions; Custom (error) message; Audit Log; Backup your database; Two Factor Authentication; Block brute force (failed) logins; Add 2nd SAVE CHANGES button
Re: Prevent Insert with custom message?
Hi,
what did you get? It worked (with the echo of course) in my environment.
Can you post the code you have.
what did you get? It worked (with the echo of course) in my environment.
Can you post the code you have.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.
Re: Prevent Insert with custom message?
Hi pbötcher,
first - I did ass session_start to the header-extra and my hooks/tablename.php file as the very first line.
I am using this code in my header-extras now:
and this code is in the TABLENAME_before_update function ( $myReturnValue is set to either TRUE or false depending on the checks I am doing):
and this results when I reproduce an error on purpose:
Source of this:
Olaf
first - I did ass session_start to the header-extra and my hooks/tablename.php file as the very first line.
I am using this code in my header-extras now:
Code: Select all
echo "<!-- before test -->";
echo "A s[scounter]: ".$_SESSION['sess_counter'];
$_SESSION['sess_counter'] = $_SESSION['sess_counter'] + 2;
if ($_SESSION['sess_counter'] >= 2){
if (isset($_SESSION['custom_err_msg'])) {
$customError ='<div class="row"><div id="customErrorMessage" class="alert alert-dismissable alert-danger" style="padding-top: 6px; padding-bottom: 6px;">'.$_SESSION['custom_err_msg'].'</div></div>';
echo $customError;
unset($_SESSION['custom_err_msg']);
unset($_SESSION['sess_counter']);
}
}
echo "B s[scounter]: ".$_SESSION['sess_counter'];
echo "<!-- after test -->";
Code: Select all
if ($myReturnValue === TRUE) {
$_SESSION['sess_counter'] = 0; }
else {
$_SESSION['sess_counter'] = $_SESSION['sess_counter'] + 2;
}
return $myReturnValue;
} //closes the TABLENAME_before_update function
Code: Select all
<!-- before test -->A s[scounter]: 2B s[scounter]: 4<!-- after test --><!-- END Header Extras --> <!-- Add header template below here .. -->
Olaf
Some postings I was involved, you might find useful:
Multi Path Upload (MPU) / dynamic upload folder; SingleEdit - Prevent concurrent edits on records; Field Permissions; Column-Value-Based-Permissions; Custom (error) message; Audit Log; Backup your database; Two Factor Authentication; Block brute force (failed) logins; Add 2nd SAVE CHANGES button
Multi Path Upload (MPU) / dynamic upload folder; SingleEdit - Prevent concurrent edits on records; Field Permissions; Column-Value-Based-Permissions; Custom (error) message; Audit Log; Backup your database; Two Factor Authentication; Block brute force (failed) logins; Add 2nd SAVE CHANGES button
Re: Prevent Insert with custom message?
Hi Olaf,
as I sayed, the header-extras.php is called twice. So according to your logic, you execute the
if (isset($_SESSION['custom_err_msg'])) {
the first time the header-extras is called and then you unset your unset($_SESSION['custom_err_msg']);
As the output is not directly displayed, you will not see anything from the first passing.
The second time you pass into the header-extras.phh and the $_SESSION['custom_err_msg'] is not existing as you unset it already.
Hence the output you see is correct.
You need to make sure that your $_SESSION['custom_err_msg'] exists, when you execute the header-extras.php the second time.
as I sayed, the header-extras.php is called twice. So according to your logic, you execute the
if (isset($_SESSION['custom_err_msg'])) {
the first time the header-extras is called and then you unset your unset($_SESSION['custom_err_msg']);
As the output is not directly displayed, you will not see anything from the first passing.
The second time you pass into the header-extras.phh and the $_SESSION['custom_err_msg'] is not existing as you unset it already.
Hence the output you see is correct.
You need to make sure that your $_SESSION['custom_err_msg'] exists, when you execute the header-extras.php the second time.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.
Re: Prevent Insert with custom message?
Hi pbötcher,
I was able to create a working solution. Thank you for your help.
This is my solution to show a custom error message on the page if the record could not be saved.
The solution here applies to updating a record, but can easily be changed to inserting a new one or deleting.
In the hooks/TABLENAME -> _before_update function, I am setting the variable $myReturnValue to either TRUE (if the record can be saved) or FALSE (if the record must not be saved).
When setting that variable, I also set a session variable custom_err_msg to the message, I want to show on the resulting page (in my case, top of the TABLENAME_view). The code looks like this
The code above prepares for the /hooks/header-extras.php code which checks for the variable, if needed, outputs the error and resets the values. The trick (at least for me) is: header-extras seems to be run more than once (why?). The session variable custom_err_msg must be reset only after we are sure, the error has been shown, thus I needed to fiddle around with the session variable custom_err_shown:
Almost forgot: My css for the IDs and classes I use for the custom error message:
Thank you for your help in solving this problem. Is this worth to be posted n the tips& tricks section as well?
Olaf
I was able to create a working solution. Thank you for your help.
This is my solution to show a custom error message on the page if the record could not be saved.
The solution here applies to updating a record, but can easily be changed to inserting a new one or deleting.
In the hooks/TABLENAME -> _before_update function, I am setting the variable $myReturnValue to either TRUE (if the record can be saved) or FALSE (if the record must not be saved).
When setting that variable, I also set a session variable custom_err_msg to the message, I want to show on the resulting page (in my case, top of the TABLENAME_view). The code looks like this
Code: Select all
function TABLENAME_before_update(&$data, $memberInfo, &$args){
// first initialize the variable to some default:
$myReturnValue = TRUE;
//... more code in the function ...
if ($OnTopCounter <> 0){ //here I do my checking if the record can be saved (=updated) or not
$_SESSION['custom_err_msg'] = 'You can not do this because ........';
$myReturnValue = FALSE;
}
//Prepare user defined error. Shown in header-extras
if ($myReturnValue === TRUE) {
unset($_SESSION['custom_err_msg']); //no custom error, when record can be saved
}
return $myReturnValue; // returns from the _before_update function. If TRUE, record will be saved, if false AG will show error AND my error
}
Code: Select all
if (isset($_SESSION['custom_err_msg'])) {
$customError ='<div id="customErrorMessage" class="custmErrMsg alert alert-dismissable alert-danger">'.$_SESSION['custom_err_msg'].'</div>';
echo $customError;
if ($_SESSION['custom_err_shown'] == 1){
unset($_SESSION['custom_err_msg']);
$_SESSION['custom_err_shown'] = 0;
}
else {
$_SESSION['custom_err_shown'] = 1;
}
}
Code: Select all
#customErrorMessage {
margin-top: 2em !important;
}
.custmErrMsg{}
Olaf
Some postings I was involved, you might find useful:
Multi Path Upload (MPU) / dynamic upload folder; SingleEdit - Prevent concurrent edits on records; Field Permissions; Column-Value-Based-Permissions; Custom (error) message; Audit Log; Backup your database; Two Factor Authentication; Block brute force (failed) logins; Add 2nd SAVE CHANGES button
Multi Path Upload (MPU) / dynamic upload folder; SingleEdit - Prevent concurrent edits on records; Field Permissions; Column-Value-Based-Permissions; Custom (error) message; Audit Log; Backup your database; Two Factor Authentication; Block brute force (failed) logins; Add 2nd SAVE CHANGES button
Re: Prevent Insert with custom message?
Hi Olaf,
glad to see it works, also I do not really see much of a difference between your solution and the solution already mentioned in
viewtopic.php?f=7&t=1740#p10877
glad to see it works, also I do not really see much of a difference between your solution and the solution already mentioned in
viewtopic.php?f=7&t=1740#p10877
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.