您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)如何使用PHP連接數(shù)據(jù)庫(kù)并實(shí)現(xiàn)用戶數(shù)據(jù)的增刪改查的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
main頁(yè)面(主頁(yè)面)
<table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>代號(hào)</td> <td>姓名</td> <td>性別</td> <td>民族</td> <td>生日</td> <td>操作</td> </tr> <?php $db = new MySQLi("localhost","root","","mydb"); if(mysqli_connect_error()){ die("連接失敗"); } $sql = "select * from Info"; $result = $db->query($sql); $attr = $result->fetch_all(); foreach($attr as $v) { $sex = $v[2]? '男':'女'; //三元運(yùn)算符判斷性別 $sql = "select Name from Nation where Code ='$v[3]'"; $result = $db ->query($sql); $attr = $result->fetch_assoc(); echo "<tr> <td>{$v[0]}</td> <td>{$v[1]}</td> <td>{$sex}</td> <td>{$attr['Name']}</td> <td>{$v[4]}</td> <td> <a href='Delete.php?code={$v[0]}'>刪除</a> <a href='Update.php?code={$v[0]}'>修改</a> </td> </tr>"; } ?> </table> <div> <a href="Add.php" rel="external nofollow" >添加數(shù)據(jù)</a> </div>
Add(添加數(shù)據(jù)頁(yè)面)
<h2>添加數(shù)據(jù)</h2> <form action="AddChuLi.php" method="post"> <div>代號(hào):<input type="text" name="code" /></div> <div>姓名:<input type="text" name="name" /></div> <div>性別: <input type="radio" value="男" name="sex" />男 <input type="radio" value="女" name="sex" />女 </div> <div>民族: <select name="nation"> <?php $db = new MySQLi("localhost","root","","mydb"); if(mysqli_connect_error()){ die("連接失敗"); } $sql = "select * from Nation"; $r = $db->query($sql); $att = $r->fetch_all(); foreach($att as $v) { echo "<option value='{$v[0]}'>{$v[1]}</option>"; } ?> </select> </div> <div>生日:<input type="text" name="birthday" /></div> <div><input type="submit" value="添加數(shù)據(jù)" /></div> </form>
AddChuLi頁(yè)面(添加數(shù)據(jù)處理頁(yè)面)
<?php $code = $_POST["code"]; $name = $_POST["name"]; $sex = $_POST["sex"]; $s = 1; if($sex =="女") { $s=0; } $nation = $_POST["nation"]; $birthday = $_POST["birthday"]; $db = new MySQLi("localhost","root","","mydb"); if(mysqli_connect_error()){ die("連接失敗"); } $sql = "insert into Info values('{$code}','{$name}','{$s}','{$nation}','{$birthday}')"; //添加數(shù)據(jù)語(yǔ)句 $result = $db->query($sql); if($result) { header("location:main.php"); //php跳轉(zhuǎn)頁(yè)面方式 } else { echo "添加失?。?quot;; }
DeleteChuLi頁(yè)面(刪除數(shù)據(jù)處理頁(yè)面)
<?php $code = $_GET["code"]; $db = new MySQLi("localhost","root","","mydb"); if(mysqli_connect_error()){ die("連接失敗"); } $sql = "delete from Info where Code ='{$code}'"; //刪除語(yǔ)句 $r = $db->query($sql); if($r) { header("location:main.php"); } else { echo "刪除失?。?quot;; }
Update頁(yè)面(修改數(shù)據(jù)頁(yè)面)
<h2>修改數(shù)據(jù)</h2> <?php $code = $_GET["code"]; $db = new MySQLi("localhost","root","","mydb"); if(mysqli_connect_error()){ die("連接失敗"); } $sql1 = "select * from Info where Code='{$code}'"; $r1 = $db->query($sql1); $att1 = $r1->fetch_row(); ?> <form action="UpdateChuLi.php" method="post"> <div>代號(hào):<input type="hidden" name="code" value="<?php echo $att1[0] ?>" /></div> <div>姓名:<input type="text" name="name" value="<?php echo $att1[1] ?>" /></div> <div>性別: <input type="radio" value="男" name="sex" <?php echo $att1[2] ? "checked='checked'" : ""; ?> />男 <input type="radio" value="女" name="sex" <?php echo $att1[2] ? "" : "checked='checked'"; ?> />女 </div> <div>民族: <select name="nation"> <?php $db = new MySQLi("localhost","root","","mydb"); if(mysqli_connect_error()){ die("連接失敗"); } $sql = "select * from Nation"; //查詢nation一組數(shù)據(jù) $r = $db->query($sql); $att = $r->fetch_all(); foreach($att as $v) { if($att1[3]==$v[0]) { echo "<option value='{$v[0]}' selected='selectec' >{$v[1]}</option>"; } else { echo "<option value='{$v[0]}'>{$v[1]}</option>"; } } ?> </select> </div> <div>生日:<input type="text" name="birthday" value="<?php echo $att1[4] ?>"/></div> <div><input type="submit" value="修改數(shù)據(jù)" /></div> </form>
ateChuLi頁(yè)面(修改數(shù)據(jù)處理頁(yè)面)
<?php $code = $_POST["code"]; $name = $_POST["name"]; $sex = $_POST["sex"]; $s=1; if($sex=="女") { $s=0; } $nation = $_POST["nation"]; $birthday = $_POST["birthday"]; $db = new MySQLi("localhost","root","","mydb"); if(mysqli_connect_error()){ die("連接失敗"); } $sql = "update Info set Name='{$name}',Sex={$s},Nation='{$nation}',Birthday='{$birthday}' where Code='{$code}'"; //修改數(shù)據(jù)語(yǔ)句 $r = $db->query($sql); if($r) { header("location:main.php"); } else { echo "修改失?。?quot;; }
感謝各位的閱讀!關(guān)于“如何使用PHP連接數(shù)據(jù)庫(kù)并實(shí)現(xiàn)用戶數(shù)據(jù)的增刪改查”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。