修改数据

03.php

<?php
$conn=mysql_connect("localhost","root","111111");
mysql_query("set names utf8",$conn);//设置编码
mysql_query("use test1",$conn);//选库
$id=addslashes($_GET['id']);
$sql="select id,name,gender from stu where id=".$id;
$rs=mysql_query($sql,$conn);
$arr=mysql_fetch_array($rs);
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>编辑</title>
</head>
<body>
<form action=<?php echo "04.php?id=".$id;?> method="post">
<table border=1 width="50%">
    <tr>
        <td>id</td>
        <td><?php echo $arr["id"]; ?></td>
    </tr>
    <tr>
        <td>name</td>
        <td><input type="text" name="name" value="<?php echo $arr['name']?>"></td>
    </tr>
    <tr>
        <td>gender</td>
        <td><input type="text" name="gender" value="<?php echo $arr['gender']?>"></td>
    </tr>
    <tr>
        <td colspan="2"><input type="submit" value="保存修改"></td>
    </tr>
</table>
 </form>   
</body>
</html>

 04.php

<?php
$conn=mysql_connect("localhost","root","111111");
mysql_query("set names utf8",$conn);//设置编码
mysql_query("use test1",$conn);//选库
$id=$_GET['id']+0;
$name=addslashes($_POST['name']);
$gender=addslashes($_POST['gender']);
$sql="update stu set name='".$name."',gender='".$gender."' where id=".$id;
$rs=mysql_query($sql,$conn);
if(mysql_affected_rows($conn)==0){
    echo "修改失败,可能与原数据一样";
}else{
    echo "修改成功";
}
echo "<a href='01.php'>返回</a>";

?>

 

posted @ 2015-08-16 22:30  飘逸110  阅读(134)  评论(0编辑  收藏  举报