这个系列的教程由表单开始写,因为表单可以把数据库和web 之间的交互表现得很明显。提交表单 ,数据库记录注册信息。
<form action='index.php' method='post'> //这里的index.php 就是一个动态传送的文件。后面会说。 <p>please type your first name:<input type="text" name="first_name" value=""></p> <p>please type your second name:<input type="text" name="second_name" value="" ></p> <p>please type your age:<input type="text" name="age" value=""></p> <p><input type="submit" name='submit' value='register'></p> </form>
<p>please type your first name:<input type="text" name="first_name" value="< if(isset($_REQUEST['first_name'])) echo $_REQUEST['first_name'];>"></p> <p>please type your second name:<input type="text" name="second_name" value="< if(isset($_REQUEST['first_name'])) echo $_REQUEST['second_name'];>" ></p> <p>please type your age:<input type="text" name="age" value="< if(isset($_REQUEST['age'])) echo $_REQUEST['age'];>"></p> <p><input type="submit" name='submit' value='register'></p>
$error=array(); if (empty($_REQUEST['first_name'])) { echo '<p>you forget type your first_name;<p>' ;# code... $error='1';
} else{ $f=$_REQUEST['first_name'];
} if (empty($_REQUEST['second_name'])) { echo '<p>you forget type your second_name;<p>' ;# code... $error='2';
} else{ $s=$_REQUEST['second_name'];
} if (empty($_REQUEST['age'])) { echo '<p>you forget type your age;<p>'; # code... $error='3';
} else{ $a=$_REQUEST['age'];
}
if (empty($error)) {
echo'<p> everything is ok<p>';
require('connect/mysqli_connect.php');
$q="insert into user (first_name,second_name,age) value('$f','$s','$a')";
$r=@mysqli_query($dbc,$q);
}
< $dbc=@mysqli_connect(localhost,root,123,test); >
create table user(
first_name varchar(20) not null,
second_name varchar(20) not null,
age varchar(20) not null );
$q="insert into user (first_name,second_name,age) value('$f','$s','$a')";
$r=@mysqli_query($dbc,$q);
< include('includes/header.html'); echo '<p>this is a test</p>'; $error=array(); if (empty($_REQUEST['first_name'])) { echo '<p>you forget type your first_name;<p>' ;# code... $error='1';
} else{ $f=$_REQUEST['first_name'];
} if (empty($_REQUEST['second_name'])) { echo '<p>you forget type your second_name;<p>' ;# code... $error='2';
} else{ $s=$_REQUEST['second_name'];
} if (empty($_REQUEST['age'])) { echo '<p>you forget type your age;<p>'; # code... $error='3';
} else{ $a=$_REQUEST['age'];
} if (empty($error)) { echo'<p> everything is ok<p>';# code... require('connect/mysqli_connect.php'); $q="insert into user (first_name,second_name,age) value('$f','$s','$a')"; $r=@mysqli_query($dbc,$q);
} >
<form action='index.php' method='post'>
<p>please type your first name:<input type="text" name="first_name" value="< if(isset($_REQUEST['first_name'])) echo $_REQUEST['first_name'];>"></p>
<p>please type your second name:<input type="text" name="second_name" value="< if(isset($_REQUEST['first_name'])) echo $_REQUEST['second_name'];>" ></p>
<p>please type your age:<input type="text" name="age" value="< if(isset($_REQUEST['age'])) echo $_REQUEST['age'];>"></p>
<p><input type="submit" name='submit' value='register'></p>
</form>
同目录下connect 下的mysqli_connect.php 文件。
里面就是
< $dbc=@mysqli_connect(localhost,root,123,test); >
应该还有一个在同目录下的includes文件夹下的 header.html 文件。这个就随便了。
热门源码