I am retrieving the details from the database if the condition is satisfied.
But when the condition is not satisfied, empty result is retrived.
So how to have a condition if status is empty
$sq = mysqli_query($link, $query);
$ro = mysqli_fetch_array($sq);
$status = $ro['status'];
if ($status == 1) {
header("location: paymentPage.php");
} elseif ($status == 0) {
header("location:login.php");
} elseif ($status == '\0') {
header("location:SignUp.php");
}
But I am not able to redirect to signup page if the status is empty.Is status=='\0' is correct to go for signup page.
If your column status has integer type in DB then it can't have value '\0' because '\0' is a string.
Maybe you should use is_null function?
For example:
else if(is_null($status))
{
header("location:SignUp.php");
}
In all case, mysqli_fetch_array function will return array|null|false types. It means that, if your connection didn't establish or data is empty, your response will return empty array or empty string|boolean item. Therefore, you can check if is exits by any case like that:
if( !($row = mysqli_fetch_array($sq)))
{
// Data doesn't exists
header("location:SignUp.php");
}else{
$status = $ro['status'];
echo 'My other validations';
}
To read documentation about function look at: https://www.php.net/manual/tr/mysqli-result.fetch-array.php
Related
I have written this php code
//check if user has active plan and search keyword is not empty
if (!empty($request['advertisername']) && ($userSubscriptionType == env('AMEMBER_STD_PLAN_CODE') || $userSubscriptionType == env('AMEMBER_PRE_PLAN_CODE'))) {
$advertisername = 'LOWER(post_owner) like "%' . strtolower($request["advertisername"]) . '%"';
} else {
//if search keyword is null, means page is loaded for first time
if (!isset($request['advertisername'])) {
$advertisername = "true";
} else {//if search keyword is not null, and user has not active plan
$response->code = 400;
$response->msg = "Permission issues";
$response->data = "";
return json_encode($response);
}
}
Here $request['advertisername']=null coming from fronend. So this condition will be true if (!isset($request['advertisername'])) {. But unfortunately it's always going to last else block.
If i save $request['advertisername'] in a variable like this. Then this condition if (!isset($advertiserName)) { becomes true.
//check if user has active plan and search keyword is not empty
if (!empty($request['advertisername']) && ($userSubscriptionType == env('AMEMBER_STD_PLAN_CODE') || $userSubscriptionType == env('AMEMBER_PRE_PLAN_CODE'))) {
$advertisername = 'LOWER(post_owner) like "%' . strtolower($request["advertisername"]) . '%"';
} else {
//if search keyword is null, means page is loaded for first time
$advertiserName=$request['advertisername'];
if (!isset($advertiserName)) {
$advertisername = "true";
} else {//if search keyword is not null, and user has not active plan
$response->code = 400;
$response->msg = "Permission issues";
$response->data = "";
return json_encode($response);
}
}
I checked the condition via var_dump(). Anyone can explain why it is happening?
you can use is_null() instead of !isset().
You can use array_key_exists() instead:
if (!array_key_exists('advertisername', $request)) {
Please note the PHP manual for isset() says:
isset — Determine if a variable is set and is not NULL
A good way would be to check if the variable is not empty as it checks both isset and null
eg if(!empty($your_variable) { /** your code here **/ }
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;
}
So I made page with unban request and in users table I save if user already sent request or not so I don't have multiple unban requests from one user.Now when I check if user sent request it's not working.In database it stands 0 and it's still showing me error pop out.
Here is code, thanks for help in advance
if(isset($_POST['btn-unban_req']))
{
if($unban_sent = 0)//THIS IS WHERE I CHECK
{
//MY THIGNS HERE
if($connection ->query($unbanquery) === TRUE)
{
//MY THIGNS HERE
if ($connection->query($sentquery) === TRUE)
{
}
else
{
echo $connection->error;
}
}
else
{
}
}
else // AND I GET THIS ERROR EVEN IF IT STANDS 0 IN DATABASE
{
echo "Unban already sent!";
}
}
You are not comparing the values, this is assigning the values:
if($unban_sent = 0) // assigning values
This should be:
if($unban_sent == 0) // comparing values
Basic Example:
Lets say,
1 = 1 its assigning
1 == 1 its checking the condition will return TRUE.
For more help: PHP Comparison Operators
You're using this code which is wrong.
$unban_sent = 0
$unban_sent = 0 means to assign 0 to $unban_sent
It should be:
$unban_sent == 0
$unban_sent == 0 means $unban_sent is equal to 0
== is for comparison, = is for assignment, and === is for identical or same type.
More information at http://php.net/manual/en/language.operators.comparison.php.
In line 3 you missed a = for comparision. Instead, you set $unban_set to 0
Try it with this code:
if(isset($_POST['btn-unban_req']))
{
if($unban_sent == 0) //<- Now you are checking it here
{
//MY THIGNS HERE
if($connection ->query($unbanquery) === TRUE)
{
//MY THIGNS HERE
if ($connection->query($sentquery) === TRUE)
{
}
else
{
echo $connection->error;
}
}
else
{
}
}
else // AND I GET THIS ERROR EVEN IF IT STANDS 0 IN DATABASE
{
echo "Unban already sent!";
}
}
I am trying to GET different rows from different columns in php/mysql, and pack them into an array. I am able to successfully GET a jason encoded array back IF all values in the GET string match. However, if there is no match, the code echos 'no match', and without the array. I know this is because of the way my code is formatted. What I would like help figuring out, is how to format my code so that it just displays "null" in the array for the match it couldn't find.
Here is my code:
include '../db/dbcon.php';
$res = $mysqli->query($q1) or trigger_error($mysqli->error."[$q1]");
if ($res) {
if($res->num_rows === 0)
{
echo json_encode($fbaddra);
}
else
{
while($row = $res->fetch_array(MYSQLI_BOTH)) {
if($_GET['a'] == "fbaddra") {
if ($row['facebook'] === $_GET['facebook']) {
$fbaddr = $row['addr'];
} else {
$fbaddr = null;
}
if ($row['facebookp'] === $_GET['facebookp']) {
$fbpaddr = $row['addr'];
} else {
$fbpaddr = null;
}
$fbaddra = (array('facebook' => $fbaddr, 'facebookp' => $fbpaddr));
echo json_encode($fbaddra);
}
}
}
$mysqli->close();
UPDATE: The GET Request
I would like the GET request below to return the full array, with whatever value that didn't match as 'null' inside the array.
domain.com/api/core/engine.php?a=fbaddra&facebook=username&facebookp=pagename
The GET above currently returns null.
Requests that work:
domain.com/api/core/engine.php?a=fbaddra&facebook=username or domain.com/api/core/engine.php?a=fbaddra&facebookp=pagename
These requests return the full array with the values that match, or null for the values that don't.
TL;DR
I need assistance figuring out how to format code to give back the full array with a value of 'null' for no match found in a row.
rather than assigning as 'null' assign null. Your full code as follows :
include '../db/dbcon.php';
$res = $mysqli->query($q1) or trigger_error($mysqli->error."[$q1]");
if ($res) {
if($res->num_rows === 0)
{
echo json_encode('no match');
}
else
{
while($row = $res->fetch_array(MYSQLI_BOTH)) {
if($_GET['a'] == "fbaddra") {
if ($row['facebook'] === $_GET['facebook']) {
$fbaddr = $row['dogeaddr'];
//echo json_encode($row['dogeaddr']);
} else {
$fpaddr = null;
}
if ($row['facebookp'] === $_GET['facebookp']) {
$fbpaddr = $row['dogeaddr'];
//echo json_encode($row['dogeaddr']);
} else {
$fbpaddr = null;
}
$fbaddra = (array('facebook' => $fbaddr, 'facebookp' => $fbpaddr));
echo json_encode($fbaddra);
}
}
}
$mysqli->close();
You can even leave else part altogether.
Check your code in this fragment you not use same names for variables:
if ($row['facebook'] === $_GET['facebook']) {
$fbaddr = $row['dogeaddr'];
//echo json_encode($row['dogeaddr']);
} else {
$fpaddr = 'null';
}
$fbaddr not is same as $fpaddr, this assign wrong result to if statement.
It was the mysql query that was the problem.
For those who come across this, and need something similar, you'll need to format your query like this:
** MYSQL QUERY **
if ($_GET['PUTVALUEHERE']) {
$g = $_GET['PUTVALUEHERE'];
$gq = $mysqli->real_escape_string($g);
$q1 = "SELECT * FROM `addrbook` WHERE `facebookp` = '".$gq."' OR `facebook` = '".$gq."'";
}
** PHP CODE **
if($_GET['PUTVALUEHERE']{
echo json_encode($row['addr']);
}
I have this function that will check if a user already exists in the DB or not:
function checkTwitterAccount($user_id) {
$accountExists = false;
$a = "SELECT * FROM twitterAccounts WHERE user_id='".$user_id."'";
$ar=mysql_query($a) or die("Error selecting twitter account: ".mysql_error());
$ac = mysql_num_rows($ar);
if ($ac > 0) {
$accountExists = true;
}
return $accountExists;
}
Now when I call this function how do I actually know if the user is or is not in the DB?
I mean how do I call this function and how do I check the result?
Is the below right?
If (checkTwitterAccount($user_id) = true) {
DO THIS
}else{
DO THAT
}
Please help me I am new to it.
Thanks
if (checkTwitterAccount($user_id)) { //true
//do something
} else { //false
//do something
}
if (checkTwitterAccount($user_id) == true) {
//do this
}
else {
//do that
}
You have to use == rather than = as the = operand sets the value to true in the code you wrote, whereas the == operand compares the returned value to true.
Since your returning a true or false value you can simply use:
If (checkTwitterAccount($user_id)) {
//DO THIS
}else{
//DO THAT
}
Note: that your original line:
If (checkTwitterAccount($user_id) = true) {
would result in an assignment error because a single "=" means assign a value which can't be done to a function. You wanted:
If (checkTwitterAccount($user_id) == true) {
because "==" compares a value. Further, == only compares the value so for example 0 is the compares positively with false, any number compares positively with true, if you want to also compare type you us "===" like this:
0 == false //true
0 === false //false
false == false //true
false === false //true
1 == true //true
1 === true //false
true == true //true
true === true //true
function checkTwitterAccount($user_id) {
$user_id = intval($user_id);
$a = "SELECT `user_id` FROM `twitterAccounts` WHERE `user_id` = '".mysql_real_escape_string($user_id)."'";
$ar = mysql_query($a) or die("Error selecting twitter account: ".mysql_error());
$ac = mysql_num_rows($ar);
return ($ac > 0);
}
if(checkTwitterAccount($someid)) {
// Exists...
} else {
// No such ID in the DB
}
Note that comparison operator is == not = (which is assign).
So you could do:
if(checkTwitterAccount($someid) == true) {
However, it isn't necessary here.
Also remember to sanitize the data in the query.
if (checkTwitterAccount($user_id) == true){
do something if its true
} else {
do something if its flase
}
should work.. given that you provide argument to that function which seems to be
the int.. or id number from id column from users table in the db.
Basically you have a HTML Form that takes in a username and checks the database
for that users id number in users table in the database. Once it has this number it will
pass it on to the function checkTwitterAccount($user_id) if that function returns True that means guessing by the name of the function that the user has a twitter account else he does not have one.
you could do:
if (checkTwitterAccount($user_id) == true){
echo "This user has a twitter account";
} else {
echo "This user does not have a twitter account";
}
You can shorten the orig. function.
function checkTwitterAccount($user_id) {
$a = "SELECT * FROM twitterAccounts WHERE user_id='" . $user_id . "'";
$ar = mysql_query($a) or die("Error selecting twitter account: " . mysql_error());
return mysql_num_rows($ar) > 0; // boolean (will be true or false)
}
Then use the answer from max_. (See comparison operators)