首頁 >後端開發 >php教程 >php中mysql實現的增刪改查功能

php中mysql實現的增刪改查功能

墨辰丷
墨辰丷原創
2018-06-08 17:17:381824瀏覽

本篇文章主要介紹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