您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP表单数据写入MySQL代码

2025/4/29 0:43:32发布39次查看
插入操作 php if(!isset($_post['submit'])){ //如果没有表单提交,显示一个表单 ?>
国家: 动物名称(英文): 动物名称(中文): php } else { //如果提交了表单 //数据库连接参数 $host = localhost; $user = root; $pass = zq19890319; $db = phpdev; // 何问起 hovertree.com //取得表单中的值,检查表单中的值是否符合标准,并做适当转义,防止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']); //打开数据库连接 $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); } ?>
参考:http://www.cnblogs.com/roucheng/p/phpmysql.html
上面的代码用不同格式,不知道哪种格式更好
插入操作         method=post>      国家:text name=country />      动物名称(英文):text name=animal />      动物名称(中文):text name=cname />      submit name=submit value=提交表单 />        
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product