dyw_tbl
,使用 insert 命令插入三筆紀錄。
[root@kvm8 html]# mysql -s -uroot -p123qwe mysql> use dyw; mysql> insert into dyw_tbl -> (dyw_title, dyw_author, submission_date) -> values -> ("PHP", "david", NOW()); mysql> insert into dyw_tbl -> (dyw_title, dyw_author, submission_date) -> values -> ("MySQL", "dywang", '2014-12-12'); mysql> insert into dyw_tbl -> (dyw_title, dyw_author, submission_date) -> values -> ("Linux", "linda", '2014-12-24'); mysql> exit
dywphp_tbl
。
get_magic_quotes_gpc()
打開時,所有的 ' (單引號), " (雙引號), \ (反斜線)
及空字符會自動轉為含有反斜線的溢出字符。
addslashes($str)
在指定的字串 $str
前加入反斜線。
$_POST
抓取網頁上輸入的資料,不同於 $_GET
抓取網址問號後的變數。
[root@kvm8 html]# vim insertentry.php [root@kvm8 html]# cat insertentry.php <html> <head> <title>Add New Record in MySQL Database</title> </head> <body> <?php if(isset($_POST['add'])) { $dbhost = 'localhost:3306'; $dbuser = 'root'; $dbpass = '123qwe'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } if(! get_magic_quotes_gpc() ) { $dywphp_title = addslashes ($_POST['dywphp_title']); $dywphp_author = addslashes ($_POST['dywphp_author']); } else { $dywphp_title = $_POST['dywphp_title']; $dywphp_author = $_POST['dywphp_author']; } $submission_date = $_POST['submission_date']; $sql = "INSERT INTO dywphp_tbl ". "(dywphp_title,dywphp_author, submission_date) ". "VALUES ". "('$dywphp_title','$dywphp_author','$submission_date')"; mysql_select_db('dywphp'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not enter data: ' . mysql_error()); } echo "Entered data successfully\n"; mysql_close($conn); } else { ?> <form method="post" action="<?php $_PHP_SELF ?>"> <table width="600" border="0" cellspacing="1" cellpadding="2"> <tr> <td width="250">Dywphp Title</td> <td> <input name="dywphp_title" type="text" id="dywphp_title"> </td> </tr> <tr> <td width="250">Dywphp Author</td> <td> <input name="dywphp_author" type="text" id="dywphp_author"> </td> </tr> <tr> <td width="250">Submission Date [ yyyy-mm-dd ]</td> <td> <input name="submission_date" type="text" id="submission_date"> </td> </tr> <tr> <td width="250"> </td> <td> </td> </tr> <tr> <td width="250"> </td> <td> <input name="add" type="submit" id="add" value="Add Dywphp"> </td> </tr> </table> </form> <?php } ?> </body> </html>
http://kvm8.deyu.wang/insertentry.php
,輸入一筆紀錄。
dywphp_tbl
,並回傳寫入成功。