zoukankan      html  css  js  c++  java
  • 中介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>
  • 相关阅读:
    poj 1236 Network of Schools 强连通分量 (添加几条边可以使整个图变成强连通图)
    poj 2363 Sightseeing ( 找次短路和最短路的条数 )
    poj 3013
    poj 1679 The Unique MST 最小生成树
    poj 1797 Heavy Transportation (spfa 最短路 )
    hdu 2680 Choose the best route (spfa)
    畅通工程续
    find the longest of the shortest
    SimpleDateFormate的使用
    SimpleDateFormate的使用
  • 原文地址:https://www.cnblogs.com/yuanyuan0809/p/5312304.html
Copyright © 2011-2022 走看看