Home > Article > Backend Development > PHP implementation code to delete records and refresh the current page
In this article, we will introduce a simple code that deletes database records and refreshes the current page at the same time. It does not use ajax, but uses the get method to refresh the current page. The experience may not be very good in actual applications. It is only for your reference.
Requirements: 1. Display the query data on the page, click "Delete" to delete the data and refresh the current page. 2. Obtain deletion conditions through GET method. 1. Database connection variable connectvars.php <?php //服务器 define('DB_HOST', 'localhost'); //用户名 define('DB_USER', 'root'); //密码 define('DB_PASSWORD', 'root'); //数据库 define('DB_NAME','test') ; ?> 2. Record display page display.php <?php /** * 记录展示页 * 点击删除,删除掉数据,同时刷新当前页面 * site bbs.it-home.org */ require_once 'connectvars.php'; $dbc = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME); //如果调用此页面时,页面链接中有‘DelID’变量,则获得要删除记录的‘ID’号,进行删除 if(isset($_GET['DelID'])){ $query = "DELETE FROM toyota WHERE ID = ".$_GET['DelID']." LIMIT 1"; mysqli_query($dbc,$query); } //查出所有记录,并在接下来的表格中进行显示(如果上面的删除代码被执行,此处相当于刷新页面) $query = "SELECT * FROM toyota ORDER BY ID DESC"; $data = mysqli_query($dbc,$query); //统计所查询出的记录条数 $count = mysqli_num_rows($data); ?> <html> <head> <title>数据展示页</title> </head> <body> <table> <!-- 表格列名 --> <tr> <th>标题</th> <th>来源</th> <th>车型</th> <th>主要部件</th> <th>操作</th> </tr> <?php //循环输出列表元素:title、source、carType、majorPart,后加一个"删除"链接 while($row = mysqli_fetch_array($data)){ echo '<tr>'; echo '<td>'.$row['title'].'</td>'; echo '<td>'.$row['source'].'</td>'; echo '<td>'.$row['carType'].'</td>'; echo '<td>'.$row['majorPart'].'</td>'; //点击"删除"链接,调用自身页面,同时在页面链接上增加‘DelID’变量,赋值为该记录在数据库中的‘ID’号,用于GET方式获得 echo '<td>删除</td>'; echo '</tr>'; } ?> </table> </body> </html> |