中介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
<?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>