php原生開發(fā)新聞?wù)局砑有侣?/h1>
在之前的一堂課我們完成了新聞清單的分頁功能,那麼從這堂課開始我們開始要完成新聞的增刪改查,現(xiàn)在查已經(jīng)完成了,接下來就是刪除,編輯以及新增!今天主要跟大家介紹如何實(shí)現(xiàn)添加!
同樣,我們找到添加的模板,根據(jù)我們創(chuàng)建的數(shù)據(jù)表裡的字段,選擇需要的部分~添加新聞我們使用form表單提交然後插入數(shù)據(jù)庫(kù)
<form method="post" class="form-x" action="" enctype="multipart/form-data">
<div class="form-group">
<div class="label">
<label>分類:</label>
</div>
<select name="category_id" style="padding:5px 15px; border:1px solid #ddd;">
<option value="">-請(qǐng)選擇-</option>
<?php
foreach( $arr_news_category as $val){
echo "<option value='{$val['id']}'>{$val['name']}</option>";
}
?>
</select>
</select>
</div>
<div class="form-group">
<div class="label">
<label>標(biāo)題:</label>
</div>
<div class="field">
<input type="text" class="input w50" value="" name="title" data-validate="required:請(qǐng)輸入標(biāo)題" />
<div class="tips"></div>
</div>
</div>
<div class="clear"></div>
<div class="form-group">
<div class="label">
<label>關(guān)鍵字:</label>
</div>
<div class="field">
<input type="text" class="input" name="tag" value="" />
</div>
</div>
<div class="form-group">
<div class="label">
<label>內(nèi)容:</label>
</div>
<div class="field">
<textarea name="content" class="input" id="content" style="height:450px; width: 98%; border:1px solid #ddd;"></textarea>
</div>
</div>
<div class="form-group">
<div class="label">
<label>作者:</label>
</div>
<div class="field">
<input type="text" class="input w50" name="author" value="" />
<div class="tips"></div>
</div>
</div>
<div class="form-group">
<div class="label">
<label>圖片:</label>
</div>
<div class="field">
<input type="file" id="url1" name="pic" class="input tips" style="width:25%; float:left;" value=""
data-toggle="hover" data-place="right" data-image="" />
<input type="button" class="button bg-blue margin-left" id="image1" value="+ 瀏覽上傳" style="float:left;">
<div class="tipss">圖片尺寸:500*500</div>
</div>
</div>
<div class="form-group">
<div class="label">
<label>發(fā)布時(shí)間:</label>
</div>
<div class="field">
<script src="../js/laydate/laydate.js"></script>
<input type="text" class="laydate-icon input w50" name="created_at"
onclick="laydate({istime: true, format: 'YYYY-MM-DD hh:mm:ss'})" value=""
data-validate="required:日期不能為空" style="padding:10px!important;
height:auto!important;border:1px solid #ddd!important;" />
<div class="tips"></div>
</div>
</div>
<div class="form-group">
<div class="label">
<label></label>
</div>
<div class="field">
<button class="button bg-main icon-check-square-o" type="submit"> 提交</button>
</div>
</div>
</form>
form表單裡我們用post傳輸資料method="post" ?
action="" 這個(gè)是提交哪裡我這裡是提交到本頁面,但是推薦大家另外建立一個(gè)處理頁面
enctype="multipart/ form-data" 這個(gè)是上傳圖片不可或缺的!
內(nèi)容這裡我們使用的編輯器,很簡(jiǎn)單的,大家在網(wǎng)路上下載一個(gè),放到你的專案裡面,然後呼叫使用<textarea></textarea>
<div class="form-group">
<div class="label">
<label>內(nèi)容:</label>
</div>
<div class="field">
<textarea name="content" class="input" id="content" style="height:450px; width: 98%; border:1px solid #ddd;"></textarea>
</div>
</div>
呼叫編輯器重要的就是<script>程式碼如下:
<script type="text/javascript">
//實(shí)例化編輯器
//建議使用工廠方法getEditor創(chuàng)建和引用編輯器實(shí)例,如果在某個(gè)閉包下引用該編輯器,直接調(diào)用UE.getEditor('editor')就能拿到相關(guān)的實(shí)例
UE.getEditor('content',{initialFrameWidth:1500,initialFrameHeight:400,});
</script>
form表單設(shè)定好,我們就開始寫php處理頁面
第一步:連接資料庫(kù)
<?php
// 連接mysql數(shù)據(jù)庫(kù)
$link = mysqli_connect('localhost', 'root', 'root');
if (!$link) {
echo "connect mysql error!";
exit();
}
// 選中數(shù)據(jù)庫(kù) news為數(shù)據(jù)庫(kù)的名字
$db_selected = mysqli_select_db($link, 'news');
if (!$db_selected) {
echo "<br>selected db error!";
exit();
}
// 設(shè)置mysql字符集 為 utf8
$link->query("set names utf8");
?>
因?yàn)槲覀冞@裡要上傳圖片。所以我對(duì)圖片上傳,保存做處理:
<?php
if(count($_FILES['pic']) > 0){
// 檢查文件類型
if( !in_array($_FILES['pic']['type'], array('image/jpeg','image/png', 'image/gif')) ){
echo "只運(yùn)行上傳jpg或png圖片, 文件類型不合法,不允許上傳";
}
// 檢查文件大小
if ($_FILES['pic']['size'] > 5*1024*1024){
echo "文件最大尺寸為5M,不允許上傳.";
}
$file_ext= pathinfo($_FILES['pic']['name'], PATHINFO_EXTENSION); // 獲取文件后綴名
$tmp_file = $_FILES['pic']['tmp_name']; // 臨時(shí)文件
$dest_file = pathinfo($tmp_file, PATHINFO_FILENAME).".".$file_ext; // 保存的文件名
//move_uploaded_file($tmp_file, "d:/wamp/www/upload/".$dest_file); // 使用絕對(duì)地址保存圖片
move_uploaded_file($tmp_file, "../../upload/".$dest_file); // 使用絕對(duì)路徑地址保存圖片
$avatar_path ="../../upload/".$dest_file; // 注意,保存的時(shí)候,設(shè)置從服務(wù)器的根目錄開始
}
?>
圖片處理完以後我們要開始講form表單傳輸過來資料插入資料庫(kù):
<?php
if ($_POST['created_at']){
$current_time = $_POST['created_at'];
}else{
$current_time = date("Y-m-d H:i:s");
}
$sql = "insert into new(category_id,title,content,tag,author,pic,created_at)
VALUES ('{$_POST['category_id']}',
'{$_POST['title']}',
'{$_POST['content']}',
'{$_POST['tag']}',
'{$_POST['author']}',
'{$avatar_path}',
'$current_time'
)";
$result = mysqli_query($link,$sql);
if($result){
$url = "http://127.0.0.1/news/Admin/new/new_list.php";
header("Location: $url");
exit;
}else{
echo "添加新聞失敗!";
echo mysqli_error($link);
exit;
}
}
?>
這裡我們還有一個(gè)分類要選擇,所以我們還需要查詢分類表:
$sql = "select * from new_category ";
$result = mysqli_query($link, $sql);
$arr_news_category = mysqli_fetch_all($result, MYSQL_ASSOC);
然後在分類這個(gè)input框一欄循環(huán)出分類
<div class="form-group">
<div class="label">
<label>分類:</label>
</div>
<select name="category_id" style="padding:5px 15px; border:1px solid #ddd;">
<option value="">-請(qǐng)選擇-</option>
<?php
foreach( $arr_news_category as $val){
echo "<option value='{$val['id']}'>{$val['name']}</option>";
}
?>
</select>
</div>
這樣添加新聞功能到這就完成了!

