扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
?php
路桥网站制作公司哪家好,找创新互联建站!从网页设计、网站建设、微信开发、APP开发、自适应网站建设等网站项目制作,到程序开发,运营维护。创新互联建站成立与2013年到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联建站。
$username = xxx; //数据库用户名
$password = xxx; //数据库密码
$con = mysql_connect("localhost",$username,$username);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_query("INSERT INTO person (id, name, sex, phone, room_id, seat, date1, opinion)
VALUES ('$_POST[id]', '$_POST[name]', '$_POST[sex]', '$_POST[phone]', '$_POST[room_id]', '$_POST[seat]', ''$_POST[date1]', '$_POST[option]')");
mysql_close($con);
?
!--表单文件,拷入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 */;
建议楼主把SQL语句显示出来,自然就明白问题在哪里了,我试着简单说一下,假设你的USER数据表有三个字段(name、age、sex),_POST提交数据也是这三个字段的,假设POST的值分别是abc、18、男,那么帖子的PHP会执行下面的三个SQL语句:
INSERT INTO USER(name) VALUES(abc)
INSERT INTO USER(age) VALUES(18)
INSERT INTO USER(sex) VALUES(男)
现在明白了吧,一、三两句会语法错误,英文没有在文本字段添加引号,第二句即使执行成功插入的记录只有年龄,姓名、性别为空,如果数据库有限制字段有效性,那么第二句插入也会失败,数据库需要的语句是:
INSERT INTO USER(name,age,sex) VALUES('abc',18,'男')
如果理解了,自然会下面这样写PHP语句:
$sql=END
INSERT INTO USER(name,age,sex)
VALUES('{$_POST['name']}',{$_POST['age']},'{$_POST['sex']}')
END;
网页:
body
form action="Connection.php"method="post"
table bgcolor="#eeeeee"
tr
td colspan="2" align="center"b情况登记/b
tr
td姓名:tdinput size="20" type="text" name="name"
tr
td密码:tdinput size="20" type="password" name="password
tr
td性别:tdinput type="radio" name="stuSex" value="0"男input type="radio" name="stuSex" value="1"女
tr
tdtdinput type="submit" value="提交"/ainput type="reset"value="重写"
/table
/form
/body
数据库表:
CREATE DATABASE `student`;
CREATE TABLE `student` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`stusex` int(1) NOT NULL,
PRIMARY KEY (`id`)
);
PHP Connection.php代码:
?php
$db = mysql_connect('localhost', 'root', '123456'); //root是数据库账号 123456 是数据库密码
if (!$db) {
die('无法连接: ' . mysql_error());
}
mysql_select_db('student', $db) or die ('无法使用数据库: ' . mysql_error());
$name = trim(strip_tags($_POST['name']));
$password = md5(trim($_POST['password']));
$stusex = intval($_POST['stusex']);
$sql = "insert into student (`name`,`password`,`stusex`) values ('$name','$password','$stusex')";
mysql_query($sql) or die("无法保存学生数据: " . mysql_error());
die("保存成功");
?
举例如下:
file1.php:
file2.php:
?php
echo
$_post[gender];
//显示前一页单选按钮传过来的值
//以下是写入数据库操作
$db=mysql_connect("hostname","username","password");
//连接数据库服务器
mysql_select_db("dbname",$db);
//选择具体的数据库
mysql_query("set
names
gbk");
//统一字符集到
gbk
$myinsert="insert
into
表名
(gender)
values
('$_post[gender]')";
//构造插入语句
$myresult=mysql_query($myinsert,$db);
//执行插入操作
if($myresult)
{
echo
"插入成功!";
}
else
{
echo
"插入失败"."
".mysql_error();
}
?
把下面的代码保存为post.php
?
$conn = mysql_connect("localhost","11111","22222");
$action = $_POST['action'];
if($action == 'send'){
$username = $_POST['username'];
$password = $_POST['password'];
mysql_select_db("333333",$conn);
$sql = "INSERT INTO player (username,password) VALUES ('$username','$password')";
$result = mysql_query($sql,$conn);
}
?
html
body
form method="post" action="post.php"
input type="text" name="username"
input type="text" name="password"
input type="hidden" name="action" value="send"
input type="submit" name="Submit" value="提交"
/form
/body
/html
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流