Rumah > pembangunan bahagian belakang > tutorial php > 用PHP向数据库中实现简单的增删改查(纯代码,待完善)

用PHP向数据库中实现简单的增删改查(纯代码,待完善)

WBOY
Lepaskan: 2016-08-08 09:21:07
asal
1282 orang telah melayarinya

<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>?></span>
Salin selepas log masuk
从服务器中获取用户所有信息(SQL SELECT语句)并以表格形式出现

<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>?>
Salin selepas log masuk
删除该用户所有信息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>?>
Salin selepas log masuk
注册一个新用户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>?>
Salin selepas log masuk
修改一个用户密码update.php

    
        <title>用PHP向数据库中实现简单的增删改查</title>
    
    
        <br>
        <h1>Insert:</h1>
        
Salin selepas log masuk
username:
password:



Delete

username:
Are you sure?



Update

username:
You want to change your password into:



以上三个功能的提交源Operate.html

以上就介绍了用PHP向数据库中实现简单的增删改查(纯代码,待完善),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Label berkaitan:
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
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan