?php
路橋網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)建站!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、自適應(yīng)網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)建站成立與2013年到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)建站。
$username = xxx; //數(shù)據(jù)庫(kù)用戶名
$password = xxx; //數(shù)據(jù)庫(kù)密碼
$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
!--?數(shù)據(jù)庫(kù)用mysqli?面向過(guò)程調(diào)用方法--
form?method="post"?action="write2db.php"
!--數(shù)據(jù)庫(kù)用mysqli?面向過(guò)程調(diào)用方法
form?method="post"?action="write2db_sqlio.php"
--
!--數(shù)據(jù)庫(kù)用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ù)庫(kù)用mysqli?面向過(guò)程調(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ù)庫(kù)用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ù)庫(kù)用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ù)庫(kù)test,?將此文件存為test.sql?導(dǎo)入數(shù)據(jù)庫(kù),或者手動(dòng)創(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?*/;
建議樓主把SQL語(yǔ)句顯示出來(lái),自然就明白問(wèn)題在哪里了,我試著簡(jiǎn)單說(shuō)一下,假設(shè)你的USER數(shù)據(jù)表有三個(gè)字段(name、age、sex),_POST提交數(shù)據(jù)也是這三個(gè)字段的,假設(shè)POST的值分別是abc、18、男,那么帖子的PHP會(huì)執(zhí)行下面的三個(gè)SQL語(yǔ)句:
INSERT INTO USER(name) VALUES(abc)
INSERT INTO USER(age) VALUES(18)
INSERT INTO USER(sex) VALUES(男)
現(xiàn)在明白了吧,一、三兩句會(huì)語(yǔ)法錯(cuò)誤,英文沒(méi)有在文本字段添加引號(hào),第二句即使執(zhí)行成功插入的記錄只有年齡,姓名、性別為空,如果數(shù)據(jù)庫(kù)有限制字段有效性,那么第二句插入也會(huì)失敗,數(shù)據(jù)庫(kù)需要的語(yǔ)句是:
INSERT INTO USER(name,age,sex) VALUES('abc',18,'男')
如果理解了,自然會(huì)下面這樣寫PHP語(yǔ)句:
$sql=END
INSERT INTO USER(name,age,sex)
VALUES('{$_POST['name']}',{$_POST['age']},'{$_POST['sex']}')
END;
網(wǎng)頁(yè):
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
數(shù)據(jù)庫(kù)表:
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是數(shù)據(jù)庫(kù)賬號(hào) 123456 是數(shù)據(jù)庫(kù)密碼
if (!$db) {
die('無(wú)法連接: ' . mysql_error());
}
mysql_select_db('student', $db) or die ('無(wú)法使用數(shù)據(jù)庫(kù): ' . 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("無(wú)法保存學(xué)生數(shù)據(jù): " . mysql_error());
die("保存成功");
?
舉例如下:
file1.php:
file2.php:
?php
echo
$_post[gender];
//顯示前一頁(yè)單選按鈕傳過(guò)來(lái)的值
//以下是寫入數(shù)據(jù)庫(kù)操作
$db=mysql_connect("hostname","username","password");
//連接數(shù)據(jù)庫(kù)服務(wù)器
mysql_select_db("dbname",$db);
//選擇具體的數(shù)據(jù)庫(kù)
mysql_query("set
names
gbk");
//統(tǒng)一字符集到
gbk
$myinsert="insert
into
表名
(gender)
values
('$_post[gender]')";
//構(gòu)造插入語(yǔ)句
$myresult=mysql_query($myinsert,$db);
//執(zhí)行插入操作
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