Update statement not working - php

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'"

Related

php Function echo's wrong string

I'm fairly new to PHP so forgive me if this function is badly done.
I have a function:
function socialLink($sm_type = NULL) {
if ($sm_type = 'twitter') {
echo 'https://www.twitter.com';
} else {
echo 'https://www.facebook.com';
}
}
In my code when I call the function socialLink('facebook'); it echo's the Twitter URL.
Surely it should echo the Facebook URL since $sm_type would be equal to 'facebook' not twitter ?
Any help would be appreciated.
Set your if condition with this,
function socialLink($sm_type = NULL) {
if ($sm_type == 'twitter') {
echo 'https://www.twitter.com';
} else {
echo 'https://www.facebook.com';
}
}
See this.
function socialLink($sm_type = NULL) {
if ($sm_type == 'twitter') {
echo 'https://www.twitter.com';
} else {
echo 'https://www.facebook.com';
}
}
NOTE: Single = use to assign the value and = = use to compare values
Different's Between = , = = , = = =
= operator Used to just assign the value.
= = operator Used to just compares the values not datatype
= = = operator Used to Compare the values as well as datatype.
Your if statement does not use a comparison operator, it is an assignment (=). For a comparison, please use "==".
if ($sm_type == 'twitter') {
echo 'https://www.twitter.com';
} else {
echo 'https://www.facebook.com';
}
if ($sm_type == 'twitter') {
echo 'https://www.twitter.com';
} else {
echo 'https://www.facebook.com';
}
In php == is use for string comparison so, In this case you can't used = for that, simple :)

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

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
}

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....

Javascript; check if a variable is 1 or 0

I'm using a variable in Javascript which will be set via Php e.g. var usesInterview = <?php echo 1;?>
If not, then var usesInterview = <?php echo 0;?>
How best should I handle this in my code? There will be a If statement to check for the variable and determine the route to take.
I've tried using typeof() == 1 and when I set it to 0, it still carries out the routine as if it where 1.
Why not set it with javascript:
usesInterview = 1;
Even if you set it with PHP, you can check like this:
if (usesInterview === 1){
// variable is equal to 1
}
else if (usesInterview === 0){
// variable is equal to 0
}
Notice the === to check for both type as well as value. If you don't want to check for type, you need to use == like this:
if (usesInterview == 1){
// variable is equal to 1 or "1" or true
}
else if (usesInterview == 0){
// variable is equal to 0 or "0" or "" or false
}
You should avoid the later approach when you are sure about both type as well as value.
More Information:
http://w3schools.com/JS/js_comparisons.asp
There are so many ways you can do it... Ie
var usesInterview = <?php echo [0|1];?>
usesInterview ? goingTrueWay() : goingFalsegWay();
or
<?php echo [0|1];?> ? goingTrueWay() : goingFalseWay();
or something like this:
var waysCollection = {
0: function () {...} //routine for usesInterview == 0
1: function () {...} //routine for usesInterview == 1
}
waysCollection[<?php echo [0|1];?>]();
also you can use one of the early suggestion:
if (<?php echo [0|1];?>) {
// truthy branch
} else {
// falsy branch
}
BTW, if you want usesInterview to be a boolean, yes/no trigger, - use true/false not 0/1. Its easier to read and understand later. For ex
var usesInterview = <?php echo [false|true];?>
if (usesInterview) {
//do this if `true`
} else {
//do this if `false`
}
typeof will return the type of the value - "number" in this case. You're using a non-strict equality check (==) so "number" == 1 is true.
Just check the value, using type-strict equality operator (===):
if (usesInterview === 1) {
// do something
}
else if (usesInterview === 0) {
// do something else
}
Read more about JavaScript comparison operators at https://developer.mozilla.org/en/JavaScript/Reference/Operators/Comparison_Operators.
When usesInterview is 1 it's truthy. So it's as simple as:
if (usesInterview) {
// truthy branch
} else {
// falsy branch
}

How to suppress the "Division by zero" error and set the result to null for the whole application?

How to suppress the "Division by zero" error and set the result to null for the whole application? By saying "for the whole application", I mean it is not for a single expression. Instead, whenever a "Division by zero" error occurs, the result is set to null automatically and no error will be thrown.
This should do the trick.
$a = #(1/0);
if(false === $a) {
$a = null;
}
var_dump($a);
outputs
NULL
See the refs here error controls.
EDIT
function division($a, $b) {
$c = #(a/b);
if($b === 0) {
$c = null;
}
return $c;
}
In any place substitute 1/0 by the function call division(1,0).
EDIT - Without third variable
function division($a, $b) {
if($b === 0)
return null;
return $a/$b;
}
Simple as.. well abc*123-pi
$number = 23;
$div = 0;
//If it's not 0 then divide
if($div != 0)
$result = $number/$div;//is set to number divided by x
}
//if it is zero than set it to null
else{
$result = null;//is set to null
}
As a function
function mydivide($divisior, $div){
if($div != 0)
$result = $divisor/$div;//is set to number divided by x
}
//if it is zero than set it to null
else{
$result = null;//is set to null
}
return $result;
}
Use it like this
$number = mydivide(20,5)//equals four
I can't think of a way to set it whenever there's division but I'd use the function and rename it to something like "d" so it's short!
This is a horrible solution, but thankfully, you won't use it because the variable is set to false instead of null.
function ignore_divide_by_zero($errno, $errstring)
{
return ($errstring == 'Division by zero');
}
set_error_handler('ignore_divide_by_zero', E_WARNING);
In your case, I'd create a function that does your division for you.
What about using a ternary operator, like so:
$a = $c ? $b/$c : null;

Categories