IBM 学习php 第一部分
一、页面表单提交到另一页面

fixed php
<html>
<head><title>Workflow System</title></head>
<body>
<h1>Register for an Account:</h1>
<form action="registration_action.php" method="POST">
Username: <input type="text" name="name" /><br />
Email: <input type="text" name="email" /><br />
Password: <input type="password" name="pword[]" /><br />
Password (again): <input type="password" name="pword[]" /><br />
<input type="submit" value="GO" />
</form>
</body>
</html>

registration_action.php
<html>
<body>
<p>You entered:</p>
<?php
//$username = $_POST['name'];
//$password = $_POST['pword'];
//echo "<p>Username = " . $username . "</p>";
//echo "<p>Password[1] = " . $password[0] . "</p>";
//echo "<p>Password[2] = " . $password[1] . "</p>";
function validate($allSubmitted){
$message = "";
$passwords = $allSubmitted['pword'];
$firstPass = $password[0];
$secondPass = $password[1];
$username = $allSubmitted['name'];
echo "<p>Username = " . $username . "</p>";
echo "<p>Password[1] = " . $firstPass . "</p>";
echo "<p>Password[2] = " . $secondPass . "</p>";
if ($firstPass != $secondPass) {
$message = $message."Passwords don't match<br />";
}
if (strlen($username) < 5 || strlen($username) > 50){
$message = $message."Username must be between 5 and 50 characters<br />";
}
if ($message == ""){
$message = "OK";
}
return $message;
}
if (validate($_POST) == "OK") {
echo "<p>Thank you for registering!</p>";
} else {
echo "<p>There was a problem with your registration:</p>";
echo validate($_POST);
echo "<p>Please try again.</p>";
}
?>
</body>
</html>

login.php
1
<html>
2
<head><title>Workflow System</title></head>
3
<body>
4
<h1>Register for an Account:</h1>
5
<form action="registration_action.php" method="POST">
6
7
name: <input type="text" name="name" /><br />
8
ID: <input type="text" name="id" /><br />
9
Password: <input type="password" name="pword[]" /><br />
10
Password (again): <input type="password" name="pword[]" /><br />
11
msn: <input type="text" name="msn" /><br />
12
address: <input type="text" name="address" /><br />
13
intruduction: <input type="text" name="intruduction" /><br />
14
school: <input type="text" name="school" /><br />
15
birthday: <input type="text" name="birthday" /><br />
16
<input type="submit" value="GO" />
17
</form>
18
19
</body>
20
</html>
21
22

registration_action2.php
1
<html>
2
<body>
3
<p>You entered:</p>
4
5
<?php
6
$username = $_POST['name'];
7
$password = $_POST['pword'];
8
$id = $_POST['id'];
9
$birthday = $_POST['birthday'];
10
$msn = $_POST['msn'];
11
$address = $_POST['address'];
12
$intruduction = $_POST['intruduction'];
13
$school = $_POST['school'];
14
15
echo "<p>Username = " . $username . "</p>";
16
echo "<p>Password[1] = " . $password[0] . "</p>";
17
echo "<p>Password[2] = " . $password[1] . "</p>";
18
echo "<p>ID= " . $id . "</p>";
19
echo "<p>birthday = " . $birthday . "</p>";
20
echo "<p>msn = " . $msn . "</p>";
21
echo "<p>address = " . $address . "</p>";
22
echo "<p>intruduction = " . $intruduction . "</p>";
23
echo "<p>school = " . $school . "</p>";
24
25
26
27
function db_connect($user='root',
28
$password='zdndd', $db='shop'){
29
30
mysql_connect('localhost', $user, $password)
31
or die('I cannot connect to db: ' . mysql_error());
32
mysql_select_db($db);
33
34
}
35
db_connect();
36
37
$sql = "insert into della (name, address, school, id, birthday, msn, intruduction) values
38
('".$username."', '".$address."', '".$school."', '".$id."', '".$birthday."', '".$msn."', '".$intruduction."')";
39
$result = mysql_query($sql);
40
41
if ($result){
42
echo "It's entered!";
43
} else {
44
echo "There's been a problem: ".mysql_error();
45
}
46
?>
47
</body>
48
</html>
49
<html>
<head><title>Workflow System</title></head>
<body>
<h1>Register for an Account:</h1>
<form action="registration_action.php" method="POST">
Username: <input type="text" name="name" /><br />
Email: <input type="text" name="email" /><br />
Password: <input type="password" name="pword[]" /><br />
Password (again): <input type="password" name="pword[]" /><br />
<input type="submit" value="GO" />
</form>
</body>
</html>
<html>
<body>
<p>You entered:</p>
<?php
//$username = $_POST['name'];
//$password = $_POST['pword'];
//echo "<p>Username = " . $username . "</p>";
//echo "<p>Password[1] = " . $password[0] . "</p>";
//echo "<p>Password[2] = " . $password[1] . "</p>";
function validate($allSubmitted){
$message = "";
$passwords = $allSubmitted['pword'];
$firstPass = $password[0];
$secondPass = $password[1];
$username = $allSubmitted['name'];
echo "<p>Username = " . $username . "</p>";
echo "<p>Password[1] = " . $firstPass . "</p>";
echo "<p>Password[2] = " . $secondPass . "</p>";
if ($firstPass != $secondPass) {
$message = $message."Passwords don't match<br />";
}
if (strlen($username) < 5 || strlen($username) > 50){
$message = $message."Username must be between 5 and 50 characters<br />";
}
if ($message == ""){
$message = "OK";
}
return $message;
}
if (validate($_POST) == "OK") {
echo "<p>Thank you for registering!</p>";
} else {
echo "<p>There was a problem with your registration:</p>";
echo validate($_POST);
echo "<p>Please try again.</p>";
}
?>
</body>
</html>
在registration_action.php当中
$firstPass = $password[0];
$secondPass = $password[1]; 没有找到数据;
二、页面表单录入数据库
该表单也是和上面的fixed.php中差不多,多出部分是della表单中的元素;
1
<html>2
<head><title>Workflow System</title></head>3
<body>4
<h1>Register for an Account:</h1>5
<form action="registration_action.php" method="POST">6

