preface
In PHP connect MySQL database to add delete to check. We created the MySQL data database and created the news table in the database. And the use of PHP for connection, we will be based on this further application.
A, modules,
1.1 Included Files
Since all operations are connected to the database, put the code to connect to the database in the include file.
Steps:
- Create the inc folder under the WWW folder:
- Create the conn.php file under Inc to connect to the database.
Connect to database:
$link=mysqli_connect('localhost'.'root'.'123456'.'data') or die(Error:.mysqli_connect_error());
mysqli_set_charset($link.'utf8');
? >
Copy the code
Second, data operation
2.1 Display Data
Steps:
- Connecting to the database
- To get the data
- Iterate over the loop data
Code:
<! DOCTYPE html> <html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="Width = device - width, initial - scale = 1.0">
<title>Document</title>
<style>
table {
width: 780px;
border: 1px solid # 000;
margin: auto;
}
th,td {
border: 1px solid # 000;
}
</style>
</head>
<body>
// 1. Connect to the database
require './inc/conn.php';
// 2. Obtain data
$resultSets=mysqli_query($link.'select * from news'); # Return result set object
$list=mysqli_fetch_all($resultSets, MYSQLI_ASSOC);
// print_r($list); # Match the result to an associative array
? >
<a href="./add.php"News > add < / a > < table > < tr > < th > id < / th > < th > name < / th > < th > skills < / th > < th > time < / th > < th > change < / th > < th > delete < / th >
foreach($list as $rows) :? >
<tr>
<td>
echo $rows['id']; ? ></td>
<td>
echo $rows['title']; ? ></td>
<td>
echo $rows['content']; ? ></td>
<td>
echo date('Y-m-d H:i:s'.$rows['createtime']); ? ></td>
<td><input type="button" value="Change"></td>
<td><input type="button" value="Delete" ></td>
</tr>
endforeach;? >
</tr>
</table>
</body>
</html>
Copy the code
Effect:
2.2 Adding Data
Create a new add.php folder in the WWW folder. Steps:
- Create a form
- Connecting to the database
- Write data to the database
Code:
<! DOCTYPE html> <html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="Width = device - width, initial - scale = 1.0">
<title>Document</title>
</head>
<body>
if (!empty($_POST)) {
// 2. Connect to the database
require './inc/conn.php';
$time = time();
$sql = "insert into news values (null, '{$_POST['name']}', '{$_POST['content']}', $time)";
// 3. Execute the SQL statement
if (mysqli_query($link.$sql)) {
header('location:./list.php');
}
else {
echo Insert error, error code:.mysqli_errno($link),'<br/>';
echo Error message:.mysqli_error($link); }}? ><! --1Create form --> <form action="" method="post"<input type="text" name="name"<br/><br/> skill: <textarea name="content" cols="30" rows="5"></textarea><br/><br/>
<input type="submit" name="button" value="Submit">
</form>
</body>
</html>
Copy the code
Effect:
2.3 Deleting Data
Step entry: list.php
- Click the delete button on the list. PHP page to go to the del.php page and pass in the deleted ID
- Connect to the database on the del.php page
- Delete data by ID
- When the deletion is successful, jump to list.php
List.php ();
<! DOCTYPE html> <html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="Width = device - width, initial - scale = 1.0">
<title>Document</title>
<style>
table {
width: 780px;
border: 1px solid # 000;
margin: auto;
}
th,td {
border: 1px solid # 000;
}
</style>
</head>
<body>
// 1. Connect to the database
require './inc/conn.php';
// 2. Obtain data
$resultSets=mysqli_query($link.'select * from news'); # Return result set object
$list=mysqli_fetch_all($resultSets, MYSQLI_ASSOC);
// print_r($list); # Match the result to an associative array
? >
<a href="./add.php"News > add < / a > < table > < tr > < th > id < / th > < th > name < / th > < th > skills < / th > < th > change < / th > < th > delete < / th >
foreach($list as $rows) :? >
<tr>
<td>
echo $rows['id']; ? ></td>
<td>
echo $rows['title']; ? ></td>
<td>
echo $rows['content']; ? ></td> <! -- <td>
echo date('Y-m-d H:i:s'.$rows['createtime']); ? ></td> -->
<td><input type="button" value="Change"></td>
<td><input type="button" value="Delete" onclick="If (confirm(' Confirm delete? ')) location.href='./del.php? id=<? php echo$rows['id'] ? > '"></td>
</tr>
endforeach;? >
</tr>
</table>
</body>
</html>
Copy the code
Del.php code:
// 1. Connect to the database
require './inc/conn.php';
// 2
$sql = "delete from news where id = {$_GET['id']}";
// 3. Execute the SQL statement
if (mysqli_query($link.$sql)) {
header('location:./list.php');
}
else {
echo 'Deletion failed';
}
? >
Copy the code
Effect:
2.4 Modifying Data
Entry: list. PHP
Step: Step 1: display the modified page 1, connect to the database 2, obtain the modified data 3, display the data in the column form
Step 2: Execute modify logic 1, obtain new data 2, concatenate modified SQL statement, execute modify logic
List. The PHP code:
<! DOCTYPE html> <html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="Width = device - width, initial - scale = 1.0">
<title>Document</title>
<style>
table {
width: 780px;
border: 1px solid # 000;
margin: auto;
}
th,td {
border: 1px solid # 000;
}
</style>
</head>
<body>
// 1. Connect to the database
require './inc/conn.php';
// 2. Obtain data
$resultSets=mysqli_query($link.'select * from news'); # Return result set object
$list=mysqli_fetch_all($resultSets, MYSQLI_ASSOC);
// print_r($list); # Match the result to an associative array
? >
<a href="./add.php"News > add < / a > < table > < tr > < th > id < / th > < th > name < / th > < th > skills < / th > < th > change < / th > < th > delete < / th >
foreach($list as $rows) :? >
<tr>
<td>
echo $rows['id']; ? ></td>
<td>
echo $rows['title']; ? ></td>
<td>
echo $rows['content']; ? ></td> <! -- <td>
echo date('Y-m-d H:i:s'.$rows['createtime']); ? ></td> -->
<td><input type="button" value="Change" onclick="location.href='./edit.php? id=<? php echo$rows['id'] ? > '"></td>
<td><input type="button" value="Delete" onclick="If (confirm(' Confirm delete? ')) location.href='./del.php? id=<? php echo$rows['id'] ? > '"></td>
</tr>
endforeach;? >
</tr>
</table>
</body>
</html>
Copy the code
Edit. PHP code:
// Connect to the database
require './inc/conn.php';
// 1. Obtain the modified data
$sql="select * from news where id={$_GET['id']}";
$rs=mysqli_query($link.$sql); // Get the modified data
$row=mysqli_fetch_assoc($rs); // Match the modified data into a one-dimensional associative array
// 2. Execute the modification logic
if (!empty($_POST)) {
$id=$_GET['id'];
$title=$_POST['name'];
$content=$_POST['content'];
$sql="update news set title='$title',content='$content' where id=$id";
if (mysqli_query($link.$sql)) {
header('location:list.php');
}
else {
echo Error:.mysqli_error($link);
}
exit;
}
? ><! DOCTYPE html> <html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="Width = device - width, initial - scale = 1.0">
<title>Document</title>
</head>
<body>
<form action="" method="post"<input type="text" name="name" value="<? php echo$row['title']? >"<br/><br/> skill: <textarea name="content" cols="30" rows="5">
echo $row['content']? ></textarea><br/><br/>
<input type="submit" name="button" value="Submit">
<input type="button" name="button" value="Return" onclick="location.href='list.php'">
</form>
</body>
</html>
Copy the code
Effect:
On the way to learning PHP, if you find this article helpful to you, then please pay attention to like comment 3 times, thank you, your must be another support of my blog.