Jump to content

if else elseif


infrabyte

Recommended Posts

Hi Guys,

 

I'm not sure what to do here. I have a variable that is passed on via a url. I have a database table that has a field called 'publish'. This field contains either a Yes or a No.

All I want to do is check the field an if it is Yes then make it No, and likewise if it is No then make it Yes.

Here is my code. It changes one way but not the other. The value of $publish seems to stay the same...please help...Thank you.

 

<?php

// connect to the database
include('connect-db.php');

// get id value
$id = $_GET['id'];
echo "$id";
$publish = $_GET['publish'];
echo "$publish";

$sql="SELECT * FROM events WHERE ID='$id'";
$result=mysql_query($sql);

if ($publish='Yes') {
   $publish=='No';
} else {
   $publish=='Yes';
}

// echo "<p>$publish</p>";

$sqltwo = "UPDATE events SET publish='$publish' WHERE ID='$id'";
$resulttwo = mysql_query($sqltwo);

// sleep(2);
// header("Location: beacon.php");

?>

Link to comment
https://forums.phpfreaks.com/topic/237346-if-else-elseif/
Share on other sites

By the way, you can put an If/Else within the query itself. I don't even see what the purpose is of the first query in the code you provided.

 // connect to the database
include('connect-db.php');

// get id value
$id = $_GET['id'];
echo "$id";
$publish = $_GET['publish'];
echo "$publish";

$sqltwo = "UPDATE events
           SET publish = IF('$publish'='Yes', 'No', 'Yes')
           WHERE ID='$id'";
$result=mysql_query($sql);

 

Also, I would advise using the values "Yes"/"No". They mean nothing programatically as they are just strings. For boolean values you should be using 1 and 0. Otherwise you are always having to convert the strings too true/false.

Link to comment
https://forums.phpfreaks.com/topic/237346-if-else-elseif/#findComment-1219645
Share on other sites

...

Also, I would advise using the values "Yes"/"No". They mean nothing programatically as they are just strings.

+1... ^^

 

.....For boolean values you should be using 1 and 0. .

 

and in that case the UPDATE is simply:

$sqltwo = "UPDATE events
           SET publish = !publish
           WHERE ID='$id'";

Link to comment
https://forums.phpfreaks.com/topic/237346-if-else-elseif/#findComment-1219649
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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