欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-07 20:35:34
列表代码

列表代码

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

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