在之前的一堂課我們完成了新聞清單的分頁功能,那麼從這堂課開始我們開始要完成新聞的增刪改查,現(xiàn)在查已經(jīng)完成了,接下來就是刪除,編輯以及新增!今天主要跟大家介紹如何實(shí)現(xiàn)添加!
同樣,我們找到添加的模板,根據(jù)我們創(chuàng)建的數(shù)據(jù)表裡的字段,選擇需要的部分~添加新聞我們使用form表單提交然後插入數(shù)據(jù)庫(kù)
<form method="post" class="form-x" action="" enctype="multipart/form-data"> <div class="form-group"> <div class="label"> <label>分類:</label> </div> <select name="category_id" style="padding:5px 15px; border:1px solid #ddd;"> <option value="">-請(qǐng)選擇-</option> <?php foreach( $arr_news_category as $val){ echo "<option value='{$val['id']}'>{$val['name']}</option>"; } ?> </select> </select> </div> <div class="form-group"> <div class="label"> <label>標(biāo)題:</label> </div> <div class="field"> <input type="text" class="input w50" value="" name="title" data-validate="required:請(qǐng)輸入標(biāo)題" /> <div class="tips"></div> </div> </div> <div class="clear"></div> <div class="form-group"> <div class="label"> <label>關(guān)鍵字:</label> </div> <div class="field"> <input type="text" class="input" name="tag" value="" /> </div> </div> <div class="form-group"> <div class="label"> <label>內(nèi)容:</label> </div> <div class="field"> <textarea name="content" class="input" id="content" style="height:450px; width: 98%; border:1px solid #ddd;"></textarea> </div> </div> <div class="form-group"> <div class="label"> <label>作者:</label> </div> <div class="field"> <input type="text" class="input w50" name="author" value="" /> <div class="tips"></div> </div> </div> <div class="form-group"> <div class="label"> <label>圖片:</label> </div> <div class="field"> <input type="file" id="url1" name="pic" class="input tips" style="width:25%; float:left;" value="" data-toggle="hover" data-place="right" data-image="" /> <input type="button" class="button bg-blue margin-left" id="image1" value="+ 瀏覽上傳" style="float:left;"> <div class="tipss">圖片尺寸:500*500</div> </div> </div> <div class="form-group"> <div class="label"> <label>發(fā)布時(shí)間:</label> </div> <div class="field"> <script src="../js/laydate/laydate.js"></script> <input type="text" class="laydate-icon input w50" name="created_at" onclick="laydate({istime: true, format: 'YYYY-MM-DD hh:mm:ss'})" value="" data-validate="required:日期不能為空" style="padding:10px!important; height:auto!important;border:1px solid #ddd!important;" /> <div class="tips"></div> </div> </div> <div class="form-group"> <div class="label"> <label></label> </div> <div class="field"> <button class="button bg-main icon-check-square-o" type="submit"> 提交</button> </div> </div> </form>
form表單裡我們用post傳輸資料method="post" ?
action="" 這個(gè)是提交哪裡我這裡是提交到本頁面,但是推薦大家另外建立一個(gè)處理頁面
enctype="multipart/ form-data" 這個(gè)是上傳圖片不可或缺的!
內(nèi)容這裡我們使用的編輯器,很簡(jiǎn)單的,大家在網(wǎng)路上下載一個(gè),放到你的專案裡面,然後呼叫使用<textarea></textarea>
<div class="form-group"> <div class="label"> <label>內(nèi)容:</label> </div> <div class="field"> <textarea name="content" class="input" id="content" style="height:450px; width: 98%; border:1px solid #ddd;"></textarea> </div> </div>
呼叫編輯器重要的就是<script>程式碼如下:
<script type="text/javascript"> //實(shí)例化編輯器 //建議使用工廠方法getEditor創(chuàng)建和引用編輯器實(shí)例,如果在某個(gè)閉包下引用該編輯器,直接調(diào)用UE.getEditor('editor')就能拿到相關(guān)的實(shí)例 UE.getEditor('content',{initialFrameWidth:1500,initialFrameHeight:400,}); </script>
form表單設(shè)定好,我們就開始寫php處理頁面
第一步:連接資料庫(kù)
<?php // 連接mysql數(shù)據(jù)庫(kù) $link = mysqli_connect('localhost', 'root', 'root'); if (!$link) { echo "connect mysql error!"; exit(); } // 選中數(shù)據(jù)庫(kù) news為數(shù)據(jù)庫(kù)的名字 $db_selected = mysqli_select_db($link, 'news'); if (!$db_selected) { echo "<br>selected db error!"; exit(); } // 設(shè)置mysql字符集 為 utf8 $link->query("set names utf8"); ?>
因?yàn)槲覀冞@裡要上傳圖片。所以我對(duì)圖片上傳,保存做處理:
<?php if(count($_FILES['pic']) > 0){ // 檢查文件類型 if( !in_array($_FILES['pic']['type'], array('image/jpeg','image/png', 'image/gif')) ){ echo "只運(yùn)行上傳jpg或png圖片, 文件類型不合法,不允許上傳"; } // 檢查文件大小 if ($_FILES['pic']['size'] > 5*1024*1024){ echo "文件最大尺寸為5M,不允許上傳."; } $file_ext= pathinfo($_FILES['pic']['name'], PATHINFO_EXTENSION); // 獲取文件后綴名 $tmp_file = $_FILES['pic']['tmp_name']; // 臨時(shí)文件 $dest_file = pathinfo($tmp_file, PATHINFO_FILENAME).".".$file_ext; // 保存的文件名 //move_uploaded_file($tmp_file, "d:/wamp/www/upload/".$dest_file); // 使用絕對(duì)地址保存圖片 move_uploaded_file($tmp_file, "../../upload/".$dest_file); // 使用絕對(duì)路徑地址保存圖片 $avatar_path ="../../upload/".$dest_file; // 注意,保存的時(shí)候,設(shè)置從服務(wù)器的根目錄開始 } ?>
圖片處理完以後我們要開始講form表單傳輸過來資料插入資料庫(kù):
<?php if ($_POST['created_at']){ $current_time = $_POST['created_at']; }else{ $current_time = date("Y-m-d H:i:s"); } $sql = "insert into new(category_id,title,content,tag,author,pic,created_at) VALUES ('{$_POST['category_id']}', '{$_POST['title']}', '{$_POST['content']}', '{$_POST['tag']}', '{$_POST['author']}', '{$avatar_path}', '$current_time' )"; $result = mysqli_query($link,$sql); if($result){ $url = "http://127.0.0.1/news/Admin/new/new_list.php"; header("Location: $url"); exit; }else{ echo "添加新聞失敗!"; echo mysqli_error($link); exit; } } ?>
這裡我們還有一個(gè)分類要選擇,所以我們還需要查詢分類表:
$sql = "select * from new_category "; $result = mysqli_query($link, $sql); $arr_news_category = mysqli_fetch_all($result, MYSQL_ASSOC);
然後在分類這個(gè)input框一欄循環(huán)出分類
<div class="form-group"> <div class="label"> <label>分類:</label> </div> <select name="category_id" style="padding:5px 15px; border:1px solid #ddd;"> <option value="">-請(qǐng)選擇-</option> <?php foreach( $arr_news_category as $val){ echo "<option value='{$val['id']}'>{$val['name']}</option>"; } ?> </select> </div>
這樣添加新聞功能到這就完成了!