Home > Backend Development > PHP Problem > How to use php and mysql to add, delete, modify and check classmate records

How to use php and mysql to add, delete, modify and check classmate records

PHPz
Release: 2023-03-31 09:57:58
Original
765 people have browsed it

In recent years, with the development of the Internet, various types of websites have emerged, among which educational websites have also attracted more and more attention. As a tool for recording student information, classmate directory has also become an indispensable feature of many school and community websites. When building the classmate record function, the combination of php and mysql allows us to easily add, delete, modify and check the classmate record.

1. Preparations before adding, deleting, modifying and checking classmate records

Before we start building the classmate record function, what we need to prepare is a basic template of a web page. In order to better reflect the role of php and mysql, some forms and buttons need to be added to the template to add, delete, modify and check the student records.

Add a table to the template to store various information in the classmate directory. Then you need to add some headers to the table to match the subsequent data.

2. To add classmates to the list

To add classmates, you need to create a form to input the information of the students to be added, including name, student number, major, class, etc. . In PHP, form submission and data storage can be achieved through code.

Code snippet:

<?php
if(isset($_GET[&#39;submit&#39;])){
$name = $_GET[&#39;name&#39;];
$id = $_GET[&#39;id&#39;];
$major = $_GET[&#39;major&#39;];
$class = $_GET[&#39;class&#39;];

$mysqli = new mysqli(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;,&#39;studentdatabase&#39;);
if(mysqli_connect_errno()){
echo mysqli_connect_error();
exit();
}

$insert_sql = "INSERT INTO student (name,id,major,class) VALUES (&#39;$name&#39;,&#39;$id&#39;,&#39;$major&#39;,&#39;$class&#39;)";

if($mysqli->query($insert_sql)){
echo "添加学生成功";
}else{
echo "添加学生失败";
}
}
?>

<form method="get" action="#">
姓名:<input type="text" name="name">
学号:<input type="text" name="id">
专业:<input type="text" name="major">
班级:<input type="text" name="class">
<input type="submit" name="submit" value="添加学生信息">
</form>
Copy after login

3. Editing the classmate record

The information in the classmate record can be edited. For this purpose, you need to add a Edit button. When the user clicks the edit button, an edit box should appear to modify the information. Similarly, in PHP, form submission and data storage are implemented through code.

Code snippet:

<?php
if(isset($_GET[&#39;modify&#39;])){
$id = $_GET[&#39;id&#39;];

$mysqli = new mysqli(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;,&#39;studentdatabase&#39;);
if(mysqli_connect_errno()){
echo mysqli_connect_error();
exit();
}

$select_sql = "SELECT * FROM student WHERE id = &#39;$id&#39;";

if($result = $mysqli->query($select_sql)){
$row = $result->fetch_assoc();
?>
<form method="get" action="#">
姓名:<input type="text" name="name" value="<?php echo $row[&#39;name&#39;]; ?>">
学号:<input type="text" name="id" value="<?php echo $row[&#39;id&#39;]; ?>" readonly>
专业:<input type="text" name="major" value="<?php echo $row[&#39;major&#39;]; ?>">
班级:<input type="text" name="class" value="<?php echo $row[&#39;class&#39;]; ?>">
<input type="submit" name="update" value="保存">
</form>

<?php
$result->free();
}
}

if(isset($_GET['update'])){
$name = $_GET['name'];
$id = $_GET['id'];
$major = $_GET['major'];
$class = $_GET['class'];

$mysqli = new mysqli('localhost','root','','studentdatabase');
if(mysqli_connect_errno()){
echo mysqli_connect_error();
exit();
}

$update_sql = "UPDATE student SET name='$name',major='$major',class='$class' WHERE id='$id'";

if($mysqli->query($update_sql)){
echo "学生信息修改成功";
}else{
echo "学生信息修改失败";
}
}
?>
Copy after login

4. Realize the deletion of the classmate record

The information in the classmate record can also be deleted. In php, the database is deleted by obtaining the data submitted by the user.

Code snippet:

<?php
if(isset($_GET[&#39;delete&#39;])){
$id = $_GET[&#39;id&#39;];

$mysqli = new mysqli(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;,&#39;studentdatabase&#39;);
if(mysqli_connect_errno()){
echo mysqli_connect_error();
exit();
}

$delete_sql = "DELETE FROM student WHERE id = &#39;$id&#39;";

if($mysqli->query($delete_sql)){
echo "学生信息删除成功";
}else{
echo "学生信息删除失败";
}
}
?>
Copy after login

5. Implement the query of the classmate record

The information in the classmate record can be searched by keywords. If you want to implement the query function of classmate records, you need to write query statements in PHP and provide users with a search box and search button on the web page.

Code snippet:

<?php
if(isset($_POST[&#39;search&#39;])){
$keyword = $_POST[&#39;keyword&#39;];

$mysqli = new mysqli(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;,&#39;studentdatabase&#39;);
if(mysqli_connect_errno()){
echo mysqli_connect_error();
exit();
}

$select_sql = "SELECT * FROM student WHERE name LIKE &#39;%$keyword%&#39;
OR id LIKE &#39;%$keyword%&#39; OR major LIKE &#39;%$keyword%&#39; OR class LIKE &#39;%$keyword%&#39;";

if($result = $mysqli->query($select_sql)){

while($row = $result->fetch_assoc()){
echo "<tr>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['major']."</td>";
echo "<td>".$row['class']."</td>";
echo "<td><a href=&#39;?modify=1&id=".$row[&#39;id&#39;]."&#39;>修改</a>  <a href=&#39;?delete=1&id=".$row[&#39;id&#39;]."&#39;>删除</a></td>";
echo "</tr>";
}

$result->free();
}
}
?>

<form method="post" action="#">
<input type="text" name="keyword" value="">
<input type="submit" name="search" value="搜索">
</form>
Copy after login

6. Summary

Through the above implementation, we can easily complete the functions of adding, editing, deleting and querying student records. The combination of php and mysql can greatly improve development efficiency and flexibility in implementing functions. I hope that the above content can provide reference and reference for beginners of PHP and MySQL.

The above is the detailed content of How to use php and mysql to add, delete, modify and check classmate records. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template