Home > Backend Development > PHP Tutorial > Use PHP to implement simple additions, deletions, modifications and queries in the database (pure code, to be improved)

Use PHP to implement simple additions, deletions, modifications and queries in the database (pure code, to be improved)

WBOY
Release: 2016-08-08 09:21:07
Original
1304 people have browsed it

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

    </span><span>if</span> (!<span>$con</span><span>) {
        </span><span>die</span>('Could not connect: ' . <span>mysql_error</span><span>());
    }

    </span><span>mysql_select_db</span>("test", <span>$con</span><span>);

    </span><span>$result</span> = <span>mysql_query</span>("SELECT * FROM user"<span>);

    </span><span>echo</span> "<span><table border='1'>
    <tr>
    <th>Username</th>
    <th>Password</th>
    </tr>"<span>;

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

    </span><span>mysql_close</span>(<span>$con</span><span>);

</span>?>
Copy after login
Get all the user’s information (SQL SELECT statement) from the server and present it in table form

<?<span>php
    </span><span>$con</span> = <span>mysql_connect</span>("localhost","root",""<span>);
    </span><span>if</span> (!<span>$con</span><span>) {
        </span><span>die</span>('Could not connect: ' . <span>mysql_error</span><span>());
    }

    </span><span>mysql_select_db</span>("test", <span>$con</span><span>);

    </span><span>mysql_query</span>("DELETE FROM user WHERE username = '<span>$_POST</span>[username]'"<span>);

    </span><span>mysql_close</span>(<span>$con</span><span>);
</span>?>
Copy after login
Delete all the user’s information delete.php

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

    </span><span>if</span> (!<span>$con</span><span>) {
        </span><span>die</span>('Could not connect: ' . <span>mysql_error</span><span>());
    }

    </span><span>mysql_select_db</span>("test", <span>$con</span><span>);

    </span><span>$sql</span> = "<span>INSERT INTO user (username,password)
    VALUES
    ('</span><span>$_POST</span>[username]','<span>$_POST</span>[password]')"<span>;

    </span><span>if</span> (!<span>mysql_query</span>(<span>$sql</span>,<span>$con</span><span>)) {
        </span><span>die</span>('Error: ' . <span>mysql_error</span><span>());
    }

    </span><span>echo</span> "1 record added"<span>;

    </span><span>mysql_close</span>(<span>$con</span><span>);

</span>?>
Copy after login
Register a new user insert.php

<?<span>php

    </span><span>$con</span> = <span>mysql_connect</span>("localhost","root",""<span>);
    </span><span>if</span> (!<span>$con</span><span>) {
        </span><span>die</span>('Could not connect: ' . <span>mysql_error</span><span>());
    }

    </span><span>mysql_select_db</span>("test", <span>$con</span><span>);

    </span><span>mysql_query</span>("UPDATE user SET password = '<span>$_POST</span>[password]' WHERE username = '<span>$_POST</span>[username]'"<span>);

    </span><span>mysql_close</span>(<span>$con</span><span>);
</span>?>
Copy after login
Change a user password update.php

<html>
    <head>
        <title>用PHP向数据库中实现简单的增删改查</title>
    </head>
    <body>
        <br />
        <h1>Insert:</h1>
        <form action="insert.php" method="post"><span>            username</span>:<input type="name" name="username"/>
            <br /><span>            password</span>:<input type="password" name="password"/>
            <input type="submit" value="submit"/>
        </form>
        <br /><hr /><br />
        <h1>Delete</h1>
        <form action="delete.php" method="post"><span>            username</span>:<input type="name" name="username" />
            <br /><span>            Are you sure</span>?<input type="submit" value="sure" />
        </form>
        <br /><hr /><br />
        <h1>Update</h1>
        <form action="update.php" method="post"><span>            username</span>:<input type="name" name="username"/>
            <br /><span>            You want to change your password into</span>:<input type="password" name="password"/>
            <input type="submit" value="submit"/>
        </form>
        <br /><hr /><br />
    </body>
</html>
Copy after login
Submission source for the above three functions Operate.html

The above introduces the use of PHP to implement simple additions, deletions, modifications and queries in the database (pure code, to be improved), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template