PHP串連MySQL資料庫並向資料庫增加記錄

來源:互聯網
上載者:User

首先需要通過PHP來串連MySQL資料庫:

#串連資料庫

下面是最簡單的PHP串連MySQL資料庫的代碼:

 代碼如下 複製代碼


<?php  
$link=mysql_connect("localhost","root","password");  
if (!$link) echo "connect error";  
else echo "connect ok";  
?>

其中mysql_connect()串連函數,localhost代表資料庫伺服器的地址,root是MySql資料庫使用者名稱,password是MySql資料庫的密碼。使用時改成自己的即可。

為了更方便以後使用,將串連代碼正常化一下:

 代碼如下 複製代碼
<?php  
$link_host='localhost';  
$link_user='root';  
$link_pass='password';  
   
$link=mysql_connect($link_host,$link_user,$link_pass);  
   
if ($link)  
{  
echo "connect ok!";  
}  
else
{  
echo "connect fail!";  
}  
?>

 

用三個變數來讀取伺服器位址、使用者名稱和密碼,方便以後進行表單讀取和賦值等。

#建立資料庫代碼

 代碼如下 複製代碼
<?php 
include ("conn.php");
  
$link_db='link_system'; 
//設定要建立的資料庫的名字,一定不能跟已有的資料庫名稱相同
  
if ($link) 

echo "connect ok!<br />"; 
if (mysql_query("create database ".$link_db,$link)) 

echo "database created!<br />"; 

else

echo "database create fail!"; 


else

echo "connect error!"; 

?>

建立了link_system資料庫之後,還需要建立表格。

#建立資料庫表格
//設定需要建設的表格為link_table,下面是需要建立的表名,用來儲存不同的資料,可以根據自己的需要來設定。
link_id 資料的id
link_name 友鏈名稱
link_url 友鏈網址
link_detail 簡介
link_contact 連絡方式
link_show 是否顯示
link_order 排列順序
link_sort 分類


//因為我們友鏈表中有分類,所以需要建立一個分類表link_sorts,我的設想是存友鏈顯示的位置,比如首頁或者頻道頁、內頁等。
sort_id 資料id
sort_name 分類名稱

建立表格的完整PHP代碼如下:

 

 代碼如下 複製代碼
<?php 
//選擇操作的資料庫 
mysql_select_db($link_db,$link); 
  
//建立表格 
$link_table = "create table link_table 

link_id int unsigned primary key not null auto_increment, 
link_name varchar(20) not null, 
link_url varchar(50) not null, 
link_detail varchar(100) not null, 
link_contact varchar(100) not null, 
link_show int unsigned not null, 
link_order int unsigned not null, 
link_sort int unsigned not null 
)"; 
  
$sort_table = "create table sort_table 

sort_id int unsigned primary key not null auto_increment, 
sort_name varchar(20) not null 
)"; 
  
//執行建表操作 
if(!mysql_query($link_table,$link)){ 
echo "Create link_table error :" . mysql_error() . "<br />"; 

else { 
echo "link_table Created!" . "<br />"; 

  
  
if(!mysql_query($sort_table,$link)){ 
echo "Create sort_table error :" . mysql_error() . "<br />"; 

else { 
echo "sort_table Created!" . "<br />"; 

  
//執行完畢關閉資料庫連接 
mysql_close($link); 
?>

首先建立一個表格,用來填寫需要向MySQL資料庫寫入的資料:

#寫入資料庫

 代碼如下 複製代碼

//insert.php


<form action="insert_ok.php" method="post">
網站名稱: <input type="text" name="site_name" />
<br />
網站連結: <input type="text" value="http://" name="site_url" />
<br />
簡介: <input type="text" value="無" name="site_detail" />
<br />
連絡方式: <input type="text" name="site_contact" />
<br />
排序: <input type="text" value="1" name="site_order" />
<br />
分類: <input type="text" value="1" name="site_sort" />
<br />
是否顯示: <input name="site_show" type="checkbox" id="checkbox" value="1" checked="checked"  />
<br />
<input type="submit" />
</form>

裡其他的都是用文字框輸入,而是否顯示使用複選框來實現,預設選中。

執行寫入的程式頁面

 代碼如下 複製代碼

//insert_ok.php


<?php 
include ("conn.php");
  
//讀取上個頁面中表單中的資料
$link_name=$_POST[site_name];
$link_url=$_POST[site_url];
$link_contact=$_POST[site_contact];
$link_detail=$_POST[site_detail];
$link_order=$_POST[site_order];
$link_sort=$_POST[site_sort];
$link_show=$_POST[site_show];
  
if (!$link_show=="1") $link_show="0";
//複選框是否選中,如果沒有選中則賦值為0
  
mysql_select_db("link_system", $link); //選擇資料庫link_system 
  
if($_POST)
{
    $sql = "INSERT INTO link_table (link_name,link_url,link_contact,link_detail,link_order,link_sort,link_show) VALUES ('$link_name','$link_url','$link_contact','$link_detail','$link_order','$link_sort','$link_show')";
    if(!mysql_query($sql,$link))
    {
        echo "添加資料失敗:".mysql_error();
    }
    else
    {
        echo "添加資料成功!";
 echo $_POST[site_name]."<br>".$_POST[site_url]."<br>".$_POST[site_contact]."<br>".$_POST[site_detail]."<br>".$_POST[site_order]."<br>".$_POST[site_sort]."<br>".$_POST[site_show];
    }
}
?>

如果執行成功,則添加友鏈資料完成,至於分類暫時先不添加,到後期再將分類加入裡面。下一步則是顯示資料、編輯資料和刪除資料的實現了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.