中介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>
复制代码

 

posted @   小金鱼紫苏  阅读(7420)  评论(0编辑  收藏  举报
编辑推荐:
· 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爆火,是硬核还是营销?
点击右上角即可分享
微信分享提示