Inspite of 'if' command,selecting values only from school id 1 - php

By using below mentioned commands,if school id is 1 or 3,these code only picking values from if ($sch = "1")......
$xyz5 = mysql_query("select id from schools where id='UserSchool'");
$xyz6 = mysql_fetch_row($xyz5);
$sch = $xyz6[0];
if ($sch = "1") {
$xyz = mysql_query("select phone from students where student_id='$student_id'");
$xyz2 = mysql_fetch_row($xyz);
$pno = $xyz2[0];
$sql = "INSERT INTO sms_fees (STUDENT_ID,SCHOOL_ID,MESSAGE,assigned_date,phone)
values('".$student_id."', '".UserSchool()."', '".str_replace("\'","'
'",$_REQUEST['
MESSAGE '])."', '".DBDate()."', '".$pno."')
";
DBQuery($sql);
}
elseif($sch = "3") {
$xyz1 = mysql_query("select phone from students where student_id='$student_id'");
$xyz12 = mysql_fetch_row($xyz1);
$pno1 = $xyz12[0];
$sql = "INSERT INTO sms_fees (STUDENT_ID,SCHOOL_ID,MESSAGE,assigned_date,phone)
values('".$student_id."', '".UserSchool()."', '".str_replace("\'","'
'",$_REQUEST['
MESSAGE '])."', '".DBDate()."', '".$pno1."')
";
DBQuery($sql);
}

It will never matches to 1. Please see your query
$xyz5=mysql_query("select id from schools where id='UserSchool'");
You are selecting id where id='UserSchool'
So the below code never matches to "1"
if ($sch = "1") {
Correct your query first.

You must use double equal sign! Your conditions are assignments and are always evaluating to true (non empty strings), so conditions other than first are ignored.
Change it to that:
if ($sch == "1") {
Or even to that if you know your variable is always string not int:
if ($sch === "1") {

You need to use "=="
if ($sch == "1") {
if ($sch == "3") {
The reason why it is entering first if condition is because you are assigning 1 to $sch and if evaluates to true, hence the control enters the first if condition

Please try this :
if ($sch == "1")
{
// do something
}
elseif($sch == "3")
{
// do something
}
else
{
// do something
}

Related

How to perform condition when null is retrieved from the database?

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

Function returning incorrect response

I've got an issue with a function where it's returning data from the first if() statement as opposed to the secondary if() statement in a function, please see below.
function training($type,$tid)
{
if($type = "id") {
$query = mysql_query("SELECT * FROM trainroster WHERE DATE = CURDATE()");
if (mysql_num_rows($query) == 1) {
$array = mysql_fetch_array($query);
$id=$array['TID'];
}
else { $id = "1"; }
return $id;
}
else if($type = "topic"){
$query = mysql_query("SELECT * FROM trainroster WHERE TID='$tid'");
$array = mysql_fetch_array($query);
$topic = $array['TOPIC'];
return $topic;
}
}
Which is being called like this:
$training = $connection->training("topic",$row['TID']);
When the function is called it's returning the $id as opposed to the $topic even though I'm setting the $type variable to "topic".
Any help greatly appreciated, cheers!
use '==' instead of '=' to compare
'=' is an assigning operator, if you use it to compare, it will always return true. That's why you are getting id always.
if($type == "id") {
....
if($type == "topic"){
....
if($type = "id") assigns the string "id" to $type and returns "id" which is true.
As mentioned by Harish Singh you have to write if($type == "id").
A way to prevent this error is to write if("id" == $type) which results in an error if you forget one "=".
Look at your if statements:
if($type = "id")
if($type = "topic")
You're assigning, not comparing. They should be:
if($type == "id")
if($type == "topic")

If Post values equal other than other post values echo"failed";?

I have two combo boxes, one to report scores and one to set who scored the goals, How can i make it so if on post['submit']
If $_POST['Score1'] and $_POST['Score2']
is other than equal to
$_POST['homegoalscorer1'] and $_POST['awaygoalscorer1']
then echo"fail";
Something like;
if(isset($_POST['submit']))
{
$homescore = $_POST['Score1'];
$awayscore = $_POST['Score2'];
$homegoalscorer = $_POST['homegoalscorer1'];
$awaygoalscorer = $_POST['awaygoalscorer1'];
if '$homescore' + '$awayscore' != $homegoalscorer + $awaygoalscorer {
echo "failed";
}
else {
}
}
Any ideas?
Single quotes on a variable will turn that variable intro a string without execution. Also you forgot to add brackets:
if(isset($_POST['submit'])) {
$homescore = (float)$_POST['Score1'];
$awayscore = (float)$_POST['Score2'];
$homegoalscorer = (float)$_POST['homegoalscorer1'];
$awaygoalscorer = (float)$_POST['awaygoalscorer1'];
if (($homescore+$awayscore) != ($homegoalscorer+$awaygoalscorer)) {
echo "failed";
} else {
}
}
Use some brackets in your if statement to force the conditional setting in the correct context - and why are you encapsulating your variables in single quotes?
if (($homescore + $awayscore) != ($homegoalscorer + $awaygoalscorer))
{
// Your code continues....

If / else if / else with query to a database resulting false.

I have a select box with an option for All, and then a list of users.
What I'm struggling with is creating something like this. I have most of it except trying to query the database to for it to check if the variable is in the database.
if ($variable == 'All') { code here }
else if ($variable != 'ALL' != *[result in database]*) { code here }
else { code here }
I have most of it except trying to query the database to for it to check if the variable is in the database.
Any suggestions how I can encorporate a query of a mySQL database in my if statement.
Thanks
if ($variable == 'All') {
... do something ...
} else {
$sql = "SELECT ...";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
if ($row['somefield'] == 'whatever') {
... do something else ...
} else {
... do something even "elser" ...
}
}
You can't use operators like that.
Replace:
else if ($variable != 'ALL' != *[result in database]*) { code here }
With:
else if ($variable != 'ALL' AND $variable != *[result in database]*) { code here }

Update statement not working

function add_new($father,$chName) // add new category
{
if($father = "1" ) {
$result = mysql_query("INSERT into stinky_menu (title,nest_under)
VALUES('".$chName."','1')");
}
else {
$result = mysql_query("UPDATE stinky_menu SET title = '$chName' nest_under = '$father'");
}
}
I am getting the value of father from parent page, but its not going to else condition if its not equal to one.
You’re using the assignment operator = rather than the comparison operator ==. So try this:
if ($father == "1") {
// …
} else {
// …
}
That's because you have
if($father = "1")
You need to use "==". "=" is the assignment operator. You are setting $father equal to "1" even when it isn't.
Try:
if ($father == 1){}
Read here about comparison operators. "=" is the assignment operator.
Look at this to see what your code does:
<?php
$father = 55;
if ($father = 1){}
else{}
echo $father;
?>
This prints "1".
Also, should not that last query be:
"UPDATE stinky_menu SET title = '$chName', nest_under = '$father'"

Categories