7
name: <input type="text" name="name" /><br />8
ID: <input type="text" name="id" /><br />9
Password: <input type="password" name="pword[]" /><br />10
Password (again): <input type="password" name="pword[]" /><br />11
msn: <input type="text" name="msn" /><br />12
address: <input type="text" name="address" /><br />13
intruduction: <input type="text" name="intruduction" /><br />14
school: <input type="text" name="school" /><br />15
birthday: <input type="text" name="birthday" /><br />16
<input type="submit" value="GO" />17
</form>18

19
</body>20
</html>21

22

1
<html>2
<body>3
<p>You entered:</p>4

5
<?php 6
$username = $_POST['name'];7
$password = $_POST['pword'];8
$id = $_POST['id'];9
$birthday = $_POST['birthday'];10
$msn = $_POST['msn'];11
$address = $_POST['address'];12
$intruduction = $_POST['intruduction'];13
$school = $_POST['school'];14

15
echo "<p>Username = " . $username . "</p>"; 16
echo "<p>Password[1] = " . $password[0] . "</p>"; 17
echo "<p>Password[2] = " . $password[1] . "</p>"; 18
echo "<p>ID= " . $id . "</p>"; 19
echo "<p>birthday = " . $birthday . "</p>"; 20
echo "<p>msn = " . $msn . "</p>";21
echo "<p>address = " . $address . "</p>"; 22
echo "<p>intruduction = " . $intruduction . "</p>"; 23
echo "<p>school = " . $school . "</p>";24
25

26

27
function db_connect($user='root',28
$password='zdndd', $db='shop'){29

30
mysql_connect('localhost', $user, $password)31
or die('I cannot connect to db: ' . mysql_error());32
mysql_select_db($db);33

34
}35
db_connect();36
37
$sql = "insert into della (name, address, school, id, birthday, msn, intruduction) values 38
('".$username."', '".$address."', '".$school."', '".$id."', '".$birthday."', '".$msn."', '".$intruduction."')";39
$result = mysql_query($sql);40

41
if ($result){42
echo "It's entered!";43
} else {44
echo "There's been a problem: ".mysql_error();45
}46
?>47
</body>48
</html>49

1.函数 db_connect 当中的$user='root',$password='zdndd', $db='shop' 都是我phpmyadmin中的用户名、密码、shop数据库;
2.$sql = "insert into della (name, address, school, id, birthday, msn, intruduction) values della是一个数据库表,本来这里不是della的,不写肯定是要报错的了,还要我够聪明看到了phpmyadmin当中的SQL语言,哇哈哈!搞定!

浙公网安备 33010602011771号