首先你要建立一个表,例如是注册的用户表user
创新互联公司专注于隆子企业网站建设,响应式网站开发,成都做商城网站。隆子网站建设公司,为隆子等地区提供建站服务。全流程按需开发,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务
,里面的结构有字段
id,
name,nickname,email等。
然后在你的表单处form
action="a.php"
method="post"
name="regform"(如果有图片上传,还要加上enctype="multipart/form-data")
,那么点击表单提交按纽后,此表单将会交给处理页a.php来作处理。
如果简单点,你就直接可以将表单传递过来的数据$_POST,直接用sql插入语句,insert
into来插入到数据库,表user中。例如insert
into
user
set
name='".$_POST['name']."'.............................
一、php配置MySQL
1、将php安装目录下的php_mysql.dll和MySQL安装目录下的libmysql.dll文件拷贝至c:/windows/system32中;
2、配置php.ini
extension=php_gd2.dll
extension=php_mbstring.dll
extension=php_mysql.dll
extension=php_mysqli.dll
把上面四个。dll的最前面的;去掉
二、php表单提交至数据库的实现过程
1、login.php页面
SPAN style="FONT-SIZE: 14px"html
FORM method=post action=add.php
Name: INPUT name=usernameBR
Email: INPUT name=emailBR
INPUT value=提交 type=submit name=submit
/FORM
/SPAN
2、add.php页面
SPAN style="FONT-SIZE: 14px"?php
include("conn.php");
?
?php
if(isset($_POST["submit"]))
{
$sql = "insert into users(username, email) values('$_POST[username]', '$_POST[email]')";
mysqli_query($conn, $sql);
echo "添加成功";
}
?/SPAN
3、conn.php页面
SPAN style="FONT-SIZE: 14px"?php
$conn = new mysqli("localhost", "root", "159357");
$conn-select_db("db_test");
//mysql_query("set name 'gb2312'");
$conn-set_charset("utf8");
?/SPAN
这个技术稍微综合了PHP的基础知识,
给你一个思路,
(1) 先将textarea 文本中的信息 传入 php的 $_POST['content'], content 是textarea的属性名称,
(2) 传过来的值是通过数组的形式进行保存的 ,其中PHP有一个函数是可以将数组转换成字符串形式,
引用那个函数后,通过var_dump()打印出你的转换数据,看是否是字符串
在这里需要提醒你一下,因为你是每一行作为一句话 通过逗号分隔出来的 ,那么 在转换成数组的时候,
将每一行数据 用 | 隔开,例如:
数据1 , 数据11, 数据111 | 数据2, 数据22, 数据222| 数据3 , 数据33, 数据333 | 数据4, 数据44, 数据444 |
这就是一个转换成字符串的格式了,
(3) 通过转换成字符串后,php中还有一个函数就是将字符串转换成 数组的函数,转换结果应该出来的数据格式是:
array=
array(0)=array{
'数据1,数据11,数据111'
},
array(1)=array{
'数据2,数据22,数据222'
}....
(4)以上的数据都是索引数组的二维数组,将二维数组用foreach()去循环打印出来,那么久可以得到每一个 所以数组下的 数据了,这些数据
就是你要保存到数据的数据,在按照(1)和(2)的方式进行操作,最后就可以把textarea的数据保存到数据库中咯。
思路就是这样的 ,希望你能自己动手,把这个程序解决,这个程序在实际开发中运用的很广泛,最好自己把它掌握了.....
!--表单文件,拷入index.php--
!DOCTYPE html
html
head
style
label{display:inline-block;width:100px;margin-bottom:10px;}
/style
titleAdd students/title
/head
body
!-- 数据库用mysqli 面向过程调用方法--
form method="post" action="write2db.php"
!--数据库用mysqli 面向过程调用方法
form method="post" action="write2db_sqlio.php"
--
!--数据库用PDO调用方法
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,数据库用mysqli 面向过程调用方法
//print_r($_POST);
// create a variable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$department=$_POST['department'];
$email=$_POST['email'];
//调试用
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,数据库用mysqli 面向对象调用方法
//print_r($_POST);
// create a variable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$department=$_POST['department'];
$email=$_POST['email'];
//调试用
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,数据库用PDO调用方法
//print_r($_POST);
a variable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$department=$_POST['department'];
$email=$_POST['email'];
//调试用
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;
?
--创建数据库test, 将此文件存为test.sql 导入数据库,或者手动创建表结构
-- 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 */;
看你的表格,只需要从表单中循环取出所有行,然后依次写入数据库就行了
假设表单中是:txt1[] txt2[]......
通过$a=$_POST["txt1[]"]取得值,然后循环写入:
for($t=0;$t=count($a)-1;$t++){
// insert into 表 (字段1,......) values ('$a[$t]',.....)
}
至于打印,在JS中直接windows.print就行了