-1

I am trying change one column value with a link, like if the column value is published it will changed to unpublished and vice versa. But the problem is that only if condition is working not else if.

Here is the link:

echo "<td><a href='testi.php?change_status={$testi_id}&status={$testi_status}'>Publish / Unpublish</a></td>";

And the php code to change the status:

<?php 
if(isset($_GET['change_status'])){
    $the_status =  escape($_GET['status']);
    $the_testi_id = escape($_GET['change_status']);


    if  ($the_status='published'){
    $query = "UPDATE testimonial SET testi_status = 'unpublished' WHERE testi_id = $the_testi_id   ";
    $change_to_sub_query = mysqli_query($connection, $query); }

   else if ($the_status='unpublished'){
        $query = "UPDATE testimonial SET testi_status = 'published' WHERE testi_id = $the_testi_id   ";
        $change_to_sub_query = mysqli_query($connection, $query); 

    }

    header("Location: testi.php");



}  ?>
Iam Srkfan
  • 197
  • 9

1 Answers1

2

Single = sets, double == checks, triple === checks explicitly.

When you do

if ($value='foo') {}

What it is doing is checking if $value was set to 'foo' which will always evaluate to true.

So you want if ($value == 'foo') instead.

In what you have written, it will never evaluate the elseif because if will always be true.


For reference, the difference between == and ===

//evalates to true
if (1 == true) { ... }

//evaluates to false
if (1 === true) { ... }

//evaluates to true
if (1 === 1) { ... }

//evaluates to true
if (true === true) { ... }
mopsyd
  • 1,877
  • 3
  • 20
  • 30