把來自表單的數(shù)據(jù)插入數(shù)據(jù)庫

創(chuàng)新互聯(lián)建站從2013年開始,先為金州等服務建站,金州等地企業(yè),進行企業(yè)商務咨詢服務。為金州企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務解決您的所有建站問題。
現(xiàn)在,我們創(chuàng)建一個 HTML 表單,這個表單可把新記錄插入 "Persons" 表。
這是這個 HTML 表單:
html
body
form?action="insert.php"?method="post"
Firstname:?input?type="text"?name="firstname"?/
Lastname:?input?type="text"?name="lastname"?/
Age:?input?type="text"?name="age"?/
input?type="submit"?/
/form
/body
/html
當用戶點擊上例中 HTML 表單中的提交按鈕時,表單數(shù)據(jù)被發(fā)送到 "insert.php"。"insert.php" 文件連接數(shù)據(jù)庫,并通過 $_POST 變量從表單取回值。然后,mysql_query() 函數(shù)執(zhí)行 INSERT INTO 語句,一條新的記錄會添加到數(shù)據(jù)庫表中。
下面是 "insert.php" 頁面的代碼:
?php
$con?=?mysql_connect("localhost","peter","abc123");
if?(!$con)
{
die('Could?not?connect:?'?.?mysql_error());
}
mysql_select_db("my_db",?$con);
$sql="INSERT?INTO?Persons?(FirstName,?LastName,?Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
if?(!mysql_query($sql,$con))
{
die('Error:?'?.?mysql_error());
}
echo?"1?record?added";
mysql_close($con)
?
把下面的代碼保存為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
因為你少寫了一個大括號的結尾。
修改后:
table?id="DataList1"?cellspacing="0"?border="0"?width="100%"
?php
$db?=?mysqli_connect('localhost',?'root','root');//連接MySQL服務器
mysqli_select_db($db,'ticket');//選擇數(shù)據(jù)庫文件
if(mysqli_connect_errno()){
echo?"Error:Could?not?connect?to?mysql?ticket"; exit;
}
$q="select?*?from?news";//設置查詢指令
$result=mysql_query($db,$q);//執(zhí)行查詢
while($row=mysql_fetch_assoc($result))?//將result結果集中查詢結果取出一條
{
echo"trtd".$row["news"]."/tdtd".$row["date"]."/td/tr";?exit;
}
}?//?就是這樣?
?
/table
謝謝謝謝謝謝碼密。的入輸要需所面。界理管臺后器。由路陸登是。碼密理管。廖仲庭。器由。路。
php代碼可以寫在html代碼中的任意部分 只要帶上? ? 界定標簽就好了和平時寫php沒有任何區(qū)別 但是需要注意的一點是 這個文件的后綴名 要是 .php 的 所以更準確的說法應該是html寫在了PHP文件中 這并不影響我們同時使用這兩種語言 隨便寫了一點 給你參考下 ?php //連接數(shù)據(jù)庫 $link = mysql_connect("localhost", "username", "password") or die("Could not connect: " . mysql_error()); print ("Connected successfully"); //選擇數(shù)據(jù)庫 mysql_select_db('dbname', $link) or die ('Can\'t use db : ' . mysql_error()); //進行你想要的操作 $a =123; ? html xmlns="" head meta http-equiv="Content-Type" content="text/html; charset=utf-8" / title發(fā)現(xiàn)I Do一刻/title link href="css/public.css" rel="stylesheet" type="text/css" script src="Scripts/AC_RunActiveContent.js" type="text/javascript"/script /head body ?php echo $a; ? /body /html 用echo 直接輸出到 頁面上就好了 你自己看我下面寫的html代碼中 就有這樣的例子 不管你想發(fā)到哪里都可以用這種方法
就是$insert這條SQL語句的問題
echo $insert;看看語句有沒有問題
檢查方法將輸出的SQL語句放在數(shù)據(jù)庫里執(zhí)行以下
標準寫法
$insert = "insert into my_tab(user,psd) values('".$user."','".$psd."')":
數(shù)據(jù)庫有很多種類:mysql,oracle,mssql,db2等等。PHP操作數(shù)據(jù)庫的時候,要保證該類型數(shù)據(jù)庫的擴展已開啟。這里連接的數(shù)據(jù)庫以mysql為例:?php
//數(shù)據(jù)庫服務器地址
$host="localhost";
//連接數(shù)據(jù)庫用戶名
$uname="root";
//連接數(shù)據(jù)庫密碼
$upass="";
//連接數(shù)據(jù)庫
$conn=mysql_connect($host, $uname,$upass);
//判斷連接
if(!$conn){
die("連接數(shù)據(jù)庫失敗!").mysql_errno();
}
//連接成功,其他操作省略
?
本文名稱:網(wǎng)頁數(shù)據(jù)庫php代碼,php寫入數(shù)據(jù)庫代碼
網(wǎng)站網(wǎng)址:http://chinadenli.net/article9/dsiicoh.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供自適應網(wǎng)站、外貿(mào)建站、搜索引擎優(yōu)化、ChatGPT、網(wǎng)站內(nèi)鏈、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)