admin管理员组文章数量:1318335
Hi I'm working on user management system. I'm displays all records from db and add, edit and delete operations are successfully performed. now I'm need to display in home page as only active records from Mysql table. i have 2 links in my admin control one is activate and another is deactivate. if i click activate the records will display in home page and deactivate means records will hide in home page. i'm inserted field status with
datatype enum('active','deactive')`.
my coding are below. In coding what changes i need it?
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> </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" ".dtd">
<html xmlns="">
<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> </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");
?>
please give some ideas or codings. this is first time i'm using this concept.??
Hi I'm working on user management system. I'm displays all records from db and add, edit and delete operations are successfully performed. now I'm need to display in home page as only active records from Mysql table. i have 2 links in my admin control one is activate and another is deactivate. if i click activate the records will display in home page and deactivate means records will hide in home page. i'm inserted field status with
datatype enum('active','deactive')`.
my coding are below. In coding what changes i need it?
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> </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/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/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> </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");
?>
please give some ideas or codings. this is first time i'm using this concept.??
Share Improve this question edited Sep 7, 2012 at 4:53 Praveen Kumar Purushothaman 167k27 gold badges213 silver badges260 bronze badges asked Sep 7, 2012 at 4:39 Boopathi VBoopathi V 372 gold badges5 silver badges9 bronze badges4 Answers
Reset to default 4While Using active deactive logic..
You have to follow following steps:-
- You have to create some flag status with enum value for active and deactive.
- while fetching data from database you have to give condition to fetch only active record.
- If you have active or deactive button you have to update your record based on that.
If you will follow these step it will work!!
It is not a good idea to use enum
for a boolean
type, as it occupies more memory.
You can have something like a active
flag in the database of the records. Since already you have a unique ID for each row, you can just do this:
SELECT * FROM `books`;
Output
+----+-----------------------+----------------------+----------------+------+--------+
| 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 |
+----+-----------------------+----------------------+----------------+------+--------+
And for the active
, you can either set 0
to hide or 1
to show! :)
Now lets activate the book, A Matter of Time
. It has an ID 1
.
UPDATE `books` SET `active` = '1' WHERE `books`.`id` = 1;
Output
+----+-----------------------+----------------------+----------------+------+--------+
| 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 |
+----+-----------------------+----------------------+----------------+------+--------+
Lets get only the active books:
SELECT * FROM `books` WHERE `active` = 1;
Output
+----+-----------------------+----------------------+----------------+------+--------+
| id | title | pub | author | copy | active |
+----+-----------------------+----------------------+----------------+------+--------+
| 1 | A Matter of Time | Outskirts Press | Michael Bowler | 2010 | 1 |
+----+-----------------------+----------------------+----------------+------+--------+
Using this you can retrieve the active books alone. Now to deactivate the book:
UPDATE `books` SET `active` = '0' WHERE `books`.`id` = 1;
The PHP Code
<?php
$result = mysql_query("SELECT * FROM `books` WHERE `active` = 1");
while (false !== ($data = mysql_fetch_array($result)))
echo $data["title"];
?>
Format the code according to your needs. :)
When you insert or update data just also add status field which stored your data or record status.
And when you displayed in query user where condition like SELECT * FROM books where status='active'
.
This will work for you just try.
This active and deactivate a account. create a status column in database table give type int and if u insert a database it automatically take 0 and when select table give condition where status="1" 1 means active and 0 is deactivate. for ex: select * from login where status='1'; Demo
本文标签: javascriptactivate and deactivate mysql record using phpStack Overflow
版权声明:本文标题:javascript - activate and deactivate mysql record using php - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742041393a2417563.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论