PHP開發(fā)簡單圖書后臺管理系統(tǒng)新書管理修改刪除功能
本節(jié)講解“新書管理”頁面選擇點(diǎn)擊操作功能里面的“修改”,“刪除”功能的實(shí)現(xiàn)
先說“刪除”功能。
主要的思路是:
獲取要刪除的書籍的id
通過SQL語句刪除此書的id來刪除此id在數(shù)據(jù)庫中的的全部記錄。
<?php $SQL ="DELETE FROM yx_books where id='".$_GET['id']."'"; $arry=mysqli_query($link,$sql); if($arry){ echo "<script> alert('刪除成功');location='list.php';</script>"; } else echo "刪除失敗"; ?>
然后是“修改”功能。
獲取需要修改書籍的id
通過SQL語句查詢數(shù)據(jù)庫中此條id的所有信息。再通過SQL語句修改此條id的信息
<?php $SQL = "SELECT * FROM yx_books where id='".$_GET['id']."'"; $arr=mysqli_query($link,$sql); $rows=mysqli_fetch_row($arr); ?> <?php if($_POST['action']=="modify"){ $sqlstr = "UPDATE yx_books SET name = '".$_POST['name']."', price = '".$_POST['price']."', uploadtime = '".$_POST['uptime']."', type = '".$_POST['type']."', total = '".$_POST['total']."' where id='".$_GET['id']."'"; $arry=mysqli_query($link,$sqlstr); if ($arry){ echo "<script> alert('修改成功');location='list.php';</script>"; } else{ echo "<script>alert('修改失敗');history.go(-1);</script>"; } } ?>
給<from>表單一個(gè)onSubmit點(diǎn)擊事件:
<form id="myform" name="myform" method="post" action="" onSubmit="return myform_Validator(this)">
通過onSubmit點(diǎn)擊事件用<javascript>判斷修改書籍信息時(shí)不能讓每項(xiàng)修改的信息為空。
<script type="text/javascript"> function myform_Validator(theForm) { if (theForm.name.value == "") { alert("請輸入書名。"); theForm.name.focus(); return (false); } if (theForm.price.value == "") { alert("請輸入書名價(jià)格。"); theForm.price.focus(); return (false); } if (theForm.type.value == "") { alert("請輸入書名所屬類別。"); theForm.type.focus(); return (false); } return (true); } </script>