tp5 r3 一个简单的SQL语句调试实例
先看效果
核心代码
- public function index()
- {
- if (IS_AJAX && session("uid")==1){
- $sql=input("post.sql");
- $sql=base64_decode($sql);
- if ($sql){
- if (preg_match('/INSERT INTO|DELETE|UPDATE\s/i', $sql, $info)){
- $result=\think\Db::execute($sql);
- }
- else{
- $result=\think\Db::query($sql);
- }
- return dump($result);//json_encode($result)
- }else{
- return "错误的sql语句";
- }
- }
- //读取所有的表
- $tables=\think\Db::query('SHOW TABLES');
- $this->assign('tables',$tables);
- $database=\think\Config::get("database");
- $this->assign('tkey',"tables_in_".$database["database"]);
- return $this->fetch();
- }
复制代码
前台
- {include file="Public:header" /}
- <div id="page-content" style="min-height: 395px;">
- <div class="row">
- <div class="col-md-6">
- <div class="block full">
- <div class="block-title"><h2>数据库命令</h2></div>
- <form id="form-sql" onsubmit="return!1" class="form-horizontal form-bordered" method="post" action="{:url('db/index?type=save')}">
- <div class="form-group">
- <div class="col-md-9 col-md-offset-3">
- <div class="col-xs-6">
- <select size="1" class="form-control" id="tables">
- {volist name="tables" id="vo"}
- <option value="{$vo[$tkey]}">{$vo[$tkey]}</option>
- {/volist}
- </select>
- </div>
- <div class="btn-group">
- <a class="btn btn-default dropdown-toggle" data-toggle="dropdown" href="javascript:void(0)" aria-expanded="false">快捷SQL语句<span class="caret"></span></a>
- <ul class="dropdown-menu text-left">
- <li class="dropdown-header"><i class="fa fa-user pull-right"></i><strong>select语句</strong></li>
- <li><a href="javascript:insert('SELECT * FROM `[table]` WHERE 1')">select</a></li>
- <li><a href="javascript:insert('INSERT INTO `[table]` VALUES ([value-1],[value-2],[value-3],[value-4],[value-5])')">insert</a></li>
- <li><a href="javascript:insert('UPDATE `[table]` SET `[]`=[value-2] WHERE 1')">update</a></li>
- <li><a href="javascript:insert('DELETE FROM `[table]` WHERE 1')">delete</a></li>
- <li class="divider"></li>
- </ul>
- </div>
- </div>
- </div>
- <div class="form-group">
- <label for="example-textarea-input" class="col-md-3 control-label">SQL命令语句</label>
- <div class="col-md-9">
- <textarea placeholder="SQL命令语句" class="form-control" rows="7" name="sql" id="sql"></textarea>
- </div>
- </div>
- <div class="form-group form-actions">
- <div class="col-md-9 col-md-offset-3">
- <button class="btn btn-effect-ripple btn-primary" id="form" type="button" style="overflow:hidden;position:relative">提交</button>
- <button class="btn btn-effect-ripple btn-danger" type="reset" style="overflow:hidden;position:relative">重置</button>
- </div>
- </div>
- </form>
- </div>
- </div>
- <div class="col-md-6">
- <div class="block full">
- <div class="block-title"><h2>输出结果</h2></div>
- <p id="result" style="#cccccc;"></p>
- </div>
- </div>
- </div>
- </div>
- {include file="Public:footer" /}
- <script src="__PUBLIC__/static/js/base64/base64.js"></script>
- <script>
- function insert($sql){
- if ($sql){
- var table=$("#tables").val();
- $("#sql").text($sql.replace("[table]", table));
- }
- }
- $("#form").click(function(){
- var sql=$("#sql").val();
- var url =$("#form-sql").attr("action");
- if (!sql){jError("SQL不能为空!!", {VerticalPosition: 'center',HorizontalPosition: 'center'});return;}
- sql=base64_encode(sql);
- $.ajax({
- url:url,
- data:{sql:sql},
- type:'post',
- cache:true,
- dataType:'html',
- success:function(data) {
- $("#result").html(data);
- },
- error : function() {
- $("#result").html('错误的SQL语句!<br/>'+$("#sql").val());
- jError("错误的SQL语句!", {VerticalPosition: 'center',HorizontalPosition: 'center'});
- }
- });
- })
- </script>