亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

medoo配合smarty實(shí)現(xiàn)的增刪改查

原創(chuàng) 2019-01-23 15:59:36 265
摘要:入口文件 require './config/config.php'; require './db/contact.php'; require './db/add.php'; require './db/update.php'; require './db/delete.php
入口文件
require './config/config.php';
require './db/contact.php';
require './db/add.php';
require './db/update.php';
require './db/delete.php';
require './db/select.php';
$smarty->assign('rows',$rows);
$smarty->display('demo1.html');
smarty配置文件
require __DIR__.'/../vendor/autoload.php';
$smarty=new Smarty();
$smarty->setTemplateDir(__DIR__.'/../temp');
$smarty->setCompileDir(__DIR__.'/../comp');
$smarty->setCacheDir(__DIR__.'/../cache');
$smarty->setConfigDir(__DIR__.'/../config');
$smarty->setCaching(false);
$smarty->setCacheLifetime(60*60*24*7);
連接數(shù)據(jù)庫文件
require __DIR__ . './../vendor/autoload.php';
use Medoo\Medoo;
$db = new Medoo([
    // required
    'database_type' => 'mysql',
    'database_name' => 'phpstudy',
    'server' => '127.0.0.1',
    'username' => 'root',
    'password' => '',
    // [optional]
    'charset' => 'utf8',
    'port' => 3306
]);
$table='user';
增加
$name=isset($_GET['name'])?$_GET['name']:null;
$password=isset($_GET['password'])?$_GET['password']:null;
if($name && $password){
    $arr['name']=$name;
    $arr['password']=sha1($password);
    $arr['token']=sha1($name.time());
    $stmt=$db->insert($table,$arr);
}
刪除
$delete_id=isset($_GET['delete_id'])?$_GET['delete_id']:null;
if($delete_id){
    $where['id']=$delete_id;
    $db->delete($table,$where);
}
查找
$fileds=['id','name','password','token'];
$where=[];
$rows=$db->select($table,$fileds,$where);
修改
$update_id=isset($_GET['update_id'])?$_GET['update_id']:null;
$update_name=isset($_GET['update_name'])?$_GET['update_name']:null;
$update_password=isset($_GET['update_password'])?$_GET['update_password']:null;
if($update_id){
    $arr['password']=sha1($update_password);
    $arr['name']=$update_name;
    $where['id']=$update_id;
    $db->update($table,$arr,$where);
}
html文件
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>medoo增刪改查</title>
</head>
<body>
    <form action="">
        <table>
            <caption>添加數(shù)據(jù)</caption>
            <tr>
                <td>姓名</td>
                <td><input type="text" name="name"></td>
            </tr>
            <tr>
                <td>密碼</td>
                <td><input type="text" name="password"></td>
            </tr>
            <tr>
                <td></td>
                <td><button>注冊</button></td>
            </tr>
        </table>
    </form>
    <hr>
    <table border="1">
        <caption>用戶信息</caption>
        <tr>
            <th>id</th>
            <th>姓名</th>
            <th>密碼</th>
            <th>token</th>
            <th>操作</th>
        </tr>
 {foreach $rows as $row}
        <tr>
            <td>{$row.id}</td>
            <td>{$row.name}</td>
            <td>{$row.password}</td>
            <td>{$row.token}</td>
            <td>
                <button onclick="update(event)">修改</button>
                <a href="?delete_id={$row.id}">刪除</a>
            </td>
        </tr>
 {foreachelse}
        <tr>
            <td colspan="5">暫無數(shù)據(jù)</td>
        </tr>
 {/foreach}
    </table>
    <hr>
    <form action="">
        <table>
            <caption>修改信息</caption>
            <tr>
                <td>姓名</td>
                <td>
                    <input type="hidden" name="update_id">
                    <input type="text" name="update_name" value="321654">
                </td>
            </tr>
            <tr>
                <td>密碼</td>
                <td><input type="text" name="update_password"></td>
            </tr>
            <tr>
                <td></td>
                <td><button>提交</button></td>
            </tr>
        </table>
    </form>
<script>
 function update(e){
        //找到按鈕所在tr
 var btn=e.target;
 var tr=btn.parentNode.parentNode;
 //獲取所有子元素
 var trs=tr.children;
 //獲取修改信息的輸入框
 var update_id=document.getElementsByName('update_id')[0];
 var update_name=document.getElementsByName('update_name')[0];
 //設(shè)置值
 update_id.value=trs[0].innerHTML;
 update_name.value=trs[1].innerHTML;
 }
</script>
</body>
</html>


批改老師:韋小寶批改時(shí)間:2019-01-23 16:08:36
老師總結(jié):寫的很不錯 很有想法 編程就是要這樣大膽的去嘗試 繼續(xù)加油吧

發(fā)布手記

熱門詞條