中介PHP连接前台HTML与数据库MySQL
1,保证各部分编码统一(utf8)
使用工具phpstudy【http://www.phpstudy.net/】
写单独PHP文件时,在<?php前面不能留空行,不然会报错
1.1在数据库配置文件mysql.ini
1.2建立新的数据库,表,每个字段时:
1.3在前端页面时定义编码
<meta charset="UTF-8">
2.PHP链接数据库页面active.php
1 2 3 4 5 | <?php $conn =mysql_connect( "localhost" , "root" , "root" ); mysql_select_db( "music" , $conn ); mysql_query( 'SET NAMES utf-8' ); ?> |
3.将数据嵌入html
<ul id="ul">
<?php
include("active.php");
$arr=mysql_query("select * from musicinfo",$conn);
while($res=mysql_fetch_row($arr)){
echo "<li href=".$res[4]."><b>".$res[1]."</b><span>".$res[2]."</span><a href=".$res[3]."></a></li>";
}
?>
</ul>
4,向数据库内添加资源(form表单提交)
<form action="" method="post">
<p><b>用户名:</b><input type="text" name="username"></p>
<p><b>密码:</b><input type="text" name="password"></p>
<input type="submit" value="注册" name="submit">
<form>
<?php include("active.php"); if(isset($_POST['submit']) and $_POST['submit']=="注册"){ $username=$_POST['username']; $password=$_POST['password']; $insert=mysql_query("insert into localauth(user_id,username,password) values(null,'$username','$password')",$conn); } ?>
5,删除某条特定的数据
<table>
<form action="" method="post" > <?php include 'base.php'; $arr=mysql_query("select * from musicinfo",$conn); while($res=mysql_fetch_row($arr)) echo "<tr><td>".$res[0]."</td><td>".$res[1]."</td><td>".$res[2]."</td><td>".$res[3]."</td>
<td>".$res[4]."</td><td><button name='delete' type='submit' value='".$res[0]."'>删除</button></td></tr>"; ?> </form> <?php if(isset($_POST['delete'])){ $thisId=$_POST['delete']; $del=mysql_query("delete from musicinfo where music_id='$thisId'",$conn); if($del){ echo "<script>alert('删除成功!');window.location.href='music_list.php';</script>;"; }else{ echo "<script>alert('失败!');</script>";
} } ?> </table>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?