Rumah > pembangunan bahagian belakang > tutorial php > php中mysql实现的增删改查功能

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

墨辰丷
Lepaskan: 2023-03-30 22:10:02
asal
1837 orang telah melayarinya

本篇文章主要介绍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);

?>
Salin selepas log masuk

删除

<?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);
?>
Salin selepas log masuk

添加

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

?>
Salin selepas log masuk

修改

<?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);
?>
Salin selepas log masuk
<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>
Salin selepas log masuk

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

PHP Closure类的使用方法及介绍

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

PHP中的switch语句的用法

Atas ialah kandungan terperinci php中mysql实现的增删改查功能. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan