使用MySQL& amp;删除查询将无法在第一行上运行PHP

时间:2014-03-21 09:20:57

标签: php mysql html-table

当我尝试删除此表的第一行时,它不会删除,而是显示我在添加新类别时使用的文本框中的详细信息。

图片:https://dl.dropboxusercontent.com/u/88831139/1.jpg

除第1行外,查询适用于每一行。如果我单击删除ID为2,3或4,则所有工作正常,但行1除外。它不适用于ID = 1.

请建议我做什么。 注意:我使用会话将值保存在变量中。


我不是删除行我只是更新它的状态以保持它在这里记录是代码: 到添加类别:

if(isset($_POST['category']))
{ $cat = $_POST['category'];

$validate_category = "SELECT * FROM Category WHERE CategoryName ='$cat'";
$result = mysql_query($validate_category);
$count = mysql_num_rows($result);


    if(empty($cat))
    {
    session_start();
    $msg =  'kindly Put Details completely';
    $_SESSION['status'] = $msg; 
    $_SESSION['color'] = 'red';
    header("location:../category.php");
    }
    else if($count==1)
    {
    session_start();
    $msg =  'Category Already Exist !!! Kindly Choose any Other Category';
    $_SESSION['status'] = $msg; 
    $_SESSION['color'] = 'red';
    header("location:../category.php");
    }
    else{

    $query = "INSERT INTO Category(CategoryName) values ('$cat')";
    mysql_query($query);

    session_start();
    $msg =  'New Category has been Created';
    $_SESSION['status'] = $msg; 
    $_SESSION['color'] = 'green';
    header("location:../category.php");
    }}

代码删除行:

   $delete_id = mysql_real_escape_string($_POST["delete_id"]);
   $del_cat = mysql_result(mysql_query("SELECT CategoryName FROM Category WHERE ID = ".$delete_id), 0); // 1 = row
    mysql_query("UPDATE Category SET hidden = '1'  WHERE  ID =".$delete_id);
    session_start();
    $msg =  "'$del_cat' Category has been Deleted";
    $_SESSION['status'] = $msg; 
    $_SESSION['color'] = 'green';
    header("location:../category.php");

主页:

      <?php 
      session_start();
      if(!isset($_SESSION['UserEmail']))
      {
        header("location:../login.php");
      }
      else if($_SESSION['UserRole'] != 'Admin')
      {
        header("location:../login.php");
      }

      include "../_inc/config.php";

      ?>

      <html>
      <head>
        <title>Add Category</title>
      <link rel="stylesheet" type="text/css" href="style.css" />

      </head>
      <body>
      <?php include "_lib/menu.php"; ?> 

      <h2>Add Category</h2>

      <form action="_lib/AddCategory.php" method="POST">
      Category : <input type='text' name="category"/>
      <input type="Submit" value="Submit" />

      <?php


        if (isset($_SESSION['status'])){
        $msg = $_SESSION['status'];
        echo $msg;
        unset($_SESSION['status']);
        }

      ?>

      <table border='1'>
      <tr>
        <th>ID</th>
        <th>Category</th>
      </tr>
      <?php
      $query1 = "SELECT * from Category where hidden = '0'";
      $result = mysql_query($query1);
       $i = 1;
       while($row = mysql_fetch_array($result)) : ?>
        <tr>
            <td> <?php echo $i; $i=$i+1; ?> </td>
            <td> <?php echo $row['CategoryName']; ?> </td>
            <td>
                <form action="_lib/DeleteCategory.php" method="POST" >
              <input type="hidden" name="delete_id" value="<?php echo $row['ID']; ?>" />
              <input type="submit" value="Delete" />
              </form>

          </td>
        </tr>

      <?php endwhile; ?>
      </table>

      </form>
      </body>
      </html>

0 个答案:

没有答案