前后台交互(无ajax等框架)
add.html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>相加</title>
</head>
<body>
<!--GET是从服务器上获取数据(地址栏显示参数,不安全)。 -->
<!--POST是向服务器传送数据(地址栏不显示参数,更安全)。-->
<form action="add.php" method="post">
a:<input type="text" name="a"><br>
b:<input type="text" name="b"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
add.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>相加</title>
</head>
<body>
<?php
if($_POST['a']&&$_POST['b']){
echo $_POST['a']+$_POST['b'];
}else{
echo "提交错误";//含有中文故需html头
}
?>
</body>
</html>
hello.html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>欢迎</title>
</head>
<body>
<form action="hello.php" method="get">
<input name="name" type="">
<input type="submit" value="提交">
</form>
</body>
</html>
hello.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>document</title>
</head>
<body>
<?php
if(isset($_GET['name'])&&$_GET['name']){
echo 'hello'.$_GET['name'];
}else{
echo '请输入名字';
}
?>
</body>
</html>
文件上传
upload.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!--enctype="multipart/form-data"可设置接受二进制文件上传 -->
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="提交">
</form>
</body>
</html>
upload.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>上传文件</title>
</head>
<body>
<?php
$file = $_FILES['file'];
$filename = $file['name'];
move_uploaded_file($file['tmp_name'], $filename);
echo "<img src='$filename'>";
?>
</body>
</html>
与数据库交互
添加用户
adduser.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加用户</title>
</head>
<body>
<form action="adduser.php" method="post">
<div>用户名字:<input type="text" name="name"></div>
<div>用户年龄:<input type="text" name="age"></div>
<input type="submit" value="提交">
</form>
</body>
</html>
adduser.php
<?php
$name = $_POST['name'];
$age = $_POST['age'];
if(!isset($name)){
die('user name not define');
}
if(!isset($age)){
die('user age not define');
}
if(empty($name)){
die('user name is empty');
}
if(empty($age)){
die('user age is empty');
}
require_once 'functions.php';
$conn = connectp1();
$age = intval($age);//非字符串必须强制转化,防止错误数据放入数据库
mysql_query("INSERT INTO users(name,age) VALUES ('$name',$age)");//字符串必须引起来,防止错误数据放入数据库
if(mysql_errno()){//如果出现错误
echo mysql_error();
}else{
header("Location:p1.php");//界面跳转到p1.php
}
config.php
<?php
define('MYSQL_HOST', 'localhost');
define('MYSQL_USER', 'root');
define('MYSQL_PWD','');
?>
functions.php
<?php
require_once 'config.php';//与config.php文件相关联
function connectp1(){
$conn = mysql_connect(MYSQL_HOST,MYSQL_USER,MYSQL_PWD);//连接数据库
if(!$conn){
die('can not connect db');//输出错误信息并运行中断
}
mysql_select_db('myapp');//选择数据表
return $conn;
}
?>
修改用户
edituser.php
<!DOCTYPE html>
<html>
<head>
<meta charset=" UTF-8">
<title>编辑用户</title>
</head>
<body>
<?php
// if(isset($_GET['id'])&&!empty($_GET['id'])){//id在集合中或者id为空
// }
require_once 'functions.php';
if(!empty($_GET['id'])){
connectp1();//functions.php中连接到数据库的函数
$id = intval($_GET['id']);
$result = mysql_query("SELECT * FROM users WHERE id = $id");//应用sql语句
if(mysql_errno()){
die('can not connect db');
}
$arr = mysql_fetch_assoc($result);
// print_r($arr);
}
else{
die('id not define');
}
?>
<form action="edituser_server.php" method="post">
<div>用户ID
<input type="text" name="id" value="<?php echo $arr['id'];?>">
</div>
<div>用户名字
<input type="text" name="name" value="<?php echo $arr['name'];?>">
</div>
<div>用户年龄
<input type="text" name="age" value="<?php echo $arr['age'];?>">
</div>
<input type="submit" value="提交修改">
</form>
</body>
</html>
edituser_server.php
<?php
require_once 'functions.php';
if(empty($_POST['id'])){
die('id is empty');
}
if(empty($_POST['name'])){
die('name is empty');
}
if(empty($_POST['age'])){
die('age is empty');
}
$id = intval($_POST['id']);
$name = $_POST['name'];
$age = intval($_POST['age']);
connectp1();
mysql_query("UPDATE users SET name='$name',age=$age WHERE id=$id");//字符串需加单引号
if(mysql_errno()){
echo mysql_error();
}else{
header("Location:p1.php");//跳转页面
}
p1.php
<?php
require_once 'functions.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户的添加</title>
</head>
<body>
<a href="adduser.html">添加用户
</a>
<table style='text-align:left' border='1'>
<tr><th>id</th><th>名字</th><th>年龄</th><th>修改</th></tr>
<?php
$conn = connectp1();//引用文件functions.php内的connectp1函数
$result = mysql_query("SELECT * FROM users ORDER BY id DESC",$conn);//从数据库表users中倒序导入所有信息
$datacount = mysql_num_rows($result);//记录表中的数据的条数
// echo $datacount.'<br>';//输出数据条数
// for($i=0;$i<$datacount;$i++){//循环输出每条数据
// $result_arr = mysql_fetch_assoc($result);
// print_r($result_arr);
// echo '<br>';
// }
for($i=0;$i<$datacount;$i++){//循环输出每条数据
$result_arr = mysql_fetch_assoc($result);
$id = $result_arr['id'];//允许字符下标
$name = $result_arr['name'];
$age = $result_arr['age'];
echo "<tr><td>$id</td><td>$name</td><td>$age</td><td><a href='edituser.php?id=$id'>修改</a></td></tr>";
}
?>
</table>
</body>
</html>