PHP Development Message Board CRUD (add, delete, change, check) operation _php Instance

Source: Internet
Author: User
Tags create database

Project structure:

Add Page: Description: Here only focus on the operation, the interface of the art does not work, I hope we understand ...

List page:

To modify a page:

SQL required in the project:

Copy Code code as follows:

Create Database form;

Use form;

CREATE TABLE ' message ' (
' id ' tinyint (1) not NULL auto_increment,
' User ' varchar not NULL,
' title ' varchar Not NULL,
' Content ' Tinytext not NULL,
' Lastdate ' date not NULL,
PRIMARY KEY (' id ')
) Engine=innodb DEFAULT CHARSET=GBK auto_increment=1;


conn.php
Copy Code code as follows:

<?php
$conn = @ mysql_connect ("localhost", "root", "") or Die ("Database link error");
mysql_select_db ("form", $conn);
mysql_query ("Set names ' GBK '");
?>

add.php
Copy Code code as follows:

<?php
Include ' conn.php ';
if ($_post[' submit ']) {
$sql = "INSERT into message (id,user,title,content,lastdate) VALUES (NULL, ' $_post[user] ', ' $_post[title] ', ' $_post[ Content] ', now () ';
mysql_query ($sql);

Page jumps, implemented as JavaScript
$url = "list.php";
echo "<script language= ' javascript ' type= ' text/javascript ' >";
echo "window.location.href= ' $url '";
echo "</script>";
}
?>
<script type= "Text/javascript" >
function Checkpost () {

if (addform.user.value== "") {
Alert ("Please enter user name");
AddForm.user.focus ();
return false;
}
if (addform.title.value.length<5) {
Alert ("title cannot be less than 5 characters");
AddForm.title.focus ();
return false;
}
}
</script>
<form name= "AddForm" method= "POST" action= "add.php" onsubmit= "return Checkpost ();" >
User: <input type= "text" name= "user"/><br/>
Title: <input type= "text" name= "title"/><br/>
Content: <textarea name= "Content" rows= "8" cols= "></textarea><br/>"
<input type= "Submit" name= "Submit" value= "Add"/></form>

list.php
Copy Code code as follows:

<?php
Include ' conn.php ';
?>
<?php
echo "<div align= ' center ' ><a href= ' add.php ' > continue to add </a></div>";
?>
<table width=500 border= "0" align= "center" cellpadding= "5" cellspacing= "1" bgcolor= "#add3ef" >
<?php
$sql = "SELECT * from Message order by ID";
$query =mysql_query ($sql);
while ($row =mysql_fetch_array ($query)) {
?>

<tr bgcolor= "#eff3ff" >
<td> title: <font color= "Red" ><?= $row [title]?></font> User: <font color= "Red" ><?= $row [  User]? ></font><div align= "right" ><a href= "preedit.php?id=<?= $row [id]?>" > Edit </a> | <a href= "delete.php?id=<?= $row [id]?>" > Delete </a></div></td>
</tr>
<tr bgcolor= "#ffffff" >
<td> content: <?= $row [content]?></td>
</tr>
<tr bgcolor= "#ffffff" >
<td><div align= "Right" > Release date: <?= $row [lastdate]?></div></td>
</tr>
<?php}?>
</table>

delete.php
Copy Code code as follows:

<?php
Include ' conn.php ';
$id = $_get[' id '];
$query = "Delete from message where id=". $id;
mysql_query ($query);
?>
<?php
Page jumps, implemented as JavaScript
$url = "list.php";
echo "<script language= ' javascript ' type= ' text/javascript ' >";
echo "window.location.href= ' $url '";
echo "</script>";
?>

preedit.php
Copy Code code as follows:

<?php
Include ' conn.php ';
$id =$_get[id];
$query = "SELECT * FROM message WHERE id =". $id;
$result =mysql_query ($query);
while ($rs =mysql_fetch_array ($result)) {
?>
<form method= "POST" action= "postedit.php" >
<input type= "hidden" name= "id" value= "<?= $rs [id]?>" >
User: <input type= "text" name= "user" value= "<?= $rs [user]?>"/><br/>
Title: <input type= "text" name= "title" value= "<?= $rs [title]?>"/><br/>
Content: <textarea name= "Content" rows= "8" cols= "a" ><?= $rs [content]?></textarea><br/>
<input type= "Submit" name= "Submit" value= "edit"/>
</FORM>
<?php}?>

postedit.php
Copy Code code as follows:

<?php
Include ' conn.php ';
$query = "Update message set user= ' $_post[user]", title= ' $_post[title] ', content= ' $_post[content] ' where id= ' $_post[id ' '";
mysql_query ($query);
?>
<?php
Page jumps, implemented as JavaScript
$url = "list.php";
echo "<script language= ' javascript ' type= ' text/javascript ' >";
echo "window.location.href= ' $url '";
echo "</script>";
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.