如何更改PHP的状态?

时间:2019-06-25 06:00:44

标签: php html mysql

下午好,当我单击非活动更改以激活此工作时,但我单击活动更改为非活动无法工作 My Database

 <?PHP //connection database 
  require_once("config.php"); ?>

 <?PHP 
//change active or Inactive
if (isset($_GET['cat_id'])){$sql = mysqli_connect('localhost','root','','ocall');$rs = mysqli_query($sql,"SELECT * FROM category WHERE id='$_GET[cat_id]'");$rows=mysqli_fetch_array($rs);if($rows==1){ $stt=0;} else{ $stt=1; }$sql4 = mysqli_connect('localhost','root','','ocall');
$sqlup = mysqli_query($sql4,"UPDATE category SET status ='$stt' WHERE id='$_GET[cat_id]'");}?>

<?PHP //delete 
if (isset($_GET['delete_id'])){$sql2 = mysqli_connect('localhost','root','','ocall');$sqld = mysqli_query($sql2,"DELETE FROM category WHERE id ='$_GET[delete_id]'");}?>

<!doctype html><html><head></head><body><h2>Category Control</h2><form action="" method="post"><br>Category Name:<input type = "text" name="category"><br>Short Description:<textarea name="shortdescription"></textarea><br>status:<select name="status"><option value="1">Active</option>
<option value="0">Inactve</option><input type ="submit" name="submit" value="Submit"></form>

<?PHP if (isset($_POST['submit'])){$sql3 = mysqli_connect('localhost','root','','ocall');$sqli = mysqli_query($sql3,"INSERT category SET name ='$_POST[category]',
status ='$_POST[status]',short_description= '$_POST[shortdescription]'");}?>

<?PHP $sqlselect=mysqli_connect('localhost','root','','ocall');$rsc=mysqli_query($sqlselect,"SELECT * FROM category ORDER BY id DESC");$count=mysqli_num_rows($rsc);if($count>0){?>

<table class="table table-hover"><thead><tr><th>ST.</th><th>Name</th<th>Short Description</th><th>Status</th><th>Action</th></tr></thead>

<?PHP $i=0;while($rowc=mysqli_fetch_array($rsc)){ $i++ ?>

<tr><td><?PHP echo $i;?></td><td><?PHP echo $rowc['name'];?></td><td><?PHP echo $rowc['short_description'];?></td><td><a href="tindex.php?cat_id=<?PHP echo $rowc['id'] ?>"><?PHP $st =($rowc['status']==1)?'Active':'Inactive';echo $st;?></td><td><a href="tindex.php?delete_id=<?PHP echo $rowc['id'] >">Delete

</tr><?PHP}?></tbody></table><?PHP}?></body></html>

1 个答案:

答案 0 :(得分:0)

我将建议您使用任何适用于php的好的编辑器,这将提高您的代码编写技能。例如sublime,vscode或phpstorm 更正下面的if语句

 <?php //connection database 
    require_once("config.php"); ?>

 <?php
    //change active or Inactive
    if (isset($_GET['cat_id']))
    {
        $sql = mysqli_connect('localhost', 'root', '', 'ocall');
        $rs = mysqli_query($sql, "SELECT * FROM category WHERE id='$_GET[cat_id]'");
        $rows = mysqli_fetch_assoc($rs);
        if ($rows)
        {
            if($rows['status'] == 1)
            {
                $stt = 0;
            }
            else
            {
                $stt = 1;
            }
            $sql4 = mysqli_connect('localhost', 'root', '', 'ocall');
            $sqlup = mysqli_query($sql4, "UPDATE category SET status ='$stt' WHERE id='$_GET[cat_id]'");
            echo "Record updated!";
        }
        else
        {
            # In this else block you can write whatever you want to do, when record doesn;t exist with the id passed by get method
            echo "Record doesn't exist!";
        }
    }
?>

 <?php //delete 
    if (isset($_GET['delete_id'])) {
        $sql2 = mysqli_connect('localhost', 'root', '', 'ocall');
        $sqld = mysqli_query($sql2, "DELETE FROM category WHERE id ='$_GET[delete_id]'");
    } ?>

 <!doctype html>
 <html>

 <head></head>

 <body>
     <h2>Category Control</h2>
     <form action="" method="post"><br>Category Name:<input type="text" name="category"><br>Short Description:<textarea name="shortdescription"></textarea><br>status:<select name="status">
             <option value="1">Active</option>
             <option value="0">Inactve</option><input type="submit" name="submit" value="Submit"></form>

     <?php if (isset($_POST['submit'])) {
            $sql3 = mysqli_connect('localhost', 'root', '', 'ocall');
            $sqli = mysqli_query($sql3, "INSERT category SET name ='$_POST[category]',
status ='$_POST[status]',short_description= '$_POST[shortdescription]'");
        } ?>

     <?php $sqlselect = mysqli_connect('localhost', 'root', '', 'ocall');
        $rsc = mysqli_query($sqlselect, "SELECT * FROM category ORDER BY id DESC");
        $count = mysqli_num_rows($rsc);
        if ($count > 0) { ?>

         <table class="table table-hover">
             <thead>
                 <tr>
                     <th>ST.</th>
                     <th>Name</th<th>Short Description</th>
                     <th>Status</th>
                     <th>Action</th>
                 </tr>
             </thead>

             <?php $i = 0;
                while ($rowc = mysqli_fetch_array($rsc)) {
                    $i++ ?>

                 <tr>
                     <td>
                         <?php echo $i; ?>
                     </td>
                     <td>
                         <?php echo $rowc['name']; ?>
                     </td>
                     <td>
                         <?php echo $rowc['short_description']; ?>
                     </td>
                     <td><a href="tindex.php?cat_id=<?php echo $rowc['id'] ?>">
                             <?php $st = ($rowc['status'] == 1) ? 'Active' : 'Inactive';
                                echo $st; ?>
                     </td>
                     <td><a href="tindex.php?delete_id=<?php echo $rowc['id']; ?> ">Delete</a></td>
         </tr>
         <?php } ?>
         </tbody>
     </table>
     <?php } ?>
 </body>
 </html>

您在if语句中输入了错误的条件。 使用上面给出的语句更正它。 谢谢。