php修改数据

一、先建立一个主页面main.php,这个页面是用户可见的:

 

代码如下:

<h1>显示users信息</h1>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
    <tr>
        <td>代号</td>
        <td>用户名</td>
        <td>密码</td>
        <td>姓名</td>
        <td>性别</td>
        <td>生日</td>
        <td>民族</td>
        <td>操作</td>
    </tr>
    
    <?php
    $db = new MySQLi("localhost","root","123","text_0306");
    $sql = "select * from users";
    $result = $db->query($sql);
    $arr = $result->fetch_all();
    foreach($arr as $v)
    {
        //修改性别
        $sex = $v[4]?"":"";
        
        //修改民族
        $sql1 = "select name1 from minzu where code='{$v[6]}'";//从minzu表查找name
        $r1 = $db->query($sql1);
        $a1 = $r1->fetch_row();//遍历出每个索引下的数据
        
        echo "<tr>
        <td>{$v[0]}</td>
        <td>{$v[1]}</td>
        <td>{$v[2]}</td>
        <td>{$v[3]}</td>
        <td>{$sex}</td>
        <td>{$v[5]}</td>
        <td>{$a1[0]}</td>
        <td><a href='del.php? code={$v[0]}' onclick=\"return confirm('确认删除么?')\">删除</a><a href='update.php?code={$v[0]}'>修改</a></td>
    </tr>";
    }
    ?>
    
</table>
</body>

二、建立update.php页面,用php代码连接数据库后做一个form表单填写信息:

<body>
<?php
$code=$_GET["code"];
$db=new MySQLi("localhost","root","123","text_0306");
$sql="select * from users where code='{$code}'";
$result=$db->query($sql);
$arr=$result->fetch_row();
?>
<h1>修改数据</h1>
<form action="xiugai.php" method="post">
<div><input type="hidden" name="code" value= "<?php echo $arr[0]; ?>" /></div><!--用php代码写默认值,因为修改数据是在已经拥有的数据基础上进行修改-->
<div>用户名:<input type="text" name="uid" value= "<?php echo $arr[1]; ?>" /></div>
<div>密码:<input type="password" name="pwd" value= "<?php echo $arr[2]; ?>" /></div>
<div>姓名:<input type="text" name="name" value= "<?php echo $arr[3]; ?>" /></div>
<div>
性别:
男<input type="radio" name="sex" value="1" <?php echo $arr[4]?"checked='checked'":""; ?>/><!--这里用三元运算来进行性别的默认--><input type="radio" name="sex" value="0" <?php echo $arr[4]?"":"checked='checked'"; ?> />
</div>
<div>生日:<input type="text" name="birthday" value="<?php echo $arr[5]; ?>" /></div>
<div>
民族:
<select name="nation">
<?php
$sqln="select * from minzu";//minzu表由于有许多民族所以用下拉框
$resultn=$db->query($sqln);
$arrn=$resultn->fetch_all();
foreach($arrn as $v)
{
    $arr[6]; //该人员的民族代号
    $v[0]; //即将造的option的民族代号
    if($arr[6]==$v[0])
    {
        echo"<option selected='selected' value='{$v[0]}'>{$v[1]}</option>";    //选出默认的民族
    }
    else
    {
        echo "<option value='{$v[0]}'>{$v[1]}</option>";
    }
}

?>
</select>
</div>
<input type="submit" value="修改" />

</form>
</body>
</html>

三、建立修改页面xiugai.php进行修改:

         修改后                   

 

 

代码如下:

<?php
$code=$_POST["code"];//取数据
$uid=$_POST["uid"];
$pwd=$_POST["pwd"];
$name=$_POST["name"];
$sex=$_POST["sex"];
$birthday=$_POST["birthday"];
$nation=$_POST["nation"];

$db=new MySQLi("localhost","root","123","text_0306");
$sql="update users set uid='{$uid}',pwd='{$pwd}',name='{$name}',sex={$sex},birthday='{$birthday}',nation='{$nation}' where code='{$code}'";//修改每个列名下的数据

if($db->query($sql))
{
    header("location:main.php");    
}
else
{
    echo "修改失败";
}
?>

 

posted @ 2017-04-27 16:17  梦深深处  阅读(1506)  评论(0编辑  收藏  举报