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

  • 课程实例设计
  • php创建数据库

    <!doctype html>

    <html>

    <head>

    <meta charset="utf-8">

    <title>php创建数据库</title>

    </head>

    <body>

    <?php

    $servername = "localhost";

    $username = "username";

    $password = "password";

    // 创建连接

    $conn = new mysqli($servername, $username, $password);

    // 检测连接

    if ($conn->connect_error) {

    die("连接失败: " . $conn->connect_error);

    }

    // 创建数据库

    $sql = "CREATE DATABASE db_database08";

    if ($conn->query($sql) === TRUE) {

    echo "数据库创建成功";

    } else {

    echo "Error creating database08: " . $conn->error;

    }

    $conn->close();

    ?>

    </body>

    </html>

    php创建数据表

    <?php

    $servername = "localhost";

    $username = "username";

    $password = "password";

    $dbname = "db_database08";

    // 创建连接

    $conn = new mysqli($servername, $username, $password, $dbname);

    // 检测连接

    if ($conn->connect_error) {

    die("连接失败: " . $conn->connect_error);

    }

    // 使用 sql 创建数据表

    $sql = "CREATE TABLE `tb_book` (

    `id` int(4) NOT NULL,

    `name` varchar(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,

    `pen` varchar(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,

    `pub` varchar(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,

    `date` varchar(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,

    `rate` varchar(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL

    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

    ";

    if ($conn->query($sql) === TRUE) {

    echo "Table tb_book created successfully";

    } else {

    echo "创建数据表错误: " . $conn->error;

    }

    $conn->close();

    ?>

    PHP MySQL 插入数据

    <?php

    $servername = "localhost";

    $username = "username";

    $password = "password";

    $dbname = "db_database08";

    // 创建连接

    $conn = new mysqli($servername, $username, $password, $dbname);

    // 检测连接

    if ($conn->connect_error) {

    die("连接失败: " . $conn->connect_error);

    }

    $sql = "

    INSERT INTO `tb_book` (`id`, `name`, `pen`, `pub`, `date`, `rate`) VALUES

    (1, 'PHP从入门到精通', '潘凯华', '清华大学出版社', '2009-08-01', '69.0'),

    (2, 'PHP开发实战宝典', '潘凯华', '清华大学出版社', '2010-06-01', '79.0'),

    (3, 'PHP函数手册', '潘凯华', '人民邮电出版社', '2008-05-05', '95.0'),

    (4, 'PHP范例宝典', '潘凯华', '人民邮电出版社', '2007-09-01', '89.0');";

    if ($conn->query($sql) === TRUE) {

    echo "新记录插入成功";

    } else {

    echo "Error: " . $sql . "<br>" . $conn->error;

    }

    $conn->close();

    ?>

    php删除数据表

    <!doctype html>

    <html>

    <head>

    <meta charset="utf-8">

    <title>php删除数据表</title>

    </head>

    <body>

    <?php

    $dbhost = 'localhost'; // mysql服务器主机地址

    $dbuser = 'username'; // mysql用户名

    $dbpass = 'password'; // mysql用户名密码

    $conn = mysqli_connect($dbhost, $dbuser, $dbpass);

    if(! $conn )

    {

    die('连接失败: ' . mysqli_error($conn));

    }

    echo '连接成功<br />';

    $sql = "DROP TABLE `db_database08`.`tb_book`";

    mysqli_select_db( $conn, 'RUNOOB' );

    $retval = mysqli_query( $conn, $sql );

    if(! $retval )

    {

    die('数据表删除失败: ' . mysqli_error($conn));

    }

    echo "数据表删除成功\n";

    mysqli_close($conn);

    ?>

    </body>

    </html>

    php删除数据库

    <!doctype html>

    <html>

    <head>

    <meta charset="utf-8">

    <title>php删除数据库</title>

    </head>

    <body>

    <?php

    $dbhost = 'localhost'; // mysql服务器主机地址

    $dbuser = 'username'; // mysql用户名

    $dbpass = 'password'; // mysql用户名密码

    $conn = mysqli_connect($dbhost, $dbuser, $dbpass);

    if(! $conn )

    {

    die('连接失败: ' . mysqli_error($conn));

    }

    echo '连接成功<br />';

    $sql = 'DROP DATABASE `db_database08`';

    $retval = mysqli_query( $conn, $sql );

    if(! $retval )

    {

    die('删除数据库失败: ' . mysqli_error($conn));

    }

    echo "数据库 RUNOOB 删除成功\n";

    mysqli_close($conn);

    ?>

    </body>

    </html>

    具体步骤如下:

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

    <?php

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

    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