本文實(shí)例講述了PHP實(shí)現(xiàn)PDO操作mysql存儲(chǔ)過(guò)程。分享給大家供大家參考,具體如下:
一 代碼
sql語(yǔ)句:
create procedure pro_reg (in nc varchar(80), in pwd varchar(80), in email varchar(80),in address varchar(50))
begin
insert into tb_reg (name, pwd ,email ,address) values (nc, pwd, email, address);
end;
index.php:
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
html xmlns="http://www.w3.org/1999/xhtml">
head>
meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
title>用戶注冊(cè)/title>
link rel="stylesheet" type="text/css" href="style.css" rel="external nofollow" >
/head>
script language="javascript">
function chkinput(form){
if(form.nc.value==""){
alert("請(qǐng)輸入用戶昵稱!");
form.nc.select();
return(false);
}
if(form.pwd.value==""){
alert("請(qǐng)輸入注冊(cè)密碼!");
form.pwd.select();
return(false);
}
if(form.email.value==""){
alert("請(qǐng)輸入E-mail地址!");
form.email.select();
return(false);
}
if(form.address.value==""){
alert("請(qǐng)輸入家庭地址!");
form.address.select();
return(false);
}
return(true);
}
/script>
body>
table width="200" border="0" align="center" cellpadding="0" cellspacing="0">
tr>
td>img src="images/banner.gif" width="500" height="65" />/td>
/tr>
/table>
table width="500" height="10" border="0" align="center" cellpadding="0" cellspacing="0">
tr>
td>/td>
/tr>
/table>
table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
tr>
td bgcolor="#1170FF">table width="500" height="157" border="0" align="center" cellpadding="0" cellspacing="1">
form name="form1" method="post" action="index.php" onsubmit="return chkinput(this)">
tr>
td height="25" colspan="2" bgcolor="#B5D3FF">div align="center">用戶注冊(cè)/div>/td>
/tr>
tr>
td width="150" height="25" bgcolor="#FFFFFF">div align="center">用戶昵稱:/div>/td>
td width="347" bgcolor="#FFFFFF">nbsp;input type="text" name="nc" class="inputcss" size="25">/td>
/tr>
tr>
td height="25" bgcolor="#FFFFFF">div align="center">注冊(cè)密碼:/div>/td>
td height="25" bgcolor="#FFFFFF">nbsp;input type="password" name="pwd" class="inputcss" size="25">/td>
/tr>
tr>
td height="25" bgcolor="#FFFFFF">div align="center">E-mail:/div>/td>
td height="25" bgcolor="#FFFFFF">nbsp;input type="text" name="email" class="inputcss" size="25">/td>
/tr>
tr>
td height="25" bgcolor="#FFFFFF">div align="center">家庭住址:/div>/td>
td height="25" bgcolor="#FFFFFF">nbsp;input type="text" name="address" class="inputcss" size="25">/td>
/tr>
tr>
td height="25" colspan="2" bgcolor="#FFFFFF">div align="center">input type="submit" name="submit" value="注冊(cè)" class="buttoncss">nbsp;nbsp;input type="reset" value="重寫" class="buttoncss">/div>/td>
/tr>
/form>
/table>/td>
/tr>
/table>
table width="600" height="80" border="0" align="center" cellpadding="0" cellspacing="0">
tr>
td>div align="center">br />
版權(quán)所有nbsp;吉林省**科技有限公司! 未經(jīng)授權(quán)禁止復(fù)制或建立鏡像!br />
Copyright copy;nbsp;, All Rights Reserved! br />
br />
建議您在大于1024*768的分辨率下使用 /div>/td>
/tr>
/table>
?php
if($_POST['submit']!=""){
$dbms='mysql'; //數(shù)據(jù)庫(kù)類型 ,對(duì)于開(kāi)發(fā)者來(lái)說(shuō),使用不同的數(shù)據(jù)庫(kù),只要改這個(gè),不用記住那么多的函數(shù)
$host='localhost'; //數(shù)據(jù)庫(kù)主機(jī)名
$dbName='db_database15'; //使用的數(shù)據(jù)庫(kù)
$user='root'; //數(shù)據(jù)庫(kù)連接用戶名
$pass='root'; //對(duì)應(yīng)的密碼
$dsn="$dbms:host=$host;dbname=$dbName";
try {
$pdo = new PDO($dsn, $user, $pass); //初始化一個(gè)PDO對(duì)象,就是創(chuàng)建了數(shù)據(jù)庫(kù)連接對(duì)象$pdo
$pdo->query("set names utf8"); //設(shè)置數(shù)據(jù)庫(kù)編碼格式
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$nc=$_POST['nc'];
$pwd=md5($_POST['pwd']);
$email=$_POST['email'];
$address=$_POST['address'];
$query="call pro_reg('$nc','$pwd','$email','$address')";
$result=$pdo->prepare($query);
if($result->execute()){
echo "數(shù)據(jù)添加成功!";
}else{
echo "數(shù)據(jù)添加失敗!";
}
} catch (PDOException $e) {
echo 'PDO Exception Caught.';
echo 'Error with the database:br/>';
echo 'SQL Query: '.$query;
echo 'pre>';
echo "Error: " . $e->getMessage(). "br/>";
echo "Code: " . $e->getCode(). "br/>";
echo "File: " . $e->getFile(). "br/>";
echo "Line: " . $e->getLine(). "br/>";
echo "Trace: " . $e->getTraceAsString(). "br/>";
echo '/pre>';
}
}
?>
/body>
/html>
二 運(yùn)行結(jié)果
數(shù)據(jù)添加成功!
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP基于pdo操作數(shù)據(jù)庫(kù)技巧總結(jié)》、《php+Oracle數(shù)據(jù)庫(kù)程序設(shè)計(jì)技巧總結(jié)》、《PHP+MongoDB數(shù)據(jù)庫(kù)操作技巧大全》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- PHP使用PDO實(shí)現(xiàn)mysql防注入功能詳解
- PHP連接MySQL數(shù)據(jù)庫(kù)的三種方式實(shí)例分析【mysql、mysqli、pdo】
- PHP使用PDO、mysqli擴(kuò)展實(shí)現(xiàn)與數(shù)據(jù)庫(kù)交互操作詳解
- PHP使用PDO創(chuàng)建MySQL數(shù)據(jù)庫(kù)、表及插入多條數(shù)據(jù)操作示例
- php使用mysqli和pdo擴(kuò)展,測(cè)試對(duì)比mysql數(shù)據(jù)庫(kù)的執(zhí)行效率完整示例
- php使用mysqli和pdo擴(kuò)展,測(cè)試對(duì)比連接mysql數(shù)據(jù)庫(kù)的效率完整示例
- PHP使用PDO操作sqlite數(shù)據(jù)庫(kù)應(yīng)用案例
- PHP基于PDO擴(kuò)展操作mysql數(shù)據(jù)庫(kù)示例
- PHP基于pdo的數(shù)據(jù)庫(kù)操作類【可支持mysql、sqlserver及oracle】
- PHP如何初始化PDO及原始SQL語(yǔ)句操作