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

yipeiwu_com5年前Mysql基础

列表代码

<?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>

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

相关文章

How do I change MySQL timezone?

However, there are ways for you to get results t...

php中处理mysql_fetch_assoc返回来的数组 不用foreach----echo

复制代码 代码如下: $id = intval($_GET['id']); $row = $db->getResult($db->query("select * from "...

Windows下IIS6/Apache2.2.4+MySQL5.2+PHP5.2.1安装配置方法

03年的时候就看过一本php的书,那时还是php3,回首四年php的在web开的的前景真还是一片光明啊!三年不见的php在去看已到了php5了,现在算起我真正从写第一个php的WEB开始...

php操作mysql获取select 结果的几种方法

如果用了 MYSQL_BOTH,将得到一个同时包含关联和数字索引的数组。 用 MYSQL_ASSOC 只得到关联索引(如同mysql_fetch_assoc() 那样), 用 MYSQL...

php与XML、XSLT、Mysql的结合运用实现代码

使用php中的xml与xslt要用到一些dll库, extension=php_domxml.dll //操作XML的函数库 extension=php_iconv.dll //转码用的...