<h1>插入操作</h1> <?<span style="color: #000000;">php </span><span style="color: #0000ff;">if</span>(!<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_POST</span>['submit'<span style="color: #000000;">])){ </span><span style="color: #008000;">//</span><span style="color: #008000;">如果没有表单提交,显示一个表单 </span> ?> <form action="" method="post"><span style="color: #000000;"> 国家:</span><input type="text" name="country" /><span style="color: #000000;"> 动物名称(英文):</span><input type="text" name="animal" /><span style="color: #000000;"> 动物名称(中文):</span><input type="text" name="cname" /> <input type="submit" name="submit" value="提交表单" /> </form> <?<span style="color: #000000;">php } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #008000;">//</span><span style="color: #008000;">如果提交了表单 //数据库连接参数 </span> <span style="color: #800080;">$host</span> = "localhost"<span style="color: #000000;">; </span><span style="color: #800080;">$user</span> = "root"<span style="color: #000000;">; </span><span style="color: #800080;">$pass</span> = "zq19890319"<span style="color: #000000;">; </span><span style="color: #800080;">$db</span> = "phpdev"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 何问起 hovertree.com //取得表单中的值,检查表单中的值是否符合标准,并做适当转义,防止SQL注入 </span> <span style="color: #800080;">$country</span> = <span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$_POST</span>['country'])? <span style="color: #0000ff;">die</span>("请输入国家名称"): <span style="color: #008080;">mysql_escape_string</span>(<span style="color: #800080;">$_POST</span>['country'<span style="color: #000000;">]); </span><span style="color: #800080;">$animal</span> = <span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$_POST</span>['animal'])? <span style="color: #0000ff;">die</span>("请输入英文名"): <span style="color: #008080;">mysql_escape_string</span>(<span style="color: #800080;">$_POST</span>['animal'<span style="color: #000000;">]); </span><span style="color: #800080;">$cname</span> = <span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$_POST</span>['cname'])? <span style="color: #0000ff;">die</span>("请输入中文名"): <span style="color: #008080;">mysql_escape_string</span>(<span style="color: #800080;">$_POST</span>['cname'<span style="color: #000000;">]); </span><span style="color: #008000;">//</span><span style="color: #008000;">打开数据库连接 </span> <span style="color: #800080;">$connection</span> = <span style="color: #008080;">mysql_connect</span>(<span style="color: #800080;">$host</span>, <span style="color: #800080;">$user</span>, <span style="color: #800080;">$pass</span>) or <span style="color: #0000ff;">die</span>("Unable to connect!"<span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">选择数据库 </span> <span style="color: #008080;">mysql_select_db</span>(<span style="color: #800080;">$db</span>) or <span style="color: #0000ff;">die</span>("Unable to select database!"<span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">构造一个SQL查询 </span> <span style="color: #800080;">$query</span> = "INSERT INTO symbols(country, animal, cname) VALUE('<span style="color: #800080;">$country</span>', '<span style="color: #800080;">$animal</span>', '<span style="color: #800080;">$cname</span>')"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;">执行该查询 </span> <span style="color: #800080;">$result</span> = <span style="color: #008080;">mysql_query</span>(<span style="color: #800080;">$query</span>) or <span style="color: #0000ff;">die</span>("Error in query: <span style="color: #800080;">$query</span>. ".<span style="color: #008080;">mysql_error</span><span style="color: #000000;">()); </span><span style="color: #008000;">//</span><span style="color: #008000;">插入操作成功后,显示插入记录的记录号 </span> <span style="color: #0000ff;">echo</span> "记录已经插入, mysql_insert_id() = ".<span style="color: #008080;">mysql_insert_id</span><span style="color: #000000;">(); </span><span style="color: #008000;">//</span><span style="color: #008000;">关闭当前数据库连接 </span> <span style="color: #008080;">mysql_close</span>(<span style="color: #800080;">$connection</span><span style="color: #000000;">); } </span>?>
参考:http://www.cnblogs.com/roucheng/p/phpmysql.html
上面的代码用不同格式,不知道哪种格式更好