PHP mysqli additions and deletions to check

Source: Internet
Author: User

In recent days, we have been learning to use MYSQLI to access the database and manipulate the data in it. Today to show you a complete example, we have to create a press release system, using MYSQLI to operate the database, to add to the news, modify, delete, query and other basic functions. (The following code is divided into the front-end display and the background PHP processing code, separated by a blank line, attention to distinguish)

1, landing page: This company internal personnel through the employee number and ID number to log in, the code is as follows://front-end display part

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Untitled Document </title>

<body>
<form action= "dlyz.php" method= "POST" >
<div> Username: <input type= "text" name= "user" value= "Please enter your work number"/></div>
<br/>
<div> Secret &nbsp;&nbsp; Code: <input type= "password" name= "PSD"/></div>
<br/>
<input type= "Submit" value= "Login"/>
<input type= "Submit" value= "register new user" formaction= "zhuc.php"/>
</form>
</body>

PHP code to process the information submitted to the login

<?php

$user = $_post["user"];
$PSD = $_post["PSD"];


Created objects
$db = new Mysqli ("localhost", "root", "" "," Newssystem ");

Judging if there is an error
!mysqli_connect_error () or Die ("Connection failed!! ");

Write SQL statements

$sql = "Select psd from Yonghu where user= ' {$user} '";


Execute SQL statement

$result = Query ($sql), $db
$v = $result->fetch_row ();
if ($psd = = $v [0])
{
Header ("location:fabuxinwen.php");
}
Else
{
echo "The user name or password you entered is not correct, please re-enter!!" ";
}

2, registration page: The company/newspaper to new employees, only after registration to login

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Untitled Document </title>

<body>
<body>

<form action= "zhucyz.php" method= "POST" >
<div> Username: <input type= "text" name= "user" value= "Please enter your work number"/></div><br/>

<div> Secret &nbsp;&nbsp; Code: <input type= "password" name= "psd"/></div><br/>

<input type= "Submit" value= "Submission"/>
</form>

</body>

<?php
$user = $_post["user"];
$PSD = $_post["PSD"];


Created objects
$db = new Mysqli ("localhost", "root", "" "," Newssystem ");

Judging if there is an error
!mysqli_connect_error () or Die ("Connection failed!! ");

Write SQL statements

$sql = "INSERT into Yonghu values (' {$user} ', ' {$psd} ')";

Execute SQL statement

$result = Query ($sql), $db

if ($result)
{
Header ("location:dl.php");
}
Else
{
echo "Sorry, registration failed!! ";
}

3, after landing, is to publish the news page, click submit button to submit to the established database, click the View button to view the confirmation

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
<meta http-equiv= "Content-type" Content= "text/html; Charset=utf-8 "/>
<title> Untitled document </TITLE>

<body>
<div style= "width:100%; Text-align:center ">
<form method= "POST" >
<input type= "hidden" name= "NewSID"/>
<table style= "margin:0 auto; Text-align:left ">
<tr>
<TD > Title: </td><td><input type= "text" style= "width:400px" name= "title"/></td>
</tr>
<tr>
<TD >
</td><td><input type= "text" style= "width:400px" name= "Author"/></td>
</tr>
<tr>
<TD > Source: </td><td><input type= "text" style= "width:400px" name= "source"/></td>
</tr>
<tr>
<TD > Content:</td>
<td><textarea cols= "Auto" rows= "Auto" style= "width:400px; height:400px "name=" content "></textarea></td>
</tr>

</table><br/>
<?php
$time = Date (' y-m-d h:i:s ');

echo "<input type=\" hidden\ "name=\" time\ "value=\" {$time}\ "/>";
?>

<input type= "Submit" value= "commit" formaction= "tijiao.php"/>

<input type= "Submit" value= "View" formaction= "chakan.php"/>
</form>
</div>
</body>

<?php
$title = $_post["title"];
$Author = $_post["Author"];
$source = $_post["source"];
$content = $_post["Content"];

Created objects
$db = new Mysqli ("localhost", "root", "" "," Newssystem ");

Judging if there is an error
!mysqli_connect_error () or Die ("Add failed!! ");

Write SQL statements

$sql = "INSERT INTO News" values (' {$title} ', ' {$Author} ', ' {$source} ', ' {$content} ') "Title,author,source,content;


Execute SQL statement

$result = Query ($sql), $db

if ($result)
{
Header ("location:fabuxinwen.php");
}
Else
{
echo "Sorry, add failed!! ";
}

4. View Page

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Untitled Document </title>

<body>
<table width= "70%" border= "1px" cellpadding= "0" cellspacing= "0" style= "Text-align:center" >
<tr>
<td> numbering </td>
<td> title </td>
<td> author </td>
<td> sources </td>
<td> dates </td>
<td> Delete </td>
<td> Modify </td>

</tr>

<?php
$db =new mysqli ("localhost", "root", "" "," Newssystem ");
!mysqli_connect_error () or Die ("Connection error");
$sql = "SELECT * FROM News";
$result = $db->query ($sql);
while ($attr = $result->fetch_row ())
{
echo "
<tr>
<td>{$attr [0]}</td>
<td>{$attr [1]}</td>
<td>{$attr [2]}</td>
<td>{$attr [3]}</td>
<td>{$attr [5]}</td>
<td><a onclick=\ "return confirm (' OK to delete ') \" href= ' scchuli.php?newsid={$attr [0]} ' > Delete </a></td >
<td><a href= ' xiugai.php?newsid={$attr [0]} ' > Modify </a></td>
</tr>
";
}

?>
</table>
</body>

5, in the View page can make the relevant content modification and deletion/modification: After clicking the Edit button, it will jump to the modification page, which will show the content, title and other related content which have been published before.

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Untitled Document </title>

<body>
<?php
$id = $_get["NewSID"];

$db =new mysqli ("localhost", "root", "" "," Newssystem ");
!mysqli_connect_error () or Die ("Connection error");
$sql = "SELECT * from news where newsid= ' {$id} '";
$result = $db->query ($sql);
$attr = $result->fetch_row ();
?>
<div style= "width:100%; Text-align:center ">
<form action= "xgchuli.php" method= "POST" >
<input type= "hidden" name= "NewSID" <?php echo "value= ' {$attr [0]} '";? >/>
<table style= "margin:0 auto; Text-align:left ">
<tr>
&LT;TD > Title: </td><td><input type= "text" style= "width:400px" name= "title" <?php echo "value=" {$ ATTR[1]} ' ";? >/></td>
</tr>
<tr>
&LT;TD >
</td><td><input type= "text" style= "width:400px" name= "Author" <?php echo "value= ' {$attr [2]} '";? >/>
</td>
</tr>
<tr>
&LT;TD > Source: </td><td><input type= "text" style= "width:400px" name= "source" <?php echo "value=" {$ ATTR[3]} ' ";? >/>
</td>
</tr>
<tr>
&LT;TD > Content:</td>
<td><textarea cols= "Auto" rows= "Auto" style= "width:400px; height:400px "name=" content "><?php echo" {$attr [4]} ";? >
</textarea></td>
</tr>

</table><br/>
<?php
$time = Date (' y-m-d h:i:s ');

echo "<input type=\" hidden\ "name=\" time\ "value=\" {$time}\ "/>";
?>

<div><a href= "chakan.php" ><input type= "button" title= "View" value= "View/></a><input type=" Submit "title=" Modify "value=" Modify "/>
</div>

</form>

</body>

<?php

$id =$_post["NewSID"];
$title =$_post["title"];
$Author =$_post["Author"];
$source =$_post["source"];
$content =$_post["Content"];
$time =$_post["Time"];

$db = new Mysqli ("localhost", "root", "" "," Newssystem ");

!mysqli_connect_error () or Die ("Connection failed");

$sql = "Update news set title= ' {$title} ', author= ' {$Author} ', source= ' {$source} ', content= ' {$content} ', Time= ' {$time} ' Where newsid= ' {$id} ';

$result = $db->query ($sql);
if ($result)
{
Header ("location:chakan.php");
}
Else
{
echo "failed to modify";
}

Delete: If you want to delete a news, after clicking the Delete button, will not jump to any foreground display page, only in the background through the PHP code related processing

<?php
$id =$_get["NewSID"];

$db =new mysqli ("localhost", "root", "" "," Newssystem ");

!mysqli_connect_error () or Die ("Connection failed");

$sql = "Delete from news where newsid= ' {$id} '";

$result = $db->query ($sql);

if ($result)
{
Header ("location:chakan.php");
}
Else
{
echo "Delete Failed";
}

6, after the completion of the modification, click the Edit button at the bottom of the page to submit, there will be no relevant front-end display page, just in the background with the relevant PHP code for the corresponding processing, after successful return to publish News page

<?php
$title = $_post["title"];
$Author = $_post["Author"];
$source = $_post["source"];
$content = $_post["Content"];

Created objects
$db = new Mysqli ("localhost", "root", "" "," Newssystem ");

Judging if there is an error
!mysqli_connect_error () or Die ("Add failed!! ");

Write SQL statements

$sql = "INSERT INTO News" values (' {$title} ', ' {$Author} ', ' {$source} ', ' {$content} ') "Title,author,source,content;


Execute SQL statement

$result = Query ($sql), $db

if ($result)
{
Header ("location:fabuxinwen.php");
}
Else
{
echo "Sorry, add failed!! ";
}

PHP mysqli additions and deletions to check

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.