1 <html> 2 <head> 3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 4 <title>無標題文檔</title> 5 </head> 6 7 <body> 8 <form action="" method="post">//創建表單 9 姓名 10 <input type="text" name="str"> 11 <input type="submit" name="submit"> 12 </form> 13 <?php 14 header("Content-type:text/html;charset=utf-8"); 15 16 if(isset($_POST['submit']))//確認是否單擊提交按鈕 17 { 18 $name=$_POST['str'];//通過post方式獲取表單數據並存入到$name變量中 19 $link=mysql_connect("localhost","root","207207");//鏈接數據庫 20 if($link) 21 { 22 echo"連接數據庫成功!"; 23 mysql_query('SET NAMES UTF8');//設置數據庫編碼解決無法輸入中文的問題 24 $sel=mysql_select_db("db_student");//選擇數據庫 25 if($sel) 26 { 27 echo"選擇數據庫成功"; 28 $str="insert into tb_student values("."\"".$name."\"".");";//mysql數據庫語句 29 mysql_query($str);//執行mysql語句 30 } 31 else 32 { 33 echo"選擇數據庫失敗"; 34 } 35 } 36 else 37 { 38 echo"連接數據庫失敗!"; 39 exit; 40 } 41 42 } 43 ?> 44 45 </body> 46 </html>
從數據庫中輸出也是同樣道理