php+mysql实现简单的增删改查功能

5年以前  |  阅读数:870 次  |  编程语言:PHP 

列表代码


    <?php
      $con = mysql_connect("localhost:3306","root","");

      if (!$con) {
        die('Could not connect: ' . mysql_error());
      }

      mysql_select_db("test", $con);

      $result = mysql_query("SELECT * FROM user");

      echo "<table border='1'>
      <tr>
      <th>Username</th>
      <th>Password</th>
      </tr>";

      while($row = mysql_fetch_array($result)) {
        echo "<tr>";
        echo "<td>" . $row['username'] . "</td>";
        echo "<td>" . $row['password'] . "</td>";
        echo "</tr>";
      }
      echo "</table>";

      mysql_close($con);

    ?>

删除


    <?php
      $con = mysql_connect("localhost","root","");
      if (!$con) {
        die('Could not connect: ' . mysql_error());
      }

      mysql_select_db("test", $con);

      mysql_query("DELETE FROM user WHERE username = '$_POST[username]'");

      mysql_close($con);
    ?>

添加


    <?php
      $con = mysql_connect("localhost:3306","root","");

      if (!$con) {
        die('Could not connect: ' . mysql_error());
      }

      mysql_select_db("test", $con);

      $sql = "INSERT INTO user (username,password)
      VALUES
      ('$_POST[username]','$_POST[password]')";

      if (!mysql_query($sql,$con)) {
        die('Error: ' . mysql_error());
      }

      echo "1 record added";

      mysql_close($con);

    ?>

修改


    <?php

      $con = mysql_connect("localhost","root","");
      if (!$con) {
        die('Could not connect: ' . mysql_error());
      }

      mysql_select_db("test", $con);

      mysql_query("UPDATE user SET password = '$_POST[password]' WHERE username = '$_POST[username]'");

      mysql_close($con);
    ?>


    <html>
      <head>
        <title>用PHP向数据库中实现简单的增删改查</title>
      </head>
      <body>
        <br />
        <h1>Insert:</h1>
        <form action="insert.php" method="post">
          username:<input type="name" name="username"/>
          <br />
          password:<input type="password" name="password"/>
          <input type="submit" value="submit"/>
        </form>
        <br /><hr /><br />
        <h1>Delete</h1>
        <form action="delete.php" method="post">
          username:<input type="name" name="username" />
          <br />
          Are you sure?<input type="submit" value="sure" />
        </form>
        <br /><hr /><br />
        <h1>Update</h1>
        <form action="update.php" method="post">
          username:<input type="name" name="username"/>
          <br />
          You want to change your password into:<input type="password" name="password"/>
          <input type="submit" value="submit"/>
        </form>
        <br /><hr /><br />
      </body>
    </html>

以上所述就是本文的全部内容了,希望大家能够喜欢。

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例