自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

php中數(shù)組插入mysql表的方法

數(shù)據(jù)庫(kù) MySQL
在用PHP開(kāi)發(fā)時(shí),如何才能將數(shù)據(jù)插入到mysql表中呢?下文對(duì)該方法的實(shí)現(xiàn)方法步驟進(jìn)行了詳述,供您參考學(xué)習(xí)之用。

下面為您介紹的數(shù)組插入mysql表的方法用于PHP開(kāi)發(fā),如果您之前在使用mysql表時(shí)遇到過(guò)類(lèi)似的方面,不妨一看。

環(huán)境:PHP 5.0 + MYSQL

第一步:我們先來(lái)創(chuàng)建一個(gè)表格來(lái)存儲(chǔ)數(shù)組變量的值,就是用mysql自帶的test數(shù)據(jù)庫(kù)吧

  1. Use test;  
  2.  
  3. Creat table ‘NEIL’ ( ‘ID’ int(20) primary key not null,  
  4.  
  5.                                ‘Major’ varchar(25) not null);  
  6.  

第二步:我們來(lái)建立一個(gè)復(fù)選框頁(yè)面用來(lái)模擬一個(gè)實(shí)例PHP數(shù)組 index.html

  1. <body> 
  2.  
  3. <form method="post" action="show.php"> 
  4.  
  5. ID: <input type=”text” name=”id”><br> 
  6.  
  7. <input type="checkbox" name="teach[0]" value="online">Online<br> 
  8.  
  9. <input type="checkbox" name="teach[1]" value="video">video<br> 
  10.  
  11. <input type="checkbox" name="teach[2]" value="face">Face-to-Face<br> 
  12.  
  13. <input type="submit" value="submit"> 
  14.  
  15. </form> 
  16.  

第三步:我們來(lái)描述show.php

Index.html通過(guò)post 將變量提交給show.php來(lái)處理,我們先來(lái)寫(xiě)show.php代碼

  1. <?php 
  2.  
  3. $db_name=”test”;  
  4.  
  5. $table_name=”neil”;  
  6.  
  7. $connection= @mysql_connect(“localhost”,”root”,”root”) or die(mysql_error());  
  8.  
  9. $db= @mysql_select_db($db_name,$connection) or die (mysql_error());  
  10.  
  11. /*這里我們來(lái)進(jìn)行數(shù)組變量的處理*/  
  12.  
  13. $value=’’; //定義一個(gè)變量value初始化為空  
  14.  
  15. Foreach($_POST[“teach”] as $key)  
  16.  
  17. { $value.=$key.’,’;} /*將數(shù)組值傳遞給中間變量key, 由key 將值傳依次遞給變量value */  
  18.  
  19. /*插入語(yǔ)句*/  
  20.  
  21. $sql=”insert into neil values (‘$_POST[id]’,’$value’)”;  
  22.  
  23. $query= @mysql_query($sql,$connection) or die(mysql_error());  
  24.  
  25. ?> 
  26.  

到這里這個(gè)php中數(shù)組插入mysql表的方法就結(jié)束了。
 

 

 

 

【編輯推薦】

MySQL遍歷數(shù)據(jù)表的方法

MySQL批量導(dǎo)入數(shù)據(jù)的實(shí)現(xiàn)

php mysql創(chuàng)建臨時(shí)表

mysql快速建表的方法

深入研究MySQL刪除多表數(shù)據(jù)

責(zé)任編輯:段燃 來(lái)源: 互聯(lián)網(wǎng)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)