數(shù)組吧,直接把數(shù)組轉(zhuǎn)字符串啊
我們擁有10多年網(wǎng)頁設(shè)計和網(wǎng)站建設(shè)經(jīng)驗(yàn),從網(wǎng)站策劃到網(wǎng)站制作,我們的網(wǎng)頁設(shè)計師為您提供的解決方案。為企業(yè)提供成都網(wǎng)站制作、網(wǎng)站設(shè)計、微信開發(fā)、小程序開發(fā)、手機(jī)網(wǎng)站開發(fā)、H5技術(shù)、等業(yè)務(wù)。無論您有什么樣的網(wǎng)站設(shè)計或者設(shè)計方案要求,我們都將富于創(chuàng)造性的提供專業(yè)設(shè)計服務(wù)并滿足您的需求。
implode() 函數(shù)返回由數(shù)組元素組合成的字符串。(適合一維數(shù)組)
$arr = array('Hello', 'World', 'I', 'love', 'Shanghai');
1 echo implode(" ",$arr);//加空格
the result : Hello World I love Shanghai
2 echo implode(",",$arr);//加逗號
the result : Hello,World,I,love,Shanghai
轉(zhuǎn)換數(shù)組為字符串后插入數(shù)據(jù)庫就可以了。
1:首先要使用PHP的超全局變量 $_GET 和 $_POST 用于收集表單數(shù)據(jù)(form-data)
2:然后使用INSERT INTO 語句用于向數(shù)據(jù)庫表中插入新記錄。
具體示例:
(1)首先創(chuàng)建了一個名為 "Persons" 的表,有三個列:"Firstname", "Lastname" 以及 "Age"。
?php
$con?=?mysql_connect("localhost","peter","abc123");
if?(!$con)
{
die('Could?not?connect:?'?.?mysql_error());
}
mysql_select_db("my_db",?$con);
mysql_query("INSERT?INTO?Persons?(FirstName,?LastName,?Age)?
VALUES?('Peter',?'Griffin',?'35')");
mysql_query("INSERT?INTO?Persons?(FirstName,?LastName,?Age)?
VALUES?('Glenn',?'Quagmire',?'33')");
mysql_close($con);
?
(2)其次創(chuàng)建一個 HTML 表單,這個表單可把新記錄插入 "Persons" 表。
html
body
form?action="insert.php"?method="post"
Firstname:?input?type="text"?name="firstname"?/
Lastname:?input?type="text"?name="lastname"?/
Age:?input?type="text"?name="age"?/
input?type="submit"?/
/form
/body
/html
(3)接著當(dāng)用戶點(diǎn)擊上例中 HTML 表單中的提交按鈕時,表單數(shù)據(jù)被發(fā)送到 "insert.php"。"insert.php" 文件連接數(shù)據(jù)庫,并通過
$_POST 變量從表單取回值。然后,mysql_query() 函數(shù)執(zhí)行 INSERT INTO 語句,一條新的記錄會添加到數(shù)據(jù)庫表中。
?php
$con?=?mysql_connect("localhost","peter","abc123");
if?(!$con)
{
die('Could?not?connect:?'?.?mysql_error());
}
mysql_select_db("my_db",?$con);
$sql="INSERT?INTO?Persons?(FirstName,?LastName,?Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
if?(!mysql_query($sql,$con))
{
die('Error:?'?.?mysql_error());
}
echo?"1?record?added";
mysql_close($con)
?
如果你會點(diǎn)php基礎(chǔ)我可以給你點(diǎn)參考意見
一個是,你這個文件肯定是txt格式的
$fp = fopen('xxx.txt','r');//用只讀方式打開文件
$getsource = fgets($fp);//讀取第一行,主要是用于去除第一行
while(!feof($fp)){
$getsource = fgets($fp);//獲取有用數(shù)據(jù)
$insource = explode(" ", $getsource);//把一行文字變成數(shù)組,按符號分割
/*---------------下面寫數(shù)據(jù)存儲的sql-------------------*/
......
/*---------------上面寫數(shù)據(jù)存儲的sql-------------------*/
}
我默認(rèn)為你對php和mysql是熟悉的,不然你也不會打算用php寫這個程序,我只提供你點(diǎn)思路,php對文件操作不熟悉的話你可以參考下這篇文章
;id=77class=2
希望對你有幫助
!--表單文件,拷入index.php--
!DOCTYPE?html
html
head
style
label{display:inline-block;width:100px;margin-bottom:10px;}
/style
titleAdd?students/title
/head
body
!--?數(shù)據(jù)庫用mysqli?面向過程調(diào)用方法--
form?method="post"?action="write2db.php"
!--數(shù)據(jù)庫用mysqli?面向過程調(diào)用方法
form?method="post"?action="write2db_sqlio.php"
--
!--數(shù)據(jù)庫用PDO調(diào)用方法
form?method="post"?action="write2db_pdo.php"
--
labelFirst?Name/label
input?type="text"?name="first_name"?/
br?/
labelLast?Name/label
input?type="text"?name="last_name"?/
br?/
labeldepartment/label
input?type="text"?name="department"?/
br?/
labelEmail/label
input?type="text"?name="email"?/
br?/
input?type="submit"?value="Add?students"
/form
/body
/html
------------------------------
?php
//拷貝命名為write2db.php,數(shù)據(jù)庫用mysqli?面向過程調(diào)用方法
//print_r($_POST);
//?create?a?variable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$department=$_POST['department'];
$email=$_POST['email'];
//調(diào)試用
echo?"Your?input:?";
echo?$first_name;
echo?'br?/';
echo?$last_name;
echo?'br?/';
echo?$department;
echo?'br?/';
echo?$email;
echo?'br?/';
$servername?=?"localhost";
//Your?database?username?and?password
//$username?=?"username";
//$password?=?"password";
$username?=?"tester";
$password?=?"testerPassword";
//your?database?name
$dbname?=?"test";
$tablename?="student";
//?Create?connection
$connect?=?mysqli_connect($servername,?$username,?$password,?$dbname);
if?(!$connect)?{
die("Connection?failed:?"?.?mysqli_connect_error());
}
//Execute?the?query
$sql="INSERT?INTO?$tablename?(first_name,last_name,department,email)
VALUES('$first_name','$last_name','$department','$email')";
if?(mysqli_query($connect,?$sql))?{
echo?"Hooray!?New?record?is?inserted?to?database?successfully.?Please?check?database.";
}?else?{
echo?"Error:?"?.?$sql?.?"br?/"?.?mysqli_error($connect);
}
mysqli_close($connect);
?
?php
//拷貝命名為write2db_sqlio.php,數(shù)據(jù)庫用mysqli?面向?qū)ο笳{(diào)用方法
//print_r($_POST);
//?create?a?variable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$department=$_POST['department'];
$email=$_POST['email'];
//調(diào)試用
echo?"Your?input:?";
echo?$first_name;
echo?'br?/';
echo?$last_name;
echo?'br?/';
echo?$department;
echo?'br?/';
echo?$email;
echo?'br?/';
$servername?=?"localhost";
//Your?database?username?and?password
//$username?=?"username";
//$password?=?"password";
$username?=?"tester";
$password?=?"testerPassword";
//database?name
$dbname?=?"test";
$tablename?="student";
//?Create?connection
$conn?=?new?mysqli($servername,?$username,?$password,?$dbname);
//?Check?connection
if?($conn-connect_error)?{
die("Connection?failed:?"?.?$conn-connect_error);
}?
$sql="INSERT?INTO?$tablename?(first_name,last_name,department,email)
VALUES('$first_name','$last_name','$department','$email')";
if?($conn-query($sql)?===?TRUE)?{
echo?"New?record?created?successfully";
}?else?{
echo?"Error:?"?.?$sql?.?"br"?.?$conn-error;
}
$conn-close();
?
?php
//拷貝為文件write2db_pdo.php,數(shù)據(jù)庫用PDO調(diào)用方法
//print_r($_POST);
a?variable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$department=$_POST['department'];
$email=$_POST['email'];
//調(diào)試用
echo?"Your?input:?";
echo?$first_name;
echo?'br?/';
echo?$last_name;
echo?'br?/';
echo?$department;
echo?'br?/';
echo?$email;
echo?'br?/';
$servername?=?"localhost";
//Your?database?username?and?password
//$username?=?"username";
//$password?=?"password";
$username?=?"tester";
$password?=?"testerPassword";
//your?database?name
$dbname?=?"test";
$tablename?="student";
//?Create?connection
try?{
$conn?=?new?PDO("mysql:host=$servername;dbname=$dbname",?$username,?$password);
//?set?the?PDO?error?mode?to?exception
$conn-setAttribute(PDO::ATTR_ERRMODE,?PDO::ERRMODE_EXCEPTION);
$sql="INSERT?INTO?$tablename?(first_name,last_name,department,email)
VALUES('$first_name','$last_name','$department','$email')";
//?use?exec()?
$conn-exec($sql);
echo?"New?record?created?successfully";
}
catch(PDOException?$e)
{
echo?$sql?.?"br"?.?$e-getMessage();
}
$conn?=?null;
?
--創(chuàng)建數(shù)據(jù)庫test,?將此文件存為test.sql?導(dǎo)入數(shù)據(jù)庫,或者手動創(chuàng)建表結(jié)構(gòu)
--?phpMyAdmin?SQL?Dump
--?version?4.7.4
--?
--
--?Host:?127.0.0.1:3306
--?Generation?Time:?Mar?12,?2018?at?04:04?AM
--?Server?version:?5.7.19
--?PHP?Version:?7.1.9
SET?SQL_MODE?=?"NO_AUTO_VALUE_ON_ZERO";
SET?AUTOCOMMIT?=?0;
START?TRANSACTION;
SET?time_zone?=?"+00:00";
/*!40101?SET?@OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT?*/;
/*!40101?SET?@OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS?*/;
/*!40101?SET?@OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION?*/;
/*!40101?SET?NAMES?utf8mb4?*/;
--
--?Database:?`test`
--
--?--------------------------------------------------------
--
--?Table?structure?for?table?`student`
--
DROP?TABLE?IF?EXISTS?`student`;
CREATE?TABLE?IF?NOT?EXISTS?`student`?(
`id`?tinyint(3)?UNSIGNED?NOT?NULL?AUTO_INCREMENT,
`first_name`?varchar(20)?NOT?NULL,
`last_name`?varchar(20)?NOT?NULL,
`department`?varchar(50)?NOT?NULL,
`email`?varchar(50)?NOT?NULL,
PRIMARY?KEY?(`id`)
)?ENGINE=MyISAM?AUTO_INCREMENT=2?DEFAULT?CHARSET=utf8;
--
--?Dumping?data?for?table?`student`
--
INSERT?INTO?`student`?(`id`,?`first_name`,?`last_name`,?`department`,?`email`)?VALUES
(1,?'first1',?'last1',?'cs',?'1985@qq.com');
COMMIT;
/*!40101?SET?CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT?*/;
/*!40101?SET?CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS?*/;
/*!40101?SET?COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION?*/;
其實(shí)sql文件,就是一些sql語句
填寫好數(shù)據(jù)庫相關(guān)操作后,點(diǎn)擊下一步,首先將數(shù)據(jù)庫連接起來
12mysql_connect(..............)//等等這些數(shù)據(jù)庫連接代碼
數(shù)據(jù)庫連接后,開始讀取sql文件
1234567$Sqls = file_get_contents( '你的sql文件' );//然后把讀取到的sql文件內(nèi)容打散成數(shù)組,當(dāng)然,這個文件要有規(guī)律,就是每條sql語句有一個特定的分隔符,比如分號;$SqlArr = explode(';', $Sqls );//最后就是循環(huán)遍歷出這些sql語句并執(zhí)行,即可foreach ( $SqlArr as $sql ) { mysql_query( $Sql );}
上面只是一個大致思路原理,
具體的話,還是要根據(jù)具體情況來弄的!
特別是那個sql文件中的內(nèi)容,一定要有一定的規(guī)律,并且一些不必要的東西不能有,
比如注釋(很多人從phpmyadmin導(dǎo)出的sql文件,都會帶上注釋,
而注釋是不符合sql語句規(guī)范的,會執(zhí)行出錯,
所以導(dǎo)出后,自己根據(jù)情況修改一下!)