php - 使用 php 激活和停用 mysql 记录

标签 php javascript jquery mysql ajax

您好,我正在开发用户管理系统。我显示了数据库中的所有记录,并且添加、编辑和删除操作已成功执行。现在我需要在主页中仅显示 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");
?>

请给出一些想法或编码。这是我第一次使用这个概念。??

最佳答案

使用主动非主动逻辑时..

您必须遵循以下步骤:-

  1. You have to create some flag status with enum value for active and deactive.
  2. while fetching data from database you have to give condition to fetch only active record.
  3. If you have active or deactive button you have to update your record based on that.

如果您遵循这些步骤,它就会起作用!

关于php - 使用 php 激活和停用 mysql 记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12311822/

相关文章:

java - JSON POST 请求使用 Php 从 Android 中的 HttpClient 从服务器端接收?

php - 多个 Laravel 网站,中央代码库

javascript - 您可以使用 HTML 和 javascript 创建一个包含可编辑内容的按钮吗?

javascript - 在 javascript 中使用 this 关键字将参数传递给函数

javascript - 使用 Ajax 回发后 JQuery Datepicker 错误

php - PHP/UDP : Can write via UDP,,但无法接收数据接缝

php - "caching"php 中的数据库值数组(2 个选项,哪个更好?)

javascript - Jquery Clone() 内部 HTML 属性不改变

javascript - 禁用 jQuery 移动 slider 点击时的文本输入

javascript - 引用错误 : _ is not defined