php+mysql处理表单(代码)

<?php
function insert_db($sid, $name, $age)
{
	//包含文件
	include('db_info.php');
	include('DB.php');
	//连接mysql
	$DBconnection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");
	if(!$DBconnection)
	{
		die("Could not connect to database: <br />".DB::errorMessage());
	}
	//对表单提交数据转义,以防sql注入攻击
	if(get_magic_quotes_gpc())
	{
		$sid = stripslashes($sid);
		$name = stripslashes($name);
		$age = stripslashes($age);
	}
	$sid = mysql_real_escape_string($sid);
	$name = mysql_real_escape_string($name);
	$age = mysql_real_escape_string($age);
	$query = "insert into t_student values('$sid','$name','$age')";
	$result = $DBconnection->query($query);
	if(DB::isError($result))
	{
		die("Could not query the database:<br />".$query." ".DB::errorMessage());
	}
	echo "Inserted OK";
	$query = "select * from t_student";
	$result = $DBconnection->query($query);
	if(DB::isError($result))
	{
		die("Could not query the database:<br />".$query." ".DB::errorMessage());
	}
	echo '<table border="1">';
	echo "<tr><th>sid</th> <th>name</th> <th>age</th></tr>";
	while($result_row = $result->fetchRow(DB_FETCHMODE_ASSOC))
	{
		echo "<tr><td>";
		echo $result_row["id"].'</td><td>';
		echo $result_row["name"].'</td><td>';
		echo $result_row["age"].'</td></tr>';
	}
	echo "</table>";	
	$DBconnection->disconnect();
}
?>

<html>
<head>
	<title>Insert from a form</title>
</head>
<body>
	<?php
	$sid = htmlentities($_GET["sid"]);
	$name = htmlentities($_GET["name"]);
	$age = htmlentities($_GET["age"]);
	if(NULL!= $sid && NULL!=$name && NULL!=$age)
	{
		insert_db($sid, $name, $age);
	}
	else
	{
		echo '
			<h1>my insert demo</h1>
			<form action="'.$_SERVER["PHP_SELF"].'" method="GET" >
				<label>sid:<input type="text" name="sid" ></label>
				<label>name:<input type="text" name="name"></label>
				<label>age:<input type="text" name="age"></label>
				<input type="submit" value="GO"></input>
			</form>
		';
	}
	?>
</body>
</html>






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值