http://www.xn--ttss6e.net
  • 生活导航网杭州第一网奎屯门户
  • 更新指定的图书信息,当用户单击“修改”超链接时,显示图书详细信息,并可对图书信息进行编辑,然后单击“提交”按钮既可完成修改操作,运行结果如下所示:

    具体步骤如下:

    (1)创建数据库连接文件conn.php,代码如下:

    <?php

    $conn=mysql_connect("localhost","root","111"); //连接数据库服务器

    mysql_select_db("db_database08",$conn); //连接db_database08数据库

    mysql_query("set names utf8"); //设置数据库编码格式

    ?>

    (2)创建index.php文件,设计图书信息浏览页面,显示所有图书信息,代码如下:

    <!DOCTYPE>

    <html>

    <head>

    <meta charset="utf-8" />

    <title></title>

    <style type="text/css">

    * { margin:0; padding:0; }

    body { font-family:Verdana, Arial, Helvetica, sans-serif; font-size:16px; width:1010px; margin:0 auto; }

    p { line-height:220%; }

    #layout {background:#cccccc;font-size:18px; }

    #layout { letter-spacing:0.5px; }

    #edit { background:#0000FF; }

    #edit p {text-indent:2px; letter-spacing:1px; color:#ffffff; }

    #edit p a { text-decoration:none; color:#ffffff; }

    </style>

    </head>

    <body>

    <form id="form1" name="form1" method="post" action="">

    <?php

    include("conn.php"); //包含conn.php文件

    $select=mysql_query("select * from tb_book"); //查询数据

    while($row=mysql_fetch_array($select)){ //循环输出查询内容

    ?>

    <p><?php echo $row['id'];?></p> <!--输出id-->

    <div id="layout">

    <p><?php echo $row['name'];?></p> <!--输出书名-->

    </div>

    <div id="edit">

    <p><?php echo $row['pen'];?></p> <!--输出作者-->

    </div>

    <div id="layout">

    <p><?php echo $row['pub'];?></p><!--输出出版社-->

    </div>

    <div id="edit">

    <p><?php echo $row['date'];?></p> <!--输出出版时间-->

    </div>

    <div id="layout">

    <p><?php echo $row['rate'];?></p> <!--输出价格-->

    </div>

    <div id="edit">

    <p><a href="update.php?id=<?php echo $row['id'];?>">修改</a></p>

    </div>

    <p><input type="hidden" name="id" value="<?php echo $row['id'];?>"/></p>

    <?php

    }

    ?>

    </form>

    </body>

    </html>

    (3)创建update.php文件,设计编辑图书信息页面,代码如下:

    <!DOCTYPE>

    <html>

    <head>

    <meta charset="utf-8" />

    <title></title>

    <style type="text/css">

    * { margin:0; padding:0; }

    body { font-family:Verdana, Arial, Helvetica, sans-serif; font-size:16px; width:1010px; margin:0 auto; }

    p { padding:10px 20px; line-height:160%; }

    label{ display:block; width:130px; height:35px; line-height:35px; float:left; text-align:right; padding-right:10px; }

    label[for="name"] { letter-spacing:11px; }

    label[for="pen"] { letter-spacing:11px; }

    label[for="pub"] { letter-spacing:4px; }

    label[for="rate"] { letter-spacing:11px; }

    input { width:220px; height:35px; line-height:35px; border:1px solid#808080; }

    input[type="text"]:focus { background:#dde3f9; }

    input[type="submit"] { width:107px; }

    .but { padding:5px 70px; }

    .but p { float:left; padding:0px 2px; }

    </style>

    </head>

    <body>

    <?php

    include("conn.php"); //包含conn.php文件

    $select=mysql_query("select * from tb_book where id='".$_GET['id']."'"); //定义查询语句

    $row=mysql_fetch_array($select); //循环输出查询内容

    ?>

    <form id="form1" name="form1" method="post" action="update_ok.php">

    <p>

    <label for="name">书名:</label>

    <input id="name" name="name" type="text" size="40" value="<?php echo $row['name'];?>"/>

    </p>

    <p>

    <label for="pen">作者:</label>

    <input id="pen" name="pen" type="text" size="40" value="<?php echo $row['pen'];?>"/>

    </p>

    <p>

    <label for="pub">出版社:</label>

    <input id="pub" name="pub" type="text" size="40" value="<?php echo $row['pub'];?>"/></p>

    <p>

    <label for="date">出版时间:</label>

    <input type="text" id="date" name="date" value="<?php echo $row['date'];?>"/>

    </p>

    <p>

    <label for="rate">价格:</label>

    <input type="text" id="rate" name="rate" value="<?php echo $row['rate'];?>"/>

    </p>

    <div class="but">

    <p><input type="submit" name="Submit" value="提交"/></p>

    <p><input type="submit" name="Submit2" value="取消"/></p>

    </div>

    <input type="hidden" name="id" value="<?php echo $row['id'];?>"/>

    </form>

    </body>

    </html>

    (4)创建update_ok.php文件,完成图书信息修改操作,代码如下

    <!DOCTYPE>

    <html>

    <head>

    <meta charset="utf-8" />

    <title></title>

    </head>

    <body>

    <?php

    include("conn.php");

    if(isset($_POST['Submit']) and $_POST['Submit']=="提交" and $_POST['name']!="" and $_POST['pen']!="" and $_POST['pub']!=""){

    $update=mysql_query("update tb_book set name='".$_POST['name']."',pen='".$_POST['pen']."',pub='".$_POST['pub']."',date='".$_POST['date']."',rate='".$_POST['rate']."' where id='".$_POST['id']."'",$conn);

    if($update){

    echo "<script>alert('修改成功!');window.location.href='index.php'</script>";

    }else{

    echo "<script>alert('修改失败!');window.location.href='index.php'</script>";

    }

    }else{

    echo "<script>alert('信息不能为空!');window.location.href='index.php'</script>";

    }

    ?>

    </body>

    </html>

    http://www.xn--ttss6e.net http://www.xn--ttss6e.net