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
Id |
Title |
Author |
Source |
Content |
Date |
Update |
Delete |
<? 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"
{$ 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...