!--表單文件,拷入index.php--
創(chuàng)新互聯(lián)建站是一家專業(yè)提供博羅企業(yè)網站建設,專注與做網站、成都網站設計、H5場景定制、小程序制作等業(yè)務。10年已為博羅眾多企業(yè)、政府機構等服務。創(chuàng)新互聯(lián)專業(yè)網絡公司優(yōu)惠進行中。
!DOCTYPE?html
html
head
style
label{display:inline-block;width:100px;margin-bottom:10px;}
/style
titleAdd?students/title
/head
body
!--?數(shù)據(jù)庫用mysqli?面向過程調用方法--
form?method="post"?action="write2db.php"
!--數(shù)據(jù)庫用mysqli?面向過程調用方法
form?method="post"?action="write2db_sqlio.php"
--
!--數(shù)據(jù)庫用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,數(shù)據(jù)庫用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,數(shù)據(jù)庫用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,數(shù)據(jù)庫用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;
?
--創(chuàng)建數(shù)據(jù)庫test,?將此文件存為test.sql?導入數(shù)據(jù)庫,或者手動創(chuàng)建表結構
--?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點抗 ');
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?*/;
p代碼中獲取表單中單選按鈕的值:(單選按鈕只能讓我們選擇一個,這里有一個“checked”屬性,這是用來默認選取的,我們每次刷新我們的頁面時就默認為這個值。)
例:form name="myform" action="" method="post"
性別:
input type="radio" name="sex" value="男" checked /鋒讓男input name="sex" type="radio" value="女" /女
input type="submit" name="submit" value="提交" /
/銀雹局form
?php
echo "您的選擇是:";
echo $_POST["sex"];
?
如果你選擇的是男,則出來的值就是“男”,要是你選擇的是女肆旁,則出來的值就是“女”。
首先,你得在diaocha.php這個文件,接收表單傳的值州橡
$radiogroup = isset($_POST['radiogroup'])?$radiogroup:'';
isset用來掘或檢測是否有選中提交,然后就是數(shù)據(jù)庫的鏈接
$con = mysql_connect('localhost','root',''); //三個參數(shù),分別是,連接的主機名,mysql的賬號,mysql密碼
mysql_query('set names utf8'); //設置連接的字符集,如果頁面是utf8的編碼,就是utf8,如果是gbk的話,那就寫 set names gbk
mysql_select_db('xxx',$con);nbsp; //xxx就是你要選擇的數(shù)據(jù)庫名稱
插入數(shù)據(jù)
$sql = "insert into xxxx set xxx = $radiogroup" //xxxx 是你要插入的表名,xxx就判跡伍是字段名
mysql_query($sql);