PHP開(kāi)發(fā)簡(jiǎn)單圖書(shū)后臺(tái)管理系統(tǒng)新書(shū)添加功能
本節(jié)來(lái)實(shí)現(xiàn)圖書(shū)后臺(tái)管理系統(tǒng)新書(shū)添加功能
基本思路是在<form>表單中添加數(shù)據(jù)
點(diǎn)擊提交按鍵后將添加的數(shù)據(jù)通過(guò)SQL語(yǔ)句INSERT?INTO增加到數(shù)據(jù)庫(kù)中
使用給提交按鍵一個(gè)value值insert。
<td align="right" class="td_bg"> <input type="hidden" name="action" value="insert"> <input type="submit" name="button" id="button" value="提交" /> </td>
使用$_POST方式獲取值。使用SQL語(yǔ)句INSERT?INTO將新書(shū)的信息增加到數(shù)據(jù)庫(kù)中。
<?php if($_POST['action']=="insert"){ $SQL = "INSERT INTO yx_books (name,price,uploadtime,type,total,leave_number) values('".$_POST['name']."','".$_POST['price']."','".$_POST['uptime']."','".$_POST['type']."','".$_POST['total']."','".$_POST['total']."')"; $arr=mysqli_query($link,$sql); if ($arr){ echo "<script language=javascript>alert('添加成功!');window.location='add.php'</script>"; } else{ echo "<script>alert('添加失敗');history.go(-1);</script>"; } } ?>
當(dāng)然我們要給<from>表單一個(gè)onSubmit點(diǎn)擊事件:
<form id="myform" name="myform" method="post" action="" onsubmit="return myform_Validator(this)">
通過(guò)onSubmit點(diǎn)擊事件用<javascript>判斷增加書(shū)籍信息時(shí)不能讓每項(xiàng)添加的信息為空。
<script type="text/javascript"> function myform_Validator(theForm) { if (theForm.name.value == "") { alert("請(qǐng)輸入書(shū)名。"); theForm.name.focus(); return (false); } if (theForm.price.value == "") { alert("請(qǐng)輸入書(shū)名價(jià)格。"); theForm.price.focus(); return (false); } if (theForm.type.value == "") { alert("請(qǐng)輸入書(shū)名所屬類別。"); theForm.type.focus(); return (false); } return (true); } </script>