php寫入數(shù)據(jù)庫源碼 php建立數(shù)據(jù)庫連接的代碼

php表單寫入mysql數(shù)據(jù)庫的代碼

!--表單文件,拷入index.php--

成都創(chuàng)新互聯(lián)公司于2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都做網(wǎng)站、成都網(wǎng)站設(shè)計網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元廬山做網(wǎng)站,已為上家服務(wù),為廬山各地企業(yè)和個人服務(wù),聯(lián)系電話:18980820575

!DOCTYPE?html

html

head

style

label{display:inline-block;width:100px;margin-bottom:10px;}

/style

titleAdd?students/title

/head

body

!--?數(shù)據(jù)庫用mysqli?面向過程調(diào)用方法--

form?method="post"?action="write2db.php"

!--數(shù)據(jù)庫用mysqli?面向過程調(diào)用方法

form?method="post"?action="write2db_sqlio.php"

--

!--數(shù)據(jù)庫用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ù)庫用mysqli?面向過程調(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ù)庫用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ù)庫用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ù)庫test,?將此文件存為test.sql?導(dǎo)入數(shù)據(jù)庫,或者手動創(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?*/;

由網(wǎng)頁提交的表單提交寫入數(shù)據(jù)庫 PHP源代碼該怎么寫

把下面的代碼保存為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

php源碼寫入mysql數(shù)據(jù)庫問題

$sql="insert into sfb_zhanghu(Account,Password) values ($zh,$mm)"; // 這出錯

加單引號

values ( '$zh' , '$mm') // 數(shù)字可以不用放,字母一定要

怎么把php源碼數(shù)據(jù)庫導(dǎo)入數(shù)據(jù)庫

可以參考:

一般是單獨(dú)導(dǎo)入的,

在mysql上,要用mysql_import工具 把文本導(dǎo)入

sqlserver上可以用數(shù)據(jù)庫備份工具恢復(fù)導(dǎo)入, 也可以使用其他數(shù)據(jù)庫引擎通過ado到。

不需要源碼,但是需要了解php源碼所需要的庫表結(jié)構(gòu)。一般php源碼里好多都有建立空庫結(jié)構(gòu)的源碼。

當(dāng)前文章:php寫入數(shù)據(jù)庫源碼 php建立數(shù)據(jù)庫連接的代碼
分享地址:http://muchs.cn/article22/hjssjc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、網(wǎng)站維護(hù)品牌網(wǎng)站建設(shè)、全網(wǎng)營銷推廣軟件開發(fā)、微信公眾號

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司