下面是在Linux上登录mysql,创建数据库和创建表的过程。
yin@yin-Ubuntu10:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 360
Server version: 5.1.41-3ubuntu12.1 (Ubuntu)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database UseCase;
Query OK, 1 row affected (0.00 sec)
mysql> use UseCase;
Database changed
mysql> create table User(UserName varchar(20) primary key,Password varchar(20) not null,CreateTime timestamp default current_timestamp);
Query OK, 0 rows affected (0.01 sec)下面就来建立一个页面来完成新建用户的页面。首先是一个简单的表单:
复制代码 代码如下:
PHP通过$_POST数组来获得通过post方法提交的表单中的数据。在PHP程序中,我们首先要判断是有OK字段,从而判断出该页面是首次访问,还是用户点击OK后提交的,接着判断两次密码输入是否统一。然后就可以获取到用户名和密码,插入数据库中。PHP连接MySQL数据库一般可以利用mysql扩展或者mysqli扩展,mysqli扩展比较新一点,这里我们采用这种方式。mysqli可能需要安装配置下,不过在我的环境中是默认装好的。利用mysqli扩展操作数据库一般分为如下几步:构造mysqli对象,构造statement,绑定参数,执行,关闭。代码如下:
复制代码 代码如下:
$match=true;
if(isset($_POST["ok"])) {
$pwd=$_POST["Password"];
$pwdConfirm=$_POST["ConfirmPassword"];
$match=($pwd==$pwdConfirm);
$conn=new mysqli("localhost","root","123","UseCase");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query="insert into User(UserName,Password) values(?,?)";
$stmt=$conn->stmt_init();
$stmt->prepare($query);
$stmt->bind_param('ss',$name,$pwd);
$name=$_POST["UserName"];
$pwd=$_POST["Password"];
$stmt->execute();
if($stmt->errno==0) {
$success=true;
}else {
$success=false;
}
$stmt->close();
$conn->close();
}
?>
其中bind_param方法需要稍微解释下,第一个参数的含义是参数类型。每个字符对应一个参数,s表示字符串,i表示整数,d表示浮点数,b表示blob。最后,再为这个页面添加一点提示信息:
复制代码 代码如下:
if(!$match) { ?>
Password and Confirm Password must match.
}
?>
if(isset($success)) {
if($success) {
echo '
User Created Successfully!';
}elseif($sucess==false) {
echo '
User Name existed.';
}
}
?>
再接下来,我们编写一个用户列表页面。
复制代码 代码如下:
User Name | CreateTime | Action |
---|---|---|
= $row['UserName'] ?> | = date('Y-m-d',strtotime($row['CreateTime']))?> |
Edit Delete |

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

记事本++7.3.1
好用且免费的代码编辑器

Dreamweaver Mac版
视觉化网页开发工具

SublimeText3 Linux新版
SublimeText3 Linux最新版