Use PHP to manipulate the contents of the database (change)

Source: Internet
Author: User

Query page (user visible)

<body>

<table width= "80%" border= "1" cellpadding= "0" cellspacing= "0" >
<tr>
<td> Nationality Code </td>
<td> National Name </td>
<td> Operations </td>
</tr>

<?php
Build a connection
$conn = @mysql_connect ("localhost", "root", "123");
Select the Operational database
mysql_select_db ("HR", $conn);
Write SQL statements
$sql = "SELECT * from Nation";

Execute SQL statement, return result set
$result = mysql_query ($sql);

Reading data
while ($attr = Mysql_fetch_row ($result))
{
echo "<tr>
<td>{$attr [0]}</td>
<td>{$attr [1]}</td>
<td><a onclick=\ "return confirm (' OK to delete ') \" href= ' delete.php?code={$attr [0]} ' > Delete </a><a href= ' update.php?code={$attr [0]}&name={$attr [1]} ' > Modify </a></td>
</tr> "; Similar to deleting content, you need to insert a tag that jumps to the modified page and should have all the data for that item as the default value before the change.
}
?>
</table>

</body>

Change Content Display page (user visible)

<body>
<form action= ' xiugai.php ' method= ' post ' >//Create form submission box, connect to modify page, data delivered in post format
<div> Nationality Code: <input type= "hidden" type= "text" name= "code" value= "<?php $code =$_get[' code ']; Echo $code;? >"/>//Extract the previous page with the code value as the default value of the Commit button, and in order to prevent the modification of the original table data caused confusion, the primary key value can not be modified, so to hide the code value to prevent it from being modified, And also to take this value to the next modification page using the
</div>
<div> Nationality Name: <input type= "text" name= "name" value= "<?php $name =$_get[' name '];echo $name;? >"/>//Extract previous page bring over name value as the initial value of the Submit button
</div>
<input type= ' Submit ' value= ' submission '/>
</form>
</body>

Action Processing page (user not visible)

<?php
Header ("Content-type:text/html;charset=utf-8"); //Change language display type to Utf-8, Chinese characters will not display garbled
$code = $_post["code"]; Receives the code value of the previous page as the value of the $code
$name = $_post["name"]; Receives the user-modified name value of the previous page as the value of the $name
[Email Protected]_connect ("localhost", "root", "123"); Build a connection
mysql_select_db ("HR", $a); Select the database to manipulate
$sql = "Update nation set name= ' $name ' where code= ' $code '"; SQL statement that writes changes
$b =mysql_query ($sql); Execute SQL statement to return result set
if ($b)
{
Header ("location:chaxun.php"); The modification succeeds returns the query page
}
Else
{
echo "Modification failed";} The modification fails to remind

Use PHP to manipulate the contents of the database (change)

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.