您好,登錄后才能下訂單哦!
本篇內容介紹了“php數據訪問的增刪改查操作”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
一、查看新聞頁面-----主頁面
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>查看新聞</title> </head> <body> <h2>查看新聞</h2> <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>id</td> <td>title</td> <td>author</td> <td>source</td> <td>content</td> <td>date</td> <td>update</td> <td>delete</td> </tr> <?php $db=new MySQLi("localhost","root","","mydb"); !mysqli_connect_error() or die("連接失敗!"); $sql="select * from news"; $result=$db->query($sql); $arr=$result->fetch_all(); foreach ($arr as $v) { echo "<tr> <td>{$v[0]}</td> <td>{$v[1]}</td> <td>{$v[2]}</td> <td>{$v[3]}</td> <td>{$v[4]}</td> <td>{$v[5]}</td> <td><a href='Update.php?newsid={$v[0]}'>update</a></td> <td><a href='Delete.php?newsid={$v[0]}'>delete</a></td> </tr>"; } ?> </table> <br> <br /> <div class="xw"><a href="xinwen.php">發布新聞</a></div> </body> </html>
二、發布新聞頁面-----添加內容
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>發布新聞</title> <style> .xw { margin-top:10px; margin-left:400px; border:thick; } .a { float:left; } </style> </head> <body> <h2><center>發布新聞</center></h2> <form action="AddChu.php" method="post"> <div class="xw">標題:<input type="text" name="title" ></div> <div class="xw">作者:<input type="text" name="author"></div> <div class="xw">來源:<input type="text" name="source"></div> <div class="xw">內容: <textarea rows="10" cols="80" name="content"></textarea></div> <div class="a"><input type="submit" value="提交" ></div> <div class="a"><a href="ChaKan.php"><input type="button" value="查看" ></a></div> </form> </body> </html>
提交內容后的處理:
<?php $newsid=$_POST["newsid"]; $title=$_POST["title"]; $author=$_POST["author"]; $source=$_POST["source"]; $content=$_POST["content"]; $time=date("Y-m-d",time()); $db=new MySQLi("localhost","root","","mydb"); !mysqli_connect_error() or die("聯系失敗!"); $sql="insert into news values('{$newsid}','{$title}','{$author}','{$source}','{$content}','{$time}')"; $result=$db->query($sql); if($result) { header ("location:xinwen.php"); } else { echo "添加新聞失敗!"; }
三、刪除內容處理
<?php $newsid=$_GET["newsid"]; $db=new MySQLi("localhost","root","","mydb"); !mysqli_connect_error() or die("連接失敗!"); $sql="delete from news where newsid='{$newsid}'"; $result=$db->query($sql); if($result) { header ("location:ChaKan.php"); } else { echo "刪除數據失敗"; } ?>
四、修改新聞頁面----修改新聞內容后提交查看
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>修改新聞</title> <style> .xw { margin-top:10px; margin-left:400px; border:thick; } .a { float:left; } </style> </head> <body> <h2><center>修改新聞</center></h2> <?php $newsid = $_GET["newsid"]; $db = new MySQLi("localhost","root","","mydb"); $sinfo = "select * from news where newsid='{$newsid}'"; $r = $db->query($sinfo); $arr = $r->fetch_row(); //這個人的所有信息 ?> <form action="UpdateChu.php" method="post"> <div class="xw"><input type="hidden" name="newsid" value="<?php echo $arr[0] ?>"></div> <div class="xw">標題:<input type="text" name="title" value="<?php echo $arr[1] ?>"></div> <div class="xw">作者:<input type="text" name="author" value="<?php echo $arr[2] ?>"></div> <div class="xw">來源:<input type="text" name="source" value="<?php echo $arr[3] ?>"></div> <div class="xw">內容: <textarea rows="10" cols="80" name="content"><?php echo $arr[4] ?></textarea></div> <div class="a"><input type="submit" value="修改" ></div> <div class="a"><a href="ChaKan.php"><input type="button" value="查看" ></a></div> </form> </body> </html>
提交修改內容后進行處理:
<?php $newsid=$_POST["newsid"]; $title=$_POST["title"]; $author=$_POST["author"]; $source=$_POST["source"]; $content=$_POST["content"]; $time=date("Y-m-d",time()); $db=new MySQLi("localhost","root","","mydb"); !mysqli_connect_error() or die("聯系失敗!"); $sql="update news set title='{$title}',author='{$author}',source='{$source}',content='{$content}',time='{$time}' where newsid='{$newsid}'"; $result=$db->query($sql); if($result) { header ("location:Update.php"); } else { echo "修改數據失敗!"; }
“php數據訪問的增刪改查操作”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。