博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

php使用insert语句动态添加用户

Posted on 2018-12-14 19:16  勇敢的鑫  阅读(713)  评论(0编辑  收藏  举报
<html>
<head>
   <title>Adding User</title>
</head>
<body>
  <h2>Adding users to mysql database.</h2>
  <form action="insertformhandler.php" method="post">
    Select gender:
	 <select name="gender">
	   <option value="male">man</option>
	   <option value="female">woman</option>
	 </select><br />
    Fill user name:
     <input name="username" type="text" size="20"/> <br />
	Fill user age:
     <input name="age" type="text" size="3"/> <br />
	Fill user info:
     <input name="info" type="text" size="60"/> <br />
	 <input name="submit" type="submit" value="Add"/>
  </form>
</body>
</html>

  

<html>
<head>
   <title>User adding</title>
</head>
<body>
   <h2>adding new user.</h2>
<?php
    $username = $_POST['username'];
	$gender = $_POST['gender'];
	$age = $_POST['age'];
	$info = $_POST['info'];
	if(!$username and !$gender and !$age and !$info){
	    echo "Error: There is no data passed.";
		exit;
	}
    if(!$username or !$gender or !$age or !$info){
	    echo "Error: Some data did not be passed.";
		exit;
	}
	if(!get_magic_quotes_gpc()){
	    $username = addslashes($username);
		$gender = addslashes($gender);
		$age = addslashes($age);
		$info = addslashes($info);
	}
		@ $db = mysqli_connect('localhost','root','12345678');
		mysqli_select_db($db,'testphp');
		if(mysqli_connect_errno()){
	 	 echo "Error: Could not connect to mysql database.";
	 	 exit;
		}
		$q = "INSERT INTO user( id,name, age, gender, info) VALUES (2,'$username',$age,'$gender', '$info')";
		if( !mysqli_query($db,$q)){
		   echo "no new user has been added to database.";
		}else{
		   echo "New user has been added to database.";
		};
		mysqli_close($db);
?>
</body>
</html>