首页 >后端开发 >php教程 >php中mysql实现的增删改查功能

php中mysql实现的增删改查功能

墨辰丷
墨辰丷原创
2018-06-08 17:17:381826浏览

本篇文章主要介绍php中mysql实现的增删改查功能,感兴趣的朋友参考下,希望对大家有所帮助。

列表代码

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

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

  mysql_select_db("test", $con);

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

  echo "<table border=&#39;1&#39;>
  <tr>
  <th>Username</th>
  <th>Password</th>
  </tr>";

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

  mysql_close($con);

?>

删除

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

  mysql_select_db("test", $con);

  mysql_query("DELETE FROM user WHERE username = &#39;$_POST[username]&#39;");

  mysql_close($con);
?>

添加

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

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

  mysql_select_db("test", $con);

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

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

  echo "1 record added";

  mysql_close($con);

?>

修改

<?php

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

  mysql_select_db("test", $con);

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

  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 Closure类的使用方法及介绍

PHP+Mysql+jQuery统计当前在线用户数

PHP中的switch语句的用法

以上是php中mysql实现的增删改查功能的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn