hq金水

愿你是阳光,明媚不忧伤~
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

案例:主页面的增删改查

Posted on 2016-06-15 08:57  hq金水  阅读(297)  评论(0编辑  收藏  举报

主页面:

主页面代码:

<body>
<h1 align="center">主页面</h1>
<table width="100%" border="1" cellpadding="0"  cellspacing="0">
<tr>
<th>代号</th>
<th>姓名</th>
<th>性别</th>
<th>民族</th>
<th>生日</th>
<th>操作</th>
</tr>
<?php
$db=new MySQLi("localhost","root","123","info");
!mysqli_connect_error() or die("连接失败");
$sql="select * from info";
$result=$db->query($sql);
$attr=$result->fetch_all();
foreach($attr as $v)
{    $s=$v[2]==1?'男':'女';
    
    $sq="select name from nation where code='{$v[3]}'";
    $r=$db->query($sq);
    $a=$r->fetch_row();
    
    echo "<tr>
    <td align='center'>{$v[0]}</td>
    <td align='center'>{$v[1]}</td>
    <td align='center'>{$s}</td>
    <td align='center'>{$a[0]}</td>
    <td align='center'>{$v[4]}</td>
    <td align='center'><a href='delete.php?code={$v[0]}'>删除</a>
    <a href='update.php?code={$v[0]}'>修改</a>
    </td>
    </tr>";
}
?>
</table>
<br />
<a href="add.php">添加数据</a>
</body>
</html>

 

添加页面:

增加代码:

除了整数、小数、bool型的不加引号,其他的都加引号

<form action="addchuli.php" method="post">
代号:<input type="text"  name="code" /><br />
姓名:<input type="text"  name="name"/><br />
性别:<input type="radio" value="1" name="sex"/>男<input type="radio" value="0" name="sex"/>女<br />
民族:<select name="select">
<?php
    $db=new MySQLi("localhost","root","123","info");    
    !mysqli_connect_error() or die("连接错误");
    $sql="select * from nation";
    $result=$db->query($sql);
    /*while($attr=$result->fetch_row())
    {
        echo "<option value='{$attr[0]}'>{$attr[1]}</option>";
    }*/
    $attr=$result->fetch_all();
    foreach($attr as $v)
    {
        echo "<option value='{$v[0]}'>{$v[1]}</option>";
    }    
?>
</select>
<br />
生日:<input type="date" name="birthday"><br />
<input type="submit"  value="提交"/>
</form>

添加处理页面代码:

<?php
header("content-type:text/html;charset=utf-8");
$code=$_POST["code"];
$name=$_POST["name"];
@$sex=$_POST["sex"];
$nation=$_POST["select"];
$birthday=$_POST["birthday"];

$db=new MySQLi("localhost","root","123","info");
!mysqli_connect_error() or die("连接失败");
$sql="insert into info values('{$code}','{$name}',{$sex},'{$nation}','{$birthday}')";
var_dump($sql);
$result=$db->query($sql);
if($result)
{
    header("location:main.php");
    }
else
{
    echo "添加失败";
}

 

删除处理页面代码:

<?php
header("content-type:text/html;charset=utf-8");
$code=$_GET["code"];
$db=new mysqli("localhost","root","123","info");
!mysqli_connect_error() or die("连接失败");
$sql="delete from info where code='{$code}'";
$result=$db->query($sql);
if($result)
{header("location:main.php");}
else
{echo "删除失败";}

 

修改显示页面:主键值是不允许修改的

代码:注意php的嵌入位置

<body>
<?php
$code=$_GET["code"];
$dx=new MySQLi("localhost","root","123","info");
!mysqli_connect_error() or die("连接失败");
$sq="select * from info where code='{$code}'";
$re=$dx->query($sq);
$att=$re->fetch_row();
?>
<form action="xiugaichuli.php" method="post">
代号:<input type="text"  name="code"  readonly="readonly" value="<?php echo $att[0];?>"/>
<br />
姓名:<input type="text"  name="name" value="<?php echo $att[1];?>"/><br />
性别:<input type="radio" value="1" name="sex" <?php echo $att[2]?"checked='checked'":""; ?>/><input type="radio" value="0" name="sex" <?php echo $att[2]?"":"checked='checked'";?>/><br />
民族:<select name="select">
<?php
    $db=new MySQLi("localhost","root","123","info");    
    !mysqli_connect_error() or die("连接错误");
    $sql="select * from nation";
    $result=$db->query($sql);
    /*while($attr=$result->fetch_row())
    {
        echo "<option value='{$attr[0]}'>{$attr[1]}</option>";
    }*/
    $attr=$result->fetch_all();
    foreach($attr as $v)
    {    
        if($att[3]==$v[0])
        {
            echo "<option  value='{$v[0]}'>{$v[1]}</option>";
        }
            echo "<option value='{$v[0]}'>{$v[1]}</option>";
    }    
?>
</select>
<br />
生日:<input type="text" name="birthday" value="<?php echo $att[4];?>"/>
<br />
<input type="submit"  value="修改"/>
</form>
</body>

修改处理页面:

<?php
header("content-type:text/html;charset=utf-8");
$code=$_POST["code"];
$name=$_POST["name"];
$sex=$_POST["sex"];
$nation=$_POST["select"];
$bir=$_POST["birthday"];

$db=new mysqli("localhost","root","123","info");
!mysqli_connect_error() or die("连接失败");
$sql="update info set name='{$name}',sex={$sex},nation='{$nation}',birthday='{$bir}' where code='{$code}'";
echo $sql;
$result=$db->query($sql);
var_dump($result);
if($result)
{    
    header("location:main.php");
    }
else
{    
    echo "修改失败";
    }