新聞中心
php表單寫入mysql數(shù)據(jù)庫(kù)的代碼
!--表單文件,拷入index.php--
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名與空間、雅安服務(wù)器托管、營(yíng)銷軟件、網(wǎng)站建設(shè)、吉利網(wǎng)站維護(hù)、網(wǎng)站推廣。
!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?*/;
求PHP數(shù)據(jù)庫(kù)封裝類操作代碼
?php
class MySQL{
private $host; //服務(wù)器地址
private $name; //登錄賬號(hào)
private $pwd; //登錄密碼
private $dBase; //數(shù)據(jù)庫(kù)名稱
private $conn; //數(shù)據(jù)庫(kù)鏈接資源
private $result; //結(jié)果集
private $msg; //返回結(jié)果
private $fields; //返回字段
private $fieldsNum; //返回字段數(shù)
private $rowsNum; //返回結(jié)果數(shù)
private $rowsRst; //返回單條記錄的字段數(shù)組
private $filesArray = array(); //返回字段數(shù)組
private $rowsArray = array(); //返回結(jié)果數(shù)組
private $charset='utf8'; //設(shè)置操作的字符集
private $query_count=0; //查詢結(jié)果次數(shù)
static private $_instance; //存儲(chǔ)對(duì)象
//初始化類
private function __construct($host='',$name='',$pwd='',$dBase=''){
if($host != '') $this-host = $host;
if($name != '') $this-name = $name;
if($pwd != '') $this-pwd = $pwd;
if($dBase != '') $this-dBase = $dBase;
$this-init_conn();
}
//防止被克隆
private function __clone(){}
public static function getInstance($host='',$name='',$pwd='',$dBase=''){
if(FALSE == (self::$_instance instanceof self)){
self::$_instance = new self($host,$name,$pwd,$dBase);
}
return self::$_instance;
}
public function __set($name,$value){
$this-$name=$value;
}
public function __get($name){
return $this-$name;
}
//鏈接數(shù)據(jù)庫(kù)
function init_conn(){
$this-conn=@mysql_connect($this-host,$this-name,$this-pwd) or die('connect db fail !');
@mysql_select_db($this-dBase,$this-conn) or die('select db fail !');
mysql_query("set names ".$this-charset);
}
//查詢結(jié)果
function mysql_query_rst($sql){
if($this-conn == '') $this-init_conn();
$this-result = @mysql_query($sql,$this-conn);
$this-query_count++;
}
//取得字段數(shù)
function getFieldsNum($sql){
$this-mysql_query_rst($sql);
$this-fieldsNum = @mysql_num_fields($this-result);
}
//取得查詢結(jié)果數(shù)
function getRowsNum($sql){
$this-mysql_query_rst($sql);
if(mysql_errno() == 0){
return @mysql_num_rows($this-result);
}else{
return '';
}
}
//取得記錄數(shù)組(單條記錄)
function getRowsRst($sql,$type=MYSQL_BOTH){
$this-mysql_query_rst($sql);
if(empty($this-result)) return '';
if(mysql_error() == 0){
$this-rowsRst = mysql_fetch_array($this-result,$type);
return $this-rowsRst;
}else{
return '';
}
}
//取得記錄數(shù)組(多條記錄)
function getRowsArray($sql,$type=MYSQL_BOTH){
!empty($this-rowsArray) ? $this-rowsArray=array() : '';
$this-mysql_query_rst($sql);
if(mysql_errno() == 0){
while($row = mysql_fetch_array($this-result,$type)) {
$this-rowsArray[] = $row;
}
return $this-rowsArray;
}else{
return '';
}
}
//更新、刪除、添加記錄數(shù)
function uidRst($sql){
if($this-conn == ''){
$this-init_conn();
}
@mysql_query($sql);
$this-rowsNum = @mysql_affected_rows();
if(mysql_errno() == 0){
return $this-rowsNum;
}else{
return '';
}
}
//返回最近插入的一條數(shù)據(jù)庫(kù)的id值
function returnRstId($sql){
if($this-conn == ''){
$this-init_conn();
}
@mysql_query($sql);
if(mysql_errno() == 0){
return mysql_insert_id();
}else{
return '';
}
}
//獲取對(duì)應(yīng)的字段值
function getFields($sql,$fields){
$this-mysql_query_rst($sql);
if(mysql_errno() == 0){
if(mysql_num_rows($this-result) 0){
$tmpfld = @mysql_fetch_row($this-result);
$this-fields = $tmpfld[$fields];
}
return $this-fields;
}else{
return '';
}
}
//錯(cuò)誤信息
function msg_error(){
if(mysql_errno() != 0) {
$this-msg = mysql_error();
}
return $this-msg;
}
//釋放結(jié)果集
function close_rst(){
mysql_free_result($this-result);
$this-msg = '';
$this-fieldsNum = 0;
$this-rowsNum = 0;
$this-filesArray = '';
$this-rowsArray = '';
}
//關(guān)閉數(shù)據(jù)庫(kù)
function close_conn(){
$this-close_rst();
mysql_close($this-conn);
$this-conn = '';
}
//取得數(shù)據(jù)庫(kù)版本
function db_version() {
return mysql_get_server_info();
}
}
求PHP從數(shù)據(jù)庫(kù)中讀取內(nèi)容并存入文件和從文件讀取數(shù)據(jù)插入數(shù)據(jù)庫(kù)的代碼
//第一種?
?php
$conn?=?mysql_connect("主機(jī)名","用戶名","密碼");
mysql_select_db("數(shù)據(jù)庫(kù)名",$conn);
$sql?=?"select?*?from?user";
$result?=?mysql_query($sql);
$data="";
while($row?=?mysql_fetch_row($result)){
foreach($row?as?$v){
$data?.=$v."|";??????//加個(gè)樹(shù)線隔開(kāi)字段
}
nl2br($data);????//讀完一行回車
}
file_put_contents("abc.txt",$data);
//第二種
?php
$filename?=?"abc.txt";
$res?=?fopen($filename,"r");
while(!feof($res)){
$buffer?=?fgets($res);
$buffer?=?str_replace('?',?'|',?$buffer);?
$array?=?explode('|',?$buffer);?
foreach($array?as?$v){
if(strlen($v)0){
$arr[]?=?$v;
}
}
}
fclose($res);
//組裝數(shù)組插入數(shù)據(jù)庫(kù)就可以了
php保存數(shù)據(jù)到數(shù)據(jù)庫(kù)
需要轉(zhuǎn)義一下。有幾個(gè)辦法:
1,最簡(jiǎn)單,把一些危險(xiǎn)字符過(guò)濾掉,比如英文的單引號(hào),雙引號(hào)等;
2,使用addslashes在php中進(jìn)行轉(zhuǎn)義,自己處理;
3,可以使用一些現(xiàn)成的處理方法,比如uchome自帶的getstr。這樣的好處就是對(duì)各種可能出現(xiàn)的情況處理比較好。
php代碼存在數(shù)據(jù)庫(kù)中使用
可以。
樓主可以采用這種思路:
取出數(shù)據(jù)庫(kù)--將代碼寫成php文件--保存到硬盤--利用一個(gè)模擬瀏覽器訪問(wèn)該php--結(jié)果該php被執(zhí)行
或者
取出數(shù)據(jù)庫(kù)--將代碼寫成php文件--保存到硬盤--include該php--結(jié)果該php被執(zhí)行--刪除硬盤上的php
網(wǎng)站題目:php存儲(chǔ)數(shù)據(jù)庫(kù)的代碼,php存儲(chǔ)數(shù)據(jù)庫(kù)的代碼是多少
網(wǎng)頁(yè)URL:http://www.ef60e0e.cn/article/hcdhhp.html