一 代码
class="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>PDO中exec()方法的使用</title> <style type="text/css"> <!-- body,td,th { font-size: 12px; } --> </style></head> <body> <table id="__01" width="464" height="336" border="0" cellpadding="0" cellspacing="0"> <tr> <td colspan="3"> <img src="images/mysql_01.gif" width="464" height="139" alt=""></td> </tr> <tr> <td> <img src="images/mysql_02.gif" width="78" height="136" alt=""></td> <td width="312" height="136" valign="top"> <form id="form1" name="form1" method="post" action="index.php"> <table width="310" border="0" cellpadding="0" cellspacing="0"> <tr> <td height="30" align="right">类型:</td> <td align="left"><input name="pdo" type="text" id="pdo" size="20" /></td> </tr> <tr> <td height="25" align="right">数据库:</td> <td align="left"><input name="databases" type="text" id="databases" size="20" /></td> </tr> <tr> <td height="25" align="right">时间:</td> <td align="left"><input name="dates" type="text" id="dates" size="20" /></td> </tr> <tr> <td height="30" colspan="2" align="center"><input type="submit" name="Submit" value="提交" /> <input type="reset" name="Submit2" value="重置" /></td> </tr> <?php if($_POST['Submit']=="提交" && $_POST['pdo']!=""){ $dbms='mysql'; //数据库类型 ,对于开发者来说,使用不同的数据库,只要改这个,不用记住那么多的函数 $host='localhost'; //数据库主机名 $dbName='db_database15'; //使用的数据库 $user='root'; //数据库连接用户名 $pass='root'; //对应的密码 $dsn="$dbms:host=$host;dbname=$dbName"; try { $pdo = new PDO($dsn, $user, $pass); //初始化一个PDO对象,就是创建了数据库连接对象$pdo $query="insert into tb_pdo_mysql(pdo_type,database_name,dates)values('".$_POST['pdo']."','".$_POST['databases']."','".$_POST['dates']."')"; $result=$pdo->exec($query); echo "数据添加成功,受影响的行数为:" . $result; } catch (PDOException $e) { die ("Error!: " . $e->getMessage() . "<br/>"); } } ?> </table> </form> </td> <td> <img src="images/mysql_04.jpg" width="74" height="136" alt=""></td> </tr> <tr> <td colspan="3"> <img src="images/mysql_05.gif" width="464" height="61" alt=""></td> </tr> </table> </body> </html>
?
二 运行结果?