php 简单操作数据库
<?php header("content-type:text/html;charset=utf-8"); /*//造一个连接 $connect = @mysql_connect("localhost","root","123"); //选择要操作的数据库 mysql_select_db("mydb",$connect); //写SQL语句 $sql = "select * from Nation"; //执行SQL语句,返回结果集 $result = mysql_query($sql); //从结果集中读取数据 while($attr = mysql_fetch_row($result)) { var_dump($attr); } */ //造一个连接 $connect = @mysql_connect("localhost","root","123"); //选择要操作的数据库 mysql_select_db("mydb",$connect); //写SQL语句 $sql = "insert into Nation values('n007','壮族')"; //执行SQL语句,返回结果集 $result = mysql_query($sql); //从结果集中读取数据 var_dump($result); //中文乱码解决方法:原因编码格式不一致 //1.建立数据库的时候,字符集选择utf-8 //2.修改mysql的配置:在[mysqld]模块下面添加character_set_server=utf8 //3.修改页面的编码格式为uft-8,header("content-type:text/html;charset=utf-8
<body>
<form action="chuli.php" method="post">
<div>民族代号:<input type="text" name="code" /></div>
<div>民族名称:<input type="text" name="name" /></div>
<input type="submit" value="提交" />
</form>
</body>
<script type="text/javascript">
confirm(确定删除么);
</script>
</html>
<?php $code = $_POST["code"]; $name = $_POST["name"]; //造连接 $conn = @mysql_connect("localhost","root","123"); //选择操作的数据库 mysql_select_db("mydb",$conn); //写SQL语句 $sql = "insert into Nation values('{$code}','{$name}')"; //执行 $result = mysql_query($sql); if($result) { //跳转页面 header("location:lizi.php"); } else { echo "添加失败!"; }
<?php $code = $_GET["code"]; //造连接 $conn = @mysql_connect("localhost","root","123"); //选择操作的数据库 mysql_select_db("mydb",$conn); //写SQL语句 $sql = "delete from Nation where Code = '{$code}'"; //执行 $result = mysql_query($sql); if($result) { //跳转页面 header("location:chaxun.php"); } else { echo "删除失败!"; }
<table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>民族代号</td> <td>民族名称</td> <td>操作</td> </tr> <?php $conn = @mysql_connect("localhost","root","123"); //选择操作的数据库 mysql_select_db("mydb",$conn); //写SQL语句 $sql = "select * from Nation"; //执行 $result = mysql_query($sql); //读取数据,造option while($attr = mysql_fetch_row($result)) { echo "<tr> <td>{$attr[0]}</td> <td>{$attr[1]}</td> <td><a onclick=\"return confirm('确定删除么')\" href='delete.php?code={$attr[0]}'>删除</a></td> </tr>"; } ?> </table>