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 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误