PHP - IF ELSE ELSE Help (NULL NOT NULL etc)

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
phpnickster
New php-forum User
New php-forum User
Posts: 3
Joined: Wed Jan 08, 2020 5:04 am

Wed Jan 08, 2020 5:28 am

I am having trouble with my if than statement and would appreciate some help with interpreting the correct values

DEFAULT for an empty table would be the following:
col type DEFAULT
1 id none AUTO_INCREMENT
2 Team1 int(2) 0
3 Team2 int(2) 0
4 players1 varchar(100) NULL
5 players2 varchar(100) NULL
6 Details varchar(100) NULL
7 confirm1 int(1) 0
8 confirm2 int(1) 0

what i'm looking for as far as an if else else:

If $players1 is NULL and $confirm equals 0 {

} elseif $players1 is not NULL and $confirm1 equals 1 and $players2 is NULL and $confirm2 equals 0 {

} else {

}

Code: Select all

if(is_null($players1) and $confirm1 == NULL) {

First INSERT SQL....

} elseif ($players1 !== NULL and $confirm1 === 1 and !isset($players2) and $confirm2 == NULL) {

UPDATE SQL INSERTED ROW

} else { 

INSERT SQL for any other rows

}

I'm pretty sure the code is just defaulting to the ELSE and i cant seem to get the correct combo of values in the IF ELSEIF ELSE Statment thanks in advance

Nick
chorn
php-forum GURU
php-forum GURU
Posts: 654
Joined: Fri Apr 01, 2016 2:18 am

Wed Jan 08, 2020 6:37 am

And what do you expect? What do you define as "wrong"? Which input did you use?

Just write a test: https://3v4l.org/tg5UQ
phpnickster
New php-forum User
New php-forum User
Posts: 3
Joined: Wed Jan 08, 2020 5:04 am

Wed Jan 08, 2020 7:51 am

The values from the database table

In 1 case would be no rows ie start of insert
2nd case when half of the row is inputted as there are 2 parts to the trade and 3rd is after the first trade ever to insert by a new Id and then just update the subsequent 2nd parts of the trades in the previous row and onward and forward
chorn
php-forum GURU
php-forum GURU
Posts: 654
Joined: Fri Apr 01, 2016 2:18 am

Thu Jan 09, 2020 12:58 am

still no clue what's wrong / what you want to achieve. you did not answer all questions. i've posted a direct translation to working, tested code from your description.
phpnickster
New php-forum User
New php-forum User
Posts: 3
Joined: Wed Jan 08, 2020 5:04 am

Thu Jan 09, 2020 3:34 am

Figured it out with your example on a test page that i altered slightly to output the values of the Database at each point of the trade process

the final IF THEN ELSE Statement is

Code: Select all


if(empty($id) and empty($players1) and empty($confirm1)) {

$sql = "INSERT INTO transactions (id, Team1, players1, confirm1)

} elseif ($players1 != NULL and $team2 == 0 and empty($players2)) {

"UPDATE transactions SET Team2=?, players2=? WHERE id=?"

} else { 

$sql = "INSERT INTO transactions (id, Team1, players1, confirm1)

}


works perfectly now thanks for your help - the php file that is processed by the form wasn't outputting anything so this side test page was a great idea - if u want me to post that i can
Post Reply