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
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
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
Share on other sites

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.