Active and Inactive

Asked by At

Hello i have code for active and inactive image which is link to a database tables filed called status.

Now when ever i m clicking to active or Inactive a record so its not working.

Here is the code:

index.php

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
<?PHP

$user_name = "root";
$password = "";
$database = "tariq";
$server = "localhost";

$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {

$SQL = "SELECT * FROM active";
$result = mysql_query($SQL);

while ( $db_field = mysql_fetch_assoc($result) ) {

if($db_field['status'] == 'Active')
{
    echo "<a href='activate.php?status= Inactive' ><img src =  'inactive.jpg' /></a>";
}
else
{
        echo "<a href='activate.php?status= Active' ><img src =  'active.jpg' /></a>";
}
}

mysql_close($db_handle);

}
else {

print "Database NOT Found ";
mysql_close($db_handle);

}

?>
    </body>
</html>

Active .php

<?php
$status =  $_GET['status'];
$con=mysqli_connect("localhost","root","","tariq");
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
if($status == 'Active')
{
    mysqli_query($con,"UPDATE `active` SET `status` = 'Inactive'");
}
else
{
    mysqli_query($con,"UPDATE `active` SET `status` = 'Active'");
}
header('location:tariq.php');
mysqli_close($con);
?> 

3 Answers

2
Abhik Chakraborty On

You have a space in

<a href='activate.php?status= Inactive' >
                             ^.............here

In the browser when you click the link it will look like status=%20Inactive

and

<a href='activate.php?status= Active' >
                             ^.............here

Remove the space as

<a href='activate.php?status=Inactive' >
<a href='activate.php?status=Active' >

And always trim() the GET values as

$status =  trim($_GET['status']);

Also sanitize the GET values with mysqli_real_escape_string()

1
Dhaval Umraliya On

check your Page Name active or activate ??

1
Coyote Crafty On

Create a active and unactive link fetch the user id of the user from database using while loop:

$sql="SELECT * FROM users order by u_id desc";
$query=mysqli_query($db,$sql);

while($rows=mysqli_fetch_array($query))
{
<a href="status.php?u_id='.$rows['user_id'].'&active=1"  >Active</a>
<a href="status.php?u_id='.$rows['user_id'].'&unactive=0"  >un-Active</a>                                                                                                
 }

create status.php :

<?php
$u_id =  $_GET['u_id'];
$active =  $_GET['active'];
$unactive =  $_GET['unactive'];
include "connection/connect.php";


if($active == 1)
{
mysqli_query($db,"UPDATE `users` SET `status` = '1' where u_id='$u_id'");
}
else
{
 mysqli_query($db,"UPDATE `users` SET `status` = '0' where u_id='$u_id'");
}
  header('location:dashboard.php');
  mysqli_close($db);
 ?> 

This will give you and idea hope its help!