Php Data access addition, deletion, modification, and query operations, php Data access addition and deletion _ PHP Tutorial

Source: Internet
Author: User
Tags learn php programming
Add, delete, modify, and query php Data access. Add, delete, modify, and query operations for php Data access, and small exercises for adding, deleting, modifying, and querying php Data access, trainer I. view the news page ----- add, delete, modify, and query of htmlheadmetahttp-equivConten php Data access on the home page, and add and delete php Data Access

Add, delete, modify, query, and perform small exercises.

1. view the news page ----- homepage

 View News View News
 
 <? Php $ db = new MySQLi ("localhost", "root", "", "mydb ");! Mysqli_connect_error () or die ("connection failed! "); $ SQL =" select * from news "; $ result = $ db-> query ($ SQL); $ arr = $ result-> fetch_all (); foreach ($ arr as $ v) {echo" 
   ";}?> 
  
Id Title Author Source Content Date Update Delete
{$ V [0]} {$ V [1]} {$ V [2]} {$ V [3]} {$ V [4]} {$ V [5]} Update Delete


Publish news

  

II. publish a news page ----- Add content

 Publish news  
 
  
Publish news
    

  

Processing of submitted content:

<? Php $ newsid =$ _ POST ["newsid"]; $ title =$ _ POST ["title"]; $ author =$ _ POST ["author"]; $ source = $ _ POST ["source"]; $ content = $ _ POST ["content"]; $ time = date ("Y-m-d ", time (); $ db = new MySQLi ("localhost", "root", "", "mydb ");! Mysqli_connect_error () or die ("Contact failed! "); $ SQL =" insert into news values ('{$ newsid}', '{$ title}', '{$ author}', '{$ source }', '{$ content}', '{$ time}') "; $ result = $ db-> query ($ SQL); if ($ result) {header (" location: xinwen. php ");} else {echo" failed to add news! ";}

3. delete content

<? Php $ newsid = $ _ GET ["newsid"]; $ db = new MySQLi ("localhost", "root", "", "mydb ");! Mysqli_connect_error () or die ("connection failed! "); $ SQL =" delete from news where newsid = '{$ newsid}' "; $ result = $ db-> query ($ SQL); if ($ result) {header ("location: ChaKan. php ") ;}else {echo" failed to delete data ";}?>
  

4. modify the news page-modify the news content and submit it for viewing

 Modify News 
 
  
Modify News
 <? Php $ newsid = $ _ GET ["newsid"]; $ db = new MySQLi ("localhost", "root", "", "mydb "); $ sinfo = "select * from news where newsid = '{$ newsid}'"; $ r = $ db-> query ($ sinfo ); $ arr = $ r-> fetch_row (); // all information about this person?>  

  

After the modification is submitted, process the modification:

<? Php $ newsid =$ _ POST ["newsid"]; $ title =$ _ POST ["title"]; $ author =$ _ POST ["author"]; $ source = $ _ POST ["source"]; $ content = $ _ POST ["content"]; $ time = date ("Y-m-d ", time (); $ db = new MySQLi ("localhost", "root", "", "mydb ");! Mysqli_connect_error () or die ("Contact failed! "); $ SQL =" update news set title = '{$ title}', author = '{$ author}', source = '{$ source }', content = '{$ content}', time = '{$ time}' where newsid = '{$ newsid }'"; $ result = $ db-> query ($ SQL); if ($ result) {header ("location: Update. php ");} else {echo" failed to modify data! ";}

The above is all the content of this article. I hope it will help you learn php programming.

Tips: add, delete, modify, and query operations. trainer I. view the news page ----- htmlheadmeta http-equiv = "Conten...

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.