您好,登錄后才能下訂單哦!
<title>index.php</title> <?php #連接數據庫 $conn = mysqli_connect("localhost","root",""); #判斷是否連接成功 if(!$conn){ echo "失敗"; } //選擇數據庫 mysqli_select_db($conn,"bbs"); //準備sql語句 $sql = "select * from bbs_user"; //發送sql語句 $obj = mysqli_query($conn,$sql); echo "<center>"; echo "<table border = 1 cellspacing = '0' cellpadding = '10'>"; echo "<th>編號</th><th>姓名</th><th>密碼</th><th>地址</th><th>性別</th><th>年齡</th><th>操作</th>"; while($row = mysqli_fetch_assoc($obj)){ echo "<tr>"; echo '<td>'.$row['id'].'</td>'; echo '<td>'.$row['username'].'</td>'; echo '<td>'.$row['password'].'</td>'; echo '<td>'.$row['address'].'</td>'; echo '<td>'.$row['sex'].'</td>'; echo '<td>'.$row['age'].'</td>'; echo '<td><a href = "del.php?id='.$row['id'].'">刪除</a>/<a href = "update.php?id='.$row['id'].'">修改</a></td>'; echo "</tr>"; } echo "</table>"; echo "<a href = 'add.php'>添加</a>"; echo "<center>"; //關閉連接 mysqli_close($conn); ?>
點擊刪除:
<title>del.php</title> <?php $id =$_GET['id']; $link = mysqli_connect('localhost','root',''); if(!$link){ exit('連接失敗'); } mysqli_select_db($link,'bbs'); $sql = "delete from bbs_user where id =$id"; $result = mysqli_query($link,$sql); if($result && mysqli_affected_rows($link)){ echo "刪除成功<a href = 'index.php'>返回</a>"; }else{ echo "刪除失敗"; } mysqli_close($link); ?>
更改數據:
<?php $id = $_GET['id']; $link = mysqli_connect('localhost','root',''); if(!$link){ exit('連接失敗'); } mysqli_select_db($link,'bbs'); $sql = "select * from bbs_user where id =$id"; $obj = mysqli_query($link,$sql); $row = mysqli_fetch_assoc($obj); ?> <form action="doupdate.php?"> <!--用隱藏式來獲取id,其他的目前不行--> <input type="hidden" name="id" value = "<?php echo $id;?>" /> <table border = 0 cellpadding="10" cellspacing="0"> <tr><td>編號:</td><td><?php echo $row['id'];?></td></tr> <tr><td>用戶名:</td><td><input type="text" name = "username" value="<?php echo $row['username'];?>" /></td></tr> <!--<tr><td>密碼:</td><td><input type="text" name = "password" value="<?php echo $row['password'];?>" /></td></tr>--> <tr><td>密碼:</td><td><?php echo $row['password'];?></td></tr> <tr><td>地址:</td><td><input type="text" name="address" value="<?php echo $row['address'];?>" /></td></tr> <tr><td>性別:</td><td><input type="text" name="sex" value="<?php echo $row['sex'];?>" /></td></tr> <tr><td>年齡:</td><td><input type="text" name="age" value="<?php echo $row['age'];?>" /><br /> <tr><td colspan="2" align="center"><input type="submit" name="" value="提交" /></td></tr> </table> </form>
執行更改:
<?php $id = $_GET['id']; $username = $_GET['username']; #$password = $_GET['password']; $address = $_GET['address']; $sex = $_GET['sex']; $age = $_GET['age']; $link = mysqli_connect('localhost','root',''); if(!$link){ exit('連接失敗'); } mysqli_select_db($link,'bbs'); $sql = "update bbs_user set username = '$username', address = '$address', sex = '$sex', age = '$age' where id =$id"; $obj = mysqli_query($link,$sql); if($obj && mysqli_affected_rows($link)){ echo "修改成功<a href = 'index.php'>返回</a>"; }else{ echo "修改失敗"; } mysqli_close($link); ?>
添加數據:
<form action = 'doadd.php' method="get"> 編號:<input type="text" name="id" value=""/><br /> 姓名:<input type="text" name="username" value=""/><br /> 密碼:<input type="text" name="password" value=""/><br /> 地址:<input type="text" name="address" value=""/><br /> 性別:<input type="radio" name="sex" value = "男"/>男 <input type="radio" name="sex" value="女" />女<br /> 年齡:<input type="text" name="age" value=""/><br /> <input type="submit" value="提交" /> </form>
執行添加數據:
<?php //獲取添加的數據信息 $id = $_GET['id']; $username = $_GET['username']; $password = md5($_GET['password']);//加密 $address = $_GET['address']; $sex = $_GET['sex']; $age = $_GET['age']; //連接數據庫 $link = mysqli_connect('localhost' , 'root' , ''); //判斷是否連接成功 if(!$link){ exit('連接數據庫失敗'.mysqli_connect_error()); } //選擇數據庫 mysqli_select_db($link , 'bbs'); //準備sql語句 $sql = "insert into bbs_user(id,username,password,address,sex,age) values('$id','$username','$password','$address','$sex','$age')"; //發送sql語句 $obj = mysqli_query($link , $sql); if($obj){ echo "添加成功<a href = 'index.php'>返回</a>"; }else{ echo "添加失敗"; } mysqli_close($link); ?>
在基礎上加了個查詢功能:
跳轉到select.php中:
<form action="doselect.php" > 請輸入您要查詢的編號:<input type="text" name="id" /> <input type="submit" value="查詢" /> </form>
執行:
執行的代碼段是:
<?php $id = $_GET['id']; $conn = mysqli_connect('localhost' , 'root' , ''); if(!$conn){ exit('連接失敗'); } mysqli_select_db($conn,'bbs'); $sql = "select * from bbs_user where id=$id"; $result = mysqli_query($conn,$sql); echo "<center>"; echo "<table border = 1 cellspacing = '0' cellpadding = '10'>"; echo "<th>編號</th><th>姓名</th><th>密碼</th><th>地址</th><th>性別</th><th>年齡</th>"; while($row = mysqli_fetch_assoc($result)){ echo "<tr>"; echo '<td>'.$row['id'].'</td>'; echo '<td>'.$row['username'].'</td>'; echo '<td>'.$row['password'].'</td>'; echo '<td>'.$row['address'].'</td>'; echo '<td>'.$row['sex'].'</td>'; echo '<td>'.$row['age'].'</td>'; echo "</tr>"; } echo "</table>"; echo "<center>"; mysqli_close($conn); ?>
查到到結果如下圖:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。