php将文本写入数据库中 php把数据写入文本

php怎么读取txt文本内容存入mysql数据库第一步,读取txt的文件 。假设为a.txt
$content = file_get_content('a.txt'); //读取文件内容存入变量 。
第二步,存入数据库
mysql_query("insert 表名 (字段名) values('".$content."'));
Ps:文件是上传的,上传后的临时文件名是:$_FILE['tmp_name']
怎么用php把html表单内容写入数据库1php将文本写入数据库中:首先要使用PHP的超全局变量 $_GET 和 $_POST 用于收集表单数据(form-data)
2php将文本写入数据库中:然后使用INSERT INTO 语句用于向数据库表中插入新记录 。
具体示例:
(1)首先创建了一个名为 "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)其次创建一个 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)接着当用户点击上例中 HTML 表单中的提交按钮时,表单数据被发送到 "insert.php" 。"insert.php" 文件连接数据库,并通过
$_POST 变量从表单取回值 。然后,mysql_query() 函数执行 INSERT INTO 语句,一条新的记录会添加到数据库表中 。
?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)
?
php如何读取txt文本内容存入mysql数据库$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
$txt= fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
fopen读取txt文件的内容 , 返回字符串,把它存入一个变量,再把变量值存入数据库 。可以看看这个函数
php代码中怎样将文本文件内容直接导入到数据库中将每一行读在一个字符串里,然后用这个字符串组成SQL语句,存入数据库
在php中把文本框的多条数据插入到数据库思路:
1、构建form表单,输出文本框,用textarea/textarea吧,input/内不能换行,页面效果也不好(php、html代码嵌套写的话,直接写就行,建议用smarty,php与模板分离 , 比较清晰)
2、提交内容 , 确定用什么method(post、get)
3、获取内容,$str=$_POST['name'](name为textarea的name值)
4、$arr=split ('\r\n', $str);按换行符分割字符串为数组
5、循环执行插入语句,$arr每一层都是一条数据
如何利用php读取txt文件再将数据插入到数据库serial_number.txt的示例内容:
serial_number.txt:
DM00001A11 0116,
SN00002A11 0116,
AB00003A11 0116,
PV00004A11 0116,
OC00005A11 0116,
IX00006A11 0116,
创建数据表:
create table serial_number(
id int primary key auto_increment not null,
serial_number varchar(50) not null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
php代码如下:

推荐阅读