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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM