Issue comparing values in php - php

I have this php code meant to compare two values,a variable $rate received from a form which can either have values 'applaud' or 'boo' so I want to check if the value is neither of it and kill the page with an error message.I've tried that but ...localhost can not handle this request
HERE IS MY CODE:
<?php
$rate=$_POST['rate'];
echo $rate;
?>
<?php
if($rate != 'applaud' OR $rate != 'boo')
{
die("Sorry there was a problem var rate was not well stated");
}
else
{
echo 'yay ,well stated!!!';
}
?>

I will apply some useful check to do so:-
<?php
if(!empty($_POST['rate'])){ //check data is coming or not actually
$rate= $_POST['rate'];
if($rate != 'applaud' && $rate != 'boo'){ // use && to check for neither of it
die("Sorry there was a problem var rate was not well stated");
}else{
echo 'yay ,well stated!!!';
}
}else{
die("POST data missing!");
}
?>

Replace OR with && and best way to compare two strings in php is strcmp()
if(strcmp($rate,"applaud")!=0 && strcmp($rate,"boo")!=0)
{
die("Sorry there was a problem var rate was not well stated");
}
If two strings are equal then it will return 0. If you want to learn more about strcmp() then you can visit here

Related

vTiger Event Handler to check if record exists

I am working on vTiger 6.5 and I am trying to figure a way to see if a record exists in a custom module of mine. I want to check whether the 'policynumber' is new before saving, here is my code so far. For some reason it seems to act randomly depending on my module number chosen.
class isaHandler extends VTEventHandler {
function handleEvent($eventName, $entityData) {
global $adb;
$moduleName = $entityData->getModuleName();
if($moduleName=='isa'){
if($eventName == 'vtiger.entity.beforesave.modifiable') {
$isNew = $entityData->isNew('policynumber');
if ($isNew == false) {
echo "Duplicate policy number";
exit;
}
}
if($eventName == 'vtiger.entity.beforesave') {}}
if($eventName == 'vtiger.entity.beforesave.final') {
$price = $entityData->get('currentamount');
if($price > 20000){
echo "Please go back and enter less than 20000";
exit;
}
if($eventName == 'vtiger.entity.aftersave') {}
}
}
At the moment I am currently using an echo just to see the result. But later on I will perform more than this.
isNew()
Returns true if new record is being created, false otherwise.
More info is here
you should write a custom query to check policynumber already exist or not in your function:
if($eventName == 'vtiger.entity.beforesave.modifiable') {
global $adb;
$result = $adb->pquery("SELECT your-field-name FROM table_name WHERE policynumber=?", array($policynumbervalue));
if($result && $adb->num_rows($result)) {
echo "This policy number exist";
die();
}else{
// write your overwrite code
}
} //end if($eventName == 'vtiger.entity.beforesave.modifiable')
Update:
I am assuming there is field i.e. policynumber in your form, you enter some value in this field and submit the form. so you will get entered policy number value from this:
$policynumbervalue = $entityData->get('policynumber'); //this is vtiger standard way
if this does not work, you can simply use php global variable $_REQUEST['policynumber'] but I is not a good practice.
Hope this will help.
This is the update to my answer, I simply done an if statement on the number of rows displayed.
if($eventName == 'vtiger.entity.beforesave.modifiable') {
$policynumbervalue = $entityData->get('policynumber');
$sql = $adb->pquery("SELECT policynumber FROM vtiger_isa WHERE policynumber=?",array($policynumbervalue));
$nrows = $adb->num_rows($sql);
if($nrows > 0){
echo "<script type=\"text/javascript\">window.alert('ISA policy number already exists, you will be redirected to the updata module.');
window.location.href = '/vtigercrm/index.php?module=isa&view=List';</script>";
exit;
}

If statement working only until 1st else if [duplicate]

This question already has answers here:
The 3 different equals
(5 answers)
Closed 6 years ago.
I am trying to show a different navigation bar depending on a users authority. Only problem is that when i log on to the system it shows the first else if, regardless of the authority of the user. To ensure that the problem is in the loop i have tried switching the else ifs and the same happened. the code is in an external php file and i call the function in the top of each page. any suggestions ?
function checkAuth() {
session_start();
if(!isset($_SESSION['role'])) {
require_once('menu.php');
} else if ($_SESSION['role'] = "registered") {
require_once('regnav.php');
} else if ($_SESSION['role'] = "admin") {
echo "FDGFGFD";
require_once('adminnav.php');
}
}
Your issue is with this part: $_SESSION['role'] = "registered". The single = means you are assigning the value "registered" to variable $_SESSION['role'].
If you are evaluating to check something, you need to use == i.e. $_SESSION['role'] == "registered"
You'll have the same issue with the second elseif
You need to use a double = sign for any condition check. For any condition check in if or else if, you have to use == in the middle of the variables.
If you use only = that means it assigning the value in the $_SESSION['role']. Also you can use === for checking the value as well as the type of the variable.
Valid function is:
function checkAuth()
{
session_start();
if(!isset($_SESSION['role']))
{
require_once('menu.php');
}
else if ($_SESSION['role'] == "registered"){
require_once('regnav.php');
}
else if ($_SESSION['role'] == "admin"){
echo "FDGFGFD";
require_once('adminnav.php');
}
}
?>

Compare strings in PHP

I'm trying to compare a POST variable with a string. Can someone help me see what in my PHP code is not written correctly? I've tried both '==' and '==='. Thank you for your help.
$action = mysqli_real_escape_string($mysqli, $_POST['action']);
if(strcmp($action, "save") == 0){
//do stuff
}elseif(strcmp($action, "load") == 0){
//do other stuff
}else{
//do even more stuff
}
why not simply use
if ($_POST['action']=='save'){
}elseif($_POST['action']=='load'){
}
don't understand the mysql in this contenxt
Don't know why you want to do this, but try casting $aciton, like (string)$action.
== is used to see if the two sides of comparison are equal, while === is used to check to see if they're identical meaning they are equal AND of the same type.
As for your code, you should just be able to do
if($action == 'save'){
echo 'save';
}
elseif ($action == 'load'){
echo 'load';
}
else{
echo 'none';
}

php and $_GET array

I have submitted some code to the redirected url and now trying to use this to echo some information out but can't figure out where I am going wrong.
I have the following code:
<?php $login_attempt = mysql_real_escape_string($_GET['login_attempt']);
if ($login_attempt) == '1'{
return 'failed';
}
?>
all I want to do is if the url has $login_attempt=1 I want to return the message 'failed' to the page.
There is no point of escaping anything if it doesn't enter anywhere important (like a database).
<?php
if ($_GET['login_attempt'] == '1') {
echo 'failed';
}
?>
Also, you have a problem in your if statement, that's corrected in the code above. Be sure to include all of the condition inside of parenthesis, and not just one side of the equality check.
Also, if you wish to display something on the screen, you should echo it, not return.
how about:
if ($login_attempt == '1'){
echo 'failed';
}
Try this one. Your error in $login_attempt == '1':
<?php $login_attempt = mysql_real_escape_string($_GET['login_attempt']);
if ($login_attempt == '1'){
echo 'failed';
return false;
}
?>
As others already mentioned you have several problems but the syntax error comes from this:
if ($login_attempt) == '1'{
it should be
if ($login_attempt == '1') {
Dont u think if ($login_attempt) == '1' should be something like this ($login_attempt == '1') Sorry...many others also suggested this :P
At the first, I must tell you that you have a mistake in your IF condition. You typed == outside of ().
In addition, you have to be aware of status of setting your variable through your URL. Check the code below. In this code, I made a function to check the status. Default status is true, and we will check it just for a negative condition. I hope it could be useful for you:
<?php
function check() {
if (isset($_GET['login_attempt'])) {
$login_attempt = mysql_real_escape_string($_GET['login_attempt']);
if ($login_attempt == '1') {
return false;
} else {
return true;
}
} else {
return true;
}
}
if (!check()) echo('Error Message');
?>

Problem with Boolean Values in PHP

I've some problems with handling Boolean values in PHP. It is a validation script before storing data into database. I wrote a global validator that will validate and return a Boolean value whether the validation was successful .
Here is my code.
//VALIDATE
$isValid = true;
foreach($team as $key=>$val) {
if(!is_array($val)){
$isValid = $isValid && validate($val, $key);
}
}
for($it=0;$it<count($team['members']);$it++){
foreach($team['members'][$it] as $key=>$val) {
$isValid = $isValid && validate($val, $key);
}
}
if(!$isValid) { // EDITED: if(!isValid)
echo "validation error";
exit(1);
}
//END OF VALIDATE
The validate function is working properly but sometimes I end up getting $isValid = true or the other way, when I try with some test cases.
Hmm.. What am I doing wrong here ?
Please check, if this form does the trick:
if( false === $isValid) {
echo "validation error";
exit(1);
}
Note, that ( ! $isValid ) or (false == $isValid ) in some cases return results, which are at first look wrong. See for example the hint in the strpos() documentation.
In fact, the results are fine, since operations line ! or == try to cast operands in a 'useful' way.
That said, it's always better to user the === operator, since it checks values and types of operands. Please see operator overview.
if(!isValid) { falls back to if (!"isValid"), if there is no constant isValid. You probably meant if (!$isValid) {.
if(!isValid) {
isValid has no dolar, (you need to give variables in PHP some cash) so:
if(!$isValid) {
Source : http://bit.ly/1hxDmVR
Here is sample code for working with logical operators in PHP. Hope it will helpful:
<html>
<head>
<title>Logical</title>
</head>
<body>
<?php
$a=10;
$b=20;
if($a>$b)
{
echo " A is Greater";
}
elseif($a<$b)
{
echo " A is lesser";
}
else
{
echo "A and B are equal";
}
?>
<?php
$c=30;
$d=40;
//if(($a<$c)AND($b<$d))
if(($a<$c)&&($b<$d))
{
echo "A and B are larger";
}
if(isset($d))
$d=100;
echo $d;
unset($d);
?>
<?php
$var1=2;
switch($var1)
{
case 1:echo "var1 is 1";
break;
case 2:echo "var1 is 2";
break;
case 3:echo "var1 is 3";
break;
default:echo "var1 is unknown";
}
?>
</body>
</html>
I think the problem is that your $isValid variable can be changed many times in the loops and by the end of your code simply applies to the last value in your final loop.
You should set it to true initially and then only set it to false IF your validity check fails - not simply assign its value based on every single validity check.

Categories