Home  >  Article  >  Backend Development  >  The addition, deletion, modification and query functions implemented by mysql in php

The addition, deletion, modification and query functions implemented by mysql in php

墨辰丷
墨辰丷Original
2018-06-08 17:17:381793browse

This article mainly introduces the addition, deletion, modification and query functions implemented by mysql in php. Interested friends can refer to it. I hope it will be helpful to everyone.

List code

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

?>

Delete

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

Add

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

?>

Modify

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

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

How to use and introduce the PHP Closure class

PHP Mysql jQuery statistics are currently online Number of users

Usage of switch statement in PHP

The above is the detailed content of The addition, deletion, modification and query functions implemented by mysql in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn