Php Getting Started Tutorial 8 (delete, query, modify, and update database operation data)

Source: Internet
Author: User

Simple Data Query

The code is as follows: Copy code

<? Php
// Connect to the database
$ Link_id = mysql_connect ("localhost", "root", "") or die ("connection failed ");
If ($ link_id)
{
// Select a database
Mysql_select_db ("my_test ");
// The above is the connection part of the header database, which is the public part below.
If (! $ _ Get [id]) {

// Display the user list
$ SQL = "select * from userinfo ";
$ Result = mysql_query ($ SQL );
  
Echo "<table border = 1>
<Tr>
<Td> No. </td>
<Td> user name </td>
<Td> Gender </td>
<Td> age </td>
<Td> Registration time </td>
<Td> Details </td>
</Tr> ";

 

While ($ row = mysql_fetch_array ($ result )){
Echo "<tr>
<Td> ". $ row [id]." </td>
<Td> ". $ row [username]." www.111cn.net </td>
<Td> ". $ row [gender]." </td>
<Td> ". $ row [age]." </td>
<Td> ". $ row [regdate]." </td>
<Td> <a href = query. php? Id = ". $ row [id]."> View </a> </td>
</Tr> ";
  }
Echo "</table> ";
 }
Else
 {  
// Display the details of a specified user
$ SQL = "select * from userinfo where id =". $ _ get [id];
$ Result = mysql_query ($ SQL );
$ Row = mysql_fetch_array ($ result );
Echo "No :". $ row [id]. "<br> user name :". $ row [username]. "<br> gender :". $ row [gender]. "<br> Age :". $ row [age]. "<br> Registration time :". $ row ['regdate'];

Echo "<br> <a href = query. php> continue query </a> ";
 }
} // End if
?>

Delete data

The code is as follows: Copy code

<? Php
// Connect to the database
$ Link_id = mysql_connect ("localhost", "root", "") or die ("connection failed ");
If ($ link_id)
{
Mysql_select_db ("my_test ");
If (! $ _ Get [id])
 {

$ Result = mysql_query ("select * from userinfo ");
Echo "<table border = 1>
<Tr>
<Td> No. </td>
<Td> user name www.111cn.net </td>
<Td> Gender </td>
<Td> age </td>
<Td> Registration time </td>
<Td> Operation </td>
</Tr> ";

 

While ($ row = mysql_fetch_array ($ result )){
Echo "<tr>
<Td> ". $ row [id]." </td>
<Td> ". $ row [username]." </td>
<Td> ". $ row [gender]." </td>
<Td> ". $ row [age]." </td>
<Td> ". $ row [regdate]." </td>
<Td> <a href = delete. php? Id = ". $ row [id]."> delete </a> </td>
</Tr> ";
  }
Echo "</table> ";

} // Display the list content
Else
 {

$ SQL = "delete from userinfo where id =". $ _ get [id];
$ Result = mysql_query ($ SQL );
If ($ result)
Echo "the record has been successfully deleted <br> <a href = 'delete. Php'> return </a> ";
Else
Echo "failed to delete record <br> <a href = delete. php. php? Id = ". $ _ get [id]."> Return </a> ";

} // Else ($ id)
} // End if
?>

Modify and update records

The code is as follows: Copy code

<? Php
// Connect to the database
$ Link_id = mysql_connect ("localhost", "root", "") or die ("connection failed ");
If ($ link_id)
{
Mysql_select_db ("my_test ");
If (! $ _ Get [id])
 {

$ Result = mysql_query ("select * from userinfo ");
Echo "<table border = 1>
<Tr>
<Td> No. </td>
<Td> user name </td>
<Td> Gender </td>
<Td> age </td>
<Td> Registration time </td>
<Td> Operation </td>
</Tr> ";

 

While ($ row = mysql_fetch_array ($ result )){
Echo "<tr>
<Td> ". $ row [id]." </td>
<Td> ". $ row [username]." </td>
<Td> ". $ row [gender]." </td>
<Td> ". $ row [age]." </td>
<Td> ". $ row [regdate]." </td>
<Td> <a href = modify. php? Id = ". $ row [id]."> edit </a> </td>
</Tr> ";
  }
Echo "</table> ";

} // Display the list content
Else
 {
If (! $ _ Post [OK])
  {
$ SQL = "select * from userinfo where id =". $ _ get [id];
$ Result = mysql_query ($ SQL );
$ Row = mysql_fetch_array ($ result );
?>
<Form method = post action = 'modify. php? Id = <? Echo $ _ get [id];?> '>
<?
Echo $ row [id]. "<br> ";
?>
<Input type = "hidden" name = "id" value = <? Echo $ row [id];? >>>
Name www.aimeige.com.cn <input type = text name = "username" value = <? Echo $ row [username];? >> <Br>
Gender <input type = text name = "gender" value = <? Echo $ row [gender];? >> <Br>
Age <input type = text name = "age" value = <? Echo $ row [age];? >> <Br>
Registration time <input type = text name = "regdate" value = <? Echo $ row ['regdate'];?> <Br>
<Input type = submit name = OK value = "submit">
</Form>
<?
} // If (! $ _ Post [OK])
Else {// processing after $ OK is activated:
   
$ SQL = "update userinfo set username = '". $ _ post [username]. "', gender = '". $ _ post [gender]. "', age = '". $ _ post [age]. "', regdate = '". $ _ post [regdate]. "'Where id = '". $ _ post [id]. "'";
$ Result = mysql_query ($ SQL );
If ($ result)
Echo "the record has been successfully modified <br> <a href = 'modify. Php'> continue to modify the record </a> ";
Else
Echo "failed to record modification <br> <a href = modify. php? Id = ". $ _ post [id]."> Return </a> ";
  }
} // Else ($ id)
} // End if
?>

Save record to database

The code is as follows: Copy code

<? Php
If ($ _ post [OK])
{
$ Link_id = mysql_connect ("localhost", "root", "") or die ("connection failed ");
If ($ link_id)
 {
// Select a database
Mysql_select_db ("my_test ");
// Insert data SQL statement
$ SQL = "insert into userinfo values ('". $ _ post [id]. "','". $ _ post [name]. "','". $ _ post [gender]. "','". $ _ post [age]. "','". $ _ post [regdate]. "')";
// Execute the SQL statement
$ Result = mysql_query ($ SQL );
If ($ result)
  {
Echo "the record has been inserted successfully <br> <a href = 'Insert. Php'> continue to insert the record </a> ";
  }
Else
Echo "an error occurred while executing the insert SQL statement ";
// Close the database
Mysql_close ($ link_id );
 }
}
Else
{
?>
<Form method = post action = insert. php>
No. <input type = text name = "id"> <br>
Name <input type = text name = "name"> <br>
Gender <input type = text name = "gender"> <br>
Age <input type = text name = "age"> <br>
Registration time <input type = text name = "regdate"> <br>
& Nbsp;
<Input type = submit name = OK value = "submit">
</Form>
<?
} // End if
?>

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.