dongwen4630 2009-12-17 19:50
浏览 44
已采纳

php插入sql函数

I want to create one insert function in php, which I will use to insert data into database.

Of course all inserts are not the same, some use one table others use other, tables have different column numbers etc. What is the best way to do this ?

I'm currently on the web page for member registration, now I don't want to allow everyone to register, I want admin to approve which members will get approved and which ones will not, so I set one hidden field to 0 and after approval it becomes one.

CASE: Someone comes to the website and changes input field value to 1 with firebug and submits the registration, automatically gets approved, I didn't try this I'm just considering the possibility. Now if I put member_active inside insert function then it won't work for other insert queries with few/less database column rows.

What is the way to do this, is the global/general insert function good idea at all, I pretty much know how to program all of this, but lately I've become more careful about data/efficiency and such things that can make your website better/worse so I want a good start. Thank you

  • 写回答

4条回答 默认 最新

  • dongmu7335 2009-12-17 19:55
    关注

    CodeIgniter also does this, but here it is:

    function Insert($table, $data, $ignore = false)
    {
        $sql = array();
    
        if (is_array($sql) === true)
        {
            $sql['query'] = 'INSERT ';
    
            if ($ignore === true)
            {
                $sql['query'] .= 'IGNORE ';
            }
    
            foreach ($data as $key => $value)
            {
                $data[$key] = Tick($key) . ' = ' . Quote($value);
            }
    
            $sql['query'] .= 'INTO ' . Tick($table) . ' SET ' . implode(', ', $data);
        }
    
        return implode('', $sql);
    }
    

    Obviously, you need to define your own Tick() and Quote() functions to back tick database entities and escape values respectively.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function