使用php激活和停用mysql记录

时间:2012-09-07 04:39:43

标签: php javascript jquery mysql ajax

您好我正在研究用户管理系统。我显示了来自db的所有记录,并且成功执行了添加,编辑和删除操作。现在我需要在主页中显示只有来自Mysql表的活动记录。我的管理控制中有2个链接,一个是激活,另一个是停用。如果我点击激活,记录将显示在主页上,停用意味着记录将隐藏在主页中。我用

插入了字段状态
datatype enum('active','deactive')`.

我的编码如下。在编码我需要的变化?

的index.php:

<form method="post">
<table>

    <tr>
        <td>Title:</td>
        <td><input type="text" name="title" /></td>
    </tr>
    <tr>
        <td>Author</td>
        <td><input type="text" name="author" /></td>
    </tr>
    <tr>
        <td>Publisher Name</td>
        <td><input type="text" name="name" /></td>
    </tr>
    <tr>
        <td>Copyright Year</td>
        <td><input type="text" name="copy" /></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td><input type="submit" name="submit" value="add" /></td>
    </tr>
</table>
<?php
if (isset($_POST['submit']))
    {      
    include 'db.php';

                    $title=$_POST['title'] ;
                    $author= $_POST['author'] ;                 
                    $name=$_POST['name'] ;
                    $copy=$_POST['copy'] ;

         mysql_query("INSERT INTO `books`(Title,Author,PublisherName,CopyrightYear) 
         VALUES ('$title','$author','$name','$copy')"); 


            }
?>
</form>
<table border="1">

            <?php
            include("db.php");


            $result=mysql_query("SELECT * FROM books");

            while($test = mysql_fetch_array($result))
            {
                $id = $test['BookID'];  
                echo "<tr align='center'>"; 
                echo"<td><font color='black'>" .$test['BookID']."</font></td>";
                echo"<td><font color='black'>" .$test['Title']."</font></td>";
                echo"<td><font color='black'>". $test['Author']. "</font></td>";
                echo"<td><font color='black'>". $test['PublisherName']. "</font></td>";
                echo"<td><font color='black'>". $test['CopyrightYear']. "</font></td>"; 
                echo"<td> <a href ='view.php?BookID=$id'>Edit</a>";
                echo"<td> <a href ='view.php?BookID=$id'>Activate</a>";
                echo"<td> <a href ='view.php?BookID=$id'>Deactivate</a>";
                echo"<td> <a href ='del.php?BookID=$id'><center>Delete</center></a>";

                echo "</tr>";
            }
            mysql_close($conn);
            ?>
</table>

View.php:
<?php
require("db.php");
$id =$_REQUEST['BookID'];

$result = mysql_query("SELECT * FROM books WHERE BookID  = '$id'");
$test = mysql_fetch_array($result);
if (!$result) 
        {
        die("Error: Data not found..");
        }
                $Title=$test['Title'] ;
                $Author= $test['Author'] ;                  
                $PublisherName=$test['PublisherName'] ;
                $CopyrightYear=$test['CopyrightYear'] ;

if(isset($_POST['save']))
{   
    $title_save = $_POST['title'];
    $author_save = $_POST['author'];
    $name_save = $_POST['name'];
    $copy_save = $_POST['copy'];

    mysql_query("UPDATE books SET Title ='$title_save', Author ='$author_save',
         PublisherName ='$name_save',CopyrightYear ='$copy_save' WHERE BookID = '$id'")
                or die(mysql_error()); 
    echo "Saved!";

    header("Location: index.php");          
}
mysql_close($conn);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form method="post">
<table>
    <tr>
        <td>Title:</td>
        <td><input type="text" name="title" value="<?php echo $Title ?>"/></td>
    </tr>
    <tr>
        <td>Author</td>
        <td><input type="text" name="author" value="<?php echo $Author ?>"/></td>
    </tr>
    <tr>
        <td>Publisher Name</td>
        <td><input type="text" name="name" value="<?php echo $PublisherName ?>"/></td>
    </tr>
    <tr>
        <td>Copyright Year</td>
        <td><input type="text" name="copy" value="<?php echo $CopyrightYear ?>"/></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td><input type="submit" name="save" value="save" /></td>
    </tr>
</table>

</body>
</html>

del.php:

<?php
  include("db.php");  

    $id =$_REQUEST['BookID'];


    // sending query
    mysql_query("DELETE FROM books WHERE BookID = '$id'")
    or die(mysql_error());      

    header("Location: index.php");
?>

请提供一些想法或编纂。这是我第一次使用这个概念。??

4 个答案:

答案 0 :(得分:4)

使用主动无效逻辑..

您必须按照以下步骤操作: -

  
      
  1. 您必须创建一些标记状态,其中包含active和的枚举值   去活。
  2.   
  3. 从数据库中获取数据时,您必须提供条件以仅获取活动记录。
  4.   
  5. 如果您有活动或非活动按钮,则必须更新您的记录   基于此。
  6.   

如果您将按照这些步骤进行操作!!

答案 1 :(得分:1)

enum用于boolean类型并不是一个好主意,因为它会占用更多内存。

您可以在记录数据库中添加active标记。由于您已经拥有每行的唯一ID,因此您可以这样做:

SELECT * FROM `books`;

输出

+----+-----------------------+----------------------+----------------+------+--------+
| id | title                 | pub                  | author         | copy | active |
+----+-----------------------+----------------------+----------------+------+--------+
|  1 | A Matter of Time      | Outskirts Press      | Michael Bowler | 2010 |      0 |
|  2 | The Promise of Change | Soul Mate Publishing | Rebecca Heflin | 2011 |      0 |
+----+-----------------------+----------------------+----------------+------+--------+

对于active,您可以设置0隐藏或1显示! :)

现在让我们激活这本书A Matter of Time。它的ID为1

UPDATE `books` SET `active` = '1' WHERE  `books`.`id` = 1;

输出

+----+-----------------------+----------------------+----------------+------+--------+
| id | title                 | pub                  | author         | copy | active |
+----+-----------------------+----------------------+----------------+------+--------+
|  1 | A Matter of Time      | Outskirts Press      | Michael Bowler | 2010 |      1 |
|  2 | The Promise of Change | Soul Mate Publishing | Rebecca Heflin | 2011 |      0 |
+----+-----------------------+----------------------+----------------+------+--------+

让我们只获得有效的书籍:

SELECT * FROM `books` WHERE `active` = 1;

输出

+----+-----------------------+----------------------+----------------+------+--------+
| id | title                 | pub                  | author         | copy | active |
+----+-----------------------+----------------------+----------------+------+--------+
|  1 | A Matter of Time      | Outskirts Press      | Michael Bowler | 2010 |      1 |
+----+-----------------------+----------------------+----------------+------+--------+

使用此功能,您可以单独检索活动书籍。现在停用这本书:

UPDATE `books` SET `active` = '0' WHERE  `books`.`id` = 1;

PHP代码

<?php
    $result = mysql_query("SELECT * FROM `books` WHERE `active` = 1");
    while (false !== ($data = mysql_fetch_array($result)))
        echo $data["title"];
?>

根据您的需要设置代码格式。 :)

答案 2 :(得分:0)

插入或更新数据时,只需添加存储数据或记录状态的状态字段。

当您在查询用户中显示SELECT * FROM books where status='active'等条件时。

这对你来说很有用。

答案 3 :(得分:0)

此活动并停用帐户。在数据库表中创建一个状态列,给出类型int,如果你插入一个数据库,它会自动取0,当select table给出条件,其中status =“1”1表示有效,0表示无效。 例如:select * from login where status ='1'; Demo