Jump to content

will this if/elseif/else statement work? [RESOLVED]


Recommended Posts

I am trying to make 3 comparisons and depending on what text fields have in them dpenedswhich SQL update to use.  But will this code work?

//if $event and $row['eventdate'] are the same and $pic and $row['photo'] are the same don't update the database and change their values but update the rest

    if (strcmp( $event,$row['eventdate'] ) =0) && (strcmp( $pic,$row['photo'] ) =0) {
        $query="UPDATE events SET title='$name', content='$message' WHERE id='$ud_id'";
        mysql_query($query);
    }

//if $event and $row['eventdate'] don't match and $pic and $row['photo'] are the same don't update $row['photo'] but update the rest

    elseif (strcmp( $event,$row['eventdate'] ) !=0) && (strcmp( $pic,$row['photo'] ) =0) {
        $query="UPDATE events SET title='$name', eventdate='$event', content='$message' WHERE id='$ud_id'";
        mysql_query($query);
    }

//if $event and $row['eventdate'] are the same and $pic and $row['photo'] don't match don't update $row['eventdate'] but update the rest

    else (strcmp( $event,$row['eventdate'] ) =0) && (strcmp( $pic,$row['photo'] ) !=0) {
        $query="UPDATE events SET title='$name', photo='$pic', content='$message' WHERE id='$ud_id'";
        mysql_query($query);
    }
example 1
elseif is the same as else if ok.
[code]
<?php

if(condition){

elseif(condition){

else if(condition){

}else{
(condition);
}
?>
[/code]

example 2
[code]
<?php

if(condition){

do somethink

}else{

not done

}
?>
[/code]


example 3

[code]
<?php

if(conditon){

if(condition){

do some think if the two condition true

}
}else{

if failed

}

?>
[/code]
try this

[code]<?php
    if (strcmp($event,$row['eventdate']) == 0 && strcmp($pic,$row['photo']) == 0) {
        $query="UPDATE events SET title='$name', content='$message' WHERE id='$ud_id'";
        mysql_query($query);
    }

    else if (strcmp($event,$row['eventdate']) !=0 && strcmp($pic,$row['photo']) == 0) {
        $query="UPDATE events SET title='$name', eventdate='$event', content='$message' WHERE id='$ud_id'";
        mysql_query($query);
    }

    else if(strcmp($event,$row['eventdate']) == 0 && strcmp($pic,$row['photo']) !=0) {
        $query="UPDATE events SET title='$name', photo='$pic', content='$message' WHERE id='$ud_id'";
        mysql_query($query);
    }
else (){
}
?>[/code]
please also read this ok.

[code]

It's definitely worth noting that the return-values of strcmp() when used for i.e. password-checking is the oposite of that of the ==-operator.
<?php

// I.e.:

$pw1 = "yeah";
$pw2 = "yeah";

if (strcmp($pw1, $pw2)) {  // This returns false.
  // $pw1 and $pw2 are NOT the same.
} else {
  // $pw1 and $pw2 are the same.
}

//Where the use of the == operator would give us.:
if ($pw1==$pw2) {    // This returns true.
  // $pw1 and $pw2 are the same.
} else {
  // $pw1 and $pw2 are NOT the same.
}

//Additionally, to check if $pw1 and $pw2 are of the same type you can use the === operator.

<?

[/code]
my version
[code]
<?php
    if ((strcmp($event,$row['eventdate']=='0'))&&(strcmp($pic,$row['photo']) == '0'))) {
        $query="UPDATE events SET title='$name', content='$message' WHERE id='$ud_id'";
        mysql_query($query);
    }

    else if ((strcmp($event,$row['eventdate'] !='0')) && (strcmp($pic,$row['photo']) == '0'))) {
        $query="UPDATE events SET title='$name', eventdate='$event', content='$message' WHERE id='$ud_id'";
        mysql_query($query);
    }

    else if((strcmp($event,$row['eventdate'] == '0' ))&& (strcmp($pic,$row['photo']) !='0'))) {
        $query="UPDATE events SET title='$name', photo='$pic', content='$message' WHERE id='$ud_id'";
        mysql_query($query);
    }
else (){
}
?>

[/code]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.