How to add, delete, modify and query database with PHP + MySQL

Time:2021-2-21

Methods of adding, deleting, modifying and querying database by PHP + MySQL: 1【 index.html 】2. Click the Add button to access the database【 addnews.html 】Add data; 3. Click the delete button, through the server file [action]- del.php 】Delete.

1. Query database

1.1. Create file dbconfig.php , save constant

define("HOST","localhost");   
  
define("USER","root");   
  
define("PASS","\*\*\*\*\*\*\*\*");  
  
define("DBNAME","news");

1.2. Create entry file index.html (connect database, query data)

< !DOCTYPE html>  

  



  

News background management system
  

  
  
  
.wrapper {width: 1000px;margin: 20px auto;}  
  
h2 {text-align: center;}  
  
.add {margin-bottom: 20px;}  
  
.add a {text-decoration: none;color: #fff;background-color: green;padding: 6px;border-radius: 5px;}  
  
td {text-align: center;}  
  

  

  

  
News background management system
  

  
Add news  
  

  
  
  


ID

title

keyword

author

Release time

content

operation


  
";  
  
echo "{$row['id']}";  
  
echo "{$row['title']}";  
  
echo "{$row['keywords']}";  
  
echo "{$row['autor']}";  
  
echo "{$row['addtime']}";  
  
echo "{$row['content']}";  
  
echo "

delete

modify

  ";  
  
echo "";  
  
}  
  
//5. Release the result set  
  
mysql\_free\_result($result);  
  
mysql\_close($link);  
  
?>  




  
  
  
function del (id) {  
  
if (confirm("确定delete这条新闻吗?")){  
  
window.location = "action-del.php?id="+id;  

}

}

The page is as follows:

2. Add news

2.1 click the Add button to open the page addnews.html Add data

< !DOCTYPE html>  

  

  

    

    Add news

  

  
  
form{  
  
margin: 20px;  
  
}  

   
  


  

       News ID:    
  
       title:  
  
       keyword:  
  
       Author:  

       Release time:  
  
       Content:

2.2 create a server file to handle the added newsaction-addnews.php

3. Delete news

Click the delete button, through the server fileaction-del.phpDelete it

4. Revise the news

4.1 click the Modify button to jump to the file editnews.php Modify it

< !DOCTYPE html>  

  



          

        Revise the news





  
  
  

       News ID:  
  
       title:  
  
       keyword:  
  
       Author:  

       Release time:  
  
       Content:

4.2 through the server file action- editnews.php Modify it

Through the server file action- editnews.php Modify it

//A page that handles editing operations  

require "dbconfig.php";  

//Connect to MySQL  

$link = @mysql\_ Connect (host, user, pass) or die ("prompt: database connection failed! ");  

//Select database  

mysql\_select\_db(DBNAME,$link);  

//Coding settings  

mysql\_set\_charset('utf8',$link);  

//Get modified news  

$id = $\_POST\['id'\];  

$title = $\_POST\['title'\];  

$keywords = $\_POST\['keywords'\];  

$autor = $\_POST\['autor'\];  

$addtime = $\_POST\['addtime'\];  

$content = $\_POST\['content'\];  

//Update data  

mysql\_ Query ("update news set title = '$title', keywords = '$keywords', autor =' $autor ', addtime =' $addtime ', content =' $content 'where id = $ID', $link) or die ('error in modifying data: '. MySQL')\_ error());  

header("Location:demo.php");

More learning content can be accessedAs long as you can read it, your salary will go up a step

The above contents hope to help youA lot of PHPer always encounter some problems and bottlenecks when they are upgrading. They write too much business code and have no sense of direction. They don’t know where to start to improve. For this, I have sorted out some materials, including but not limited to:Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, tp6, laravel, yii2, redis, spool, swoft, Kafka, MySQL optimization, shell script, docker, microservice, nginxAnd so on many knowledge points, advanced dry goods need can be free to share with you, need can join mePHP Technology Exchange Group953224940

Advanced PHP monthly salary 30K > > > architect growth path [free access to videos and interview documents]