PHP mysqli 实现简单的增删改查

首先需要下载jquery.js 文件

数据表 tb_mysqli

数据库名为:mysqli_curd


DROP TABLE IF EXISTS `tb_mysqli`;
CREATE TABLE `tb_mysqli` (
  `uid` int(100) NOT NULL AUTO_INCREMENT,
  `uname` varchar(100) DEFAULT NULL,
  `pswd` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`uid`)
)



创建首页:login_reg.php

  
  
 
登录和注册  
 
 
 
用户名:
密码:



创建 登录:login.php


";
$uname=$_POST["uname"];
$pswd=$_POST["pswd"];
echo $uname." ".$pswd;
//造对象
$db = new MySQLi("localhost","root","","mysqli_curd");
//判断是否出错
!mysqli_connect_error() or die("连接失败!!");   
//SQL语句
$sql = "select pswd from tb_mysqli where uname='$uname'";
// 执行SQL语句
$result = $db-> query($sql);
$v = $result->fetch_row();  
//  mysql_fetch_row() 函数从结果集中取得一行作为数字数组
if($pswd==$v[0])  
{  
header("location:yes.php");  
}  
else  
{  
echo"您输入的用户名或密码不正确,请重新输入!!";  
}  
?>



注册:reg.php


";
$uname=$_POST["uname"];
$pswd=$_POST["pswd"];
echo $uname." ".$pswd;
//造对象
$db = new MySQLi("localhost","root","","mysqli_curd");
//判断是否出错
!mysqli_connect_error() or die("连接失败!!");
// 判断用户名是否已经存在
$sql_exist="select * from tb_mysqli where uname='$uname'"; 
// 执行SQL语句
$count = $db-> query($sql_exist);
$v = $count->fetch_row();  
if($v>=1)
{
echo "用户名已经存在";
}else {
$sql = "insert into tb_mysqli (uname,pswd) values ('$uname','$pswd');";
    // 执行SQL语句
    $result = $db-> query($sql);
    if($result)  
    {  
    header("location:yes.php");  
    }  
    else  
    {  
    echo"很抱歉,注册失败!!";  
    }  

}

?>

添加一个yes.php


";

?>
显示全部信息


显示全部:showall.php




全部信息


 query($sql);
while($attr=$result->fetch_row())
{
echo 
"
";
?>
ID 用户名 密码 删除 修改
{$attr[0]} {$attr[1]} {$attr[2]}" οnclick="return window.confirm('确认删除?')">删除 " οnclick="return window.confirm('确认修改?')">修改



删除:delete.php


";
//造对象
$db = new MySQLi("localhost","root","","mysqli_curd");
//判断是否出错
!mysqli_connect_error() or die("连接失败!!"); 
//SQL语句
$sql = "delete from tb_mysqli where uid='$uid'";
// 执行
$result = $db-> query($sql);
if($result)
{
echo 
"


";
}else 
{
echo 
"

";
}
?>



修改:getone.php



 



 
 query($sql);
while($attr=$result->fetch_row())
{
$uname=$attr[1];
$pswd=$attr[2];
}
?>
用户名:

密码:




执行修改:update.php

 query($sql);
if($result)
{
echo 
"


";
}else 
{
echo 
"

";
}
?>




你可能感兴趣的:(PHP,增删改查,初学,mysqli,CURD)