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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.