如何實(shí)現(xiàn)數(shù)據(jù)修改功能
上一節(jié)我們建立了修改頁(yè)面edit.php?,這一節(jié)我們就來實(shí)作修改功能
id ?透過SQL語句查詢資料庫(kù)中此條id的所有資訊。再透過SQL語句修改此條id的資訊
建立?update.php 檔實(shí)作修改功能。
<?php $id = isset($_GET["id"])?$_GET["id"]:""; $title = isset($_POST['title'])?$_POST['title']:""; $name = isset($_POST['name'])?$_POST['name']:""; $video = isset($_POST['video'])?$_POST['video']:""; $sql = "select id,title,name,video from list where id = '$id'"; $result = mysqli_query($link,$sql); $rel = mysqli_fetch_array($result); ?>在html程式碼中顯示:這裡要用到隱藏域?
type="hidden " 取得id。
<form method="post" class="form-x" action="update.php" enctype="multipart/form-data"> <input type="hidden" name="id" value="<?php echo $rel["id"]?>"> </form>對(duì)一下的標(biāo)題,影片內(nèi)容名稱,描述做如下的修改,css樣式可以自行依需求調(diào)整:
<div class="form-group"> <div class="label"> <label>標(biāo)題:</label> </div> <div class="field"> <input type="text" class="input w50" value="<?php echo $rel["title"]?>" name="title" data-validate="required:請(qǐng)輸入標(biāo)題" /> <div class="tips"></div> </div> </div>
<div class="form-group"> <div class="label"> <label>視頻:</label> </div> <div class="field"> <input type="text" class="input w50" value="<?php echo $rel["video"]?>" name="video" data-validate="required:請(qǐng)輸入視頻名稱" /> <input type="submit" name="upload" class="button bg-blue margin-left" id="image1" value="+ 瀏覽上傳" style="float:left;"> <div class="tips"></div> </div> </div>
<div class="form-group"> <div class="label"> <label>描述:</label> </div> <div class="field"> <textarea class="input" name="name" style=" width:400px;height:200px;"><?php echo $rel["name"]?></textarea> <div class="tips"></div> </div> </div>當(dāng)然在?list.php中做如下的修改,$rows["id"]跟刪除功能一樣while 迴圈輸出。
<div class="button-group"> <a class="button border-main" href="edit.php?id=<?php echo $rows["id"]?>"><span class="icon-edit"></span>修 改</a> <a class="button border-red" href="delete.php?id=<?php echo $rows["id"]?>" onclick="return del(1,1,1)"> <span class="icon-trash-o"></span>刪 除 </a> </div>這樣就可以在?update.php?檔案實(shí)作修改功能程式碼,透過SQL語句修改資料庫(kù)中此條id的資訊。
<?php header("content-type:text/html;charset=utf-8"); include("config.php"); //引入數(shù)據(jù)庫(kù)公共文件 $id = isset($_POST["id"])?$_POST["id"]:""; $title = isset($_POST['title'])?$_POST['title']:""; $name = isset($_POST['name'])?$_POST['name']:""; $video = isset($_POST['video'])?$_POST['video']:""; $sql="update list set title='$title',name='$name',video='$video' where id='$id'"; //echo $sql; $rel=mysqli_query($link,$sql);//執(zhí)行sql語句 //echo $rel if($rel){ echo "<script>alert('修改成功');window.location.href='list.php'</script>"; }else{ echo "<script>alert('修改失敗');window.location.href='edit.php'</script>"; } ?>
#