如何使用PHP OOP插入多个单选按钮值

我正在使用PHP OOP开发CMS.在此项目中,用户可以添加新的Telegram Channel功能.对于此功能,我添加了此表单,其中还包含操作代码

    <?PHP 
if(isset($_POST['submit'])){
    $token = $_POST['token'];
    $cat = $_POST['cat'];
    $ads = $_POST['ads'];
    $key = $_POST['keyboard'];
    $tel = new Telegram();
    $notice = $tel->AddNew($token,$cat,$ads,$key);
}
?>
<div class='content-wrapper'>
    <section class='content-header'>
        <h1>
            Add New Telegram Account
            <small>You can add a new Telegram channel here</small>
        </h1>
        <ol class='breadcrumb'>
            <li class='active'>telegram.PHP</li>
        </ol>
    </section>
    <?PHP 
    if($dataSet->GetLevel()==1)
    { echo "
        <section class='content'>
            <div class='row'>
                <div class='col-md-6'>
                    <div class='Box Box-primary'>
                        <div class='Box-header with-border'>
                            <h3 class='Box-title'>required information</h3>
                        </div>
                        ";
                        if(isset($notice['success_message'])){
                            echo "
                                <div class='alert alert-success'>
                                    <strong>hey!</strong> ".$notice['success_message'].".
                                </div>
                            ";
                        }
                        echo "
                        <form role='form' method='POST' action=''>
                            <div class='Box-body'>
                                <div class='form-group'>
                                    <label>Token Number</label>
                                    <input type='text' class='form-control' placeholder='Enter token' name='token' required>
                                    <a href='#' style='color:purple;'>Having problem while getting token</a>
                                </div>
                                <div class='form-group'>
                                    <label>Select Category</label>
                                    <select name='cat' class='form-control'>
                                        <option value='empty'>---</option>
                                        <option value='technology'>Technology</option>
                                        <option value='4fun'>Game & Fun</option>
                                        <option value='news'>News</option>
                                        <option value='tools'>Tools</option>
                                        <option value='learning'>Learning</option>
                                        <option value='Traditional'>Traditional</option>
                                        <option value='media'>Media</option>
                                    </select>
                                </div>
                                <div class='form-group'>
                                    <div class='radio'>
                                        <label>
                                            <input type='radio' name='ads' id='optionsRadios1' value='on' checked>
                                            Set ads on</br>
                                            <input type='radio' name='ads' id='optionsRadios1' value='off'>
                                            Set ads off
                                        </label>
                                    </div>
                                </div>
                                <div class='form-group'>
                                    <div class='checkBox'>
                                        <label>
                                            <input type='checkBox' name='keyboard' value='with_keyboard'>
                                            Use dedicated keyboard for this bot
                                        </label></br>
                                        <label>
                                            <input type='checkBox' name='keyboard' value='without_keyboard'>
                                            Show keyboard at groups
                                        </label></br>
                                        <label>
                                            <input type='checkBox' name='answer' value='answer_messages_chats' checked>
                                            In private chats, just anwser the pre defined messages
                                        </label></br>
                                        <label>
                                            <input type='checkBox' name='answer' value='answer_messages_groups' checked>
                                            In groups, just answer the pre defined messages
                                        </label>
                                    </div>
                                </div>
                            </div>
                            <div class='Box-footer'>
                                Visit <a href='https://zite.pouyavagefi.com/documentation/telegram.PHP'>Telegram</a> Social Media Documentation.
                            </div>
                            <div class='Box-footer'>
                                <button name='submit' type='submit' class='btn btn-primary'>Submit</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </section> "; 
    }else{
        echo "
        <section class='content'>
            <div class='alert alert-warning'>
                <strong>Access Denied!</strong> You don\'t have permission to access this page.
            </div> 
        </section> "; 
    }
    ?>
</div>

正如你可以在顶部,我已经调用一个名为Telegram.class.PHP的类,这个类是这样的:

    <?PHP 
class Telegram
{   
    protected $notice = array();
    private $db;
    public function __construct()
    {
        $this->db = new Connection();
        $this->db = $this->db->dbConnect();
    }
    public function AddNew($token,$cat,$ads,$key)
    {
        if(!empty($token)&&!empty($cat)&&!empty($ads))
        {
            for ($i=0;$i<sizeof($ads);$i++)
            {
                for ($i=0;$i<sizeof($key);$i++)
                {
                    $new = $this->db->prepare("INSERT INTO channels (token_number, category_name, ads_set, keyboard_status) VALUES (?, ?, "/*.$ads[$i].*/", "/*.$key[$i].*/")");
                    $new->bindParam(1,$token);
                    $new->bindParam(2,$cat);
                    $new->bindParam(3,$ads);
                    $new->bindParam(4,$key);
                    $new->execute();
                    $notice['success_message'] = "New Telegram Channel was successfully added";
                    return $this->notice;
                }
            }

        }
    }
    public function getNotice()
    {
        return $this->notice;
    }
}
?>

因为我想在表中添加多个复选框,所以我在方法Add_New中使用了这个for循环(根据这个qeustion):

for ($i=0;$i<sizeof($ads);$i++)
        {
            for ($i=0;$i<sizeof($key);$i++)
            {
                $new = $this->db->prepare("INSERT INTO channels (token_number, category_name, ads_set, keyboard_status) VALUES (?, ?, "/*.$ads[$i].*/", "/*.$key[$i].*/")");
                $new->bindParam(1,$token);
                $new->bindParam(2,$cat);
                $new->bindParam(3,$ads);
                $new->bindParam(4,$key);
                $new->execute();
                $notice['success_message'] = "New Telegram Channel was successfully added";
                return $this->notice;
            }
        }

我知道这不正确,但我不知道将这些$ads [$i]和$key [$i]变量添加到insert语句中的正确方法……

所以如果你知道如何以正确的顺序这样做,请告诉我..谢谢!

解决方法:

只需使用PHP的内爆和爆炸功能.在保存数据的同时,将其破坏如下:

$key = implode(", ", $_POST['keyboard']);
$tel = new Telegram();
$notice = $tel->AddNew($token,$cat,$ads,$key);

根据您的需要替换内爆“,”的第一个参数.

对于显示使用爆炸如下:
$id = explode(“,”,$DbRes->键盘);

将数组转换为字符串后,无需在类中循环.查看更新的类代码如下:

<?PHP 
class Telegram
{   
    protected $notice = array();
    private $db;
    public function __construct()
    {
        $this->db = new Connection();
        $this->db = $this->db->dbConnect();
    }
    public function AddNew($token,$cat,$ads,$key)
    {
        if(!empty($token)&&!empty($cat)&&!empty($ads))
        {

            $new = $this->db->prepare("INSERT INTO channels (token_number, category_name, ads_set, keyboard_status) VALUES (?, ?, ".$ads.", ".$key.")");
            $new->bindParam(1,$token);
            $new->bindParam(2,$cat);
            $new->bindParam(3,$ads);
            $new->bindParam(4,$key);
            $new->execute();
            $notice['success_message'] = "New Telegram Channel was successfully added";
            return $this->notice;
        }
    }
    public function getNotice()
    {
        return $this->notice;
    }
}
?>

这有助于您建立理解.您需要将Array转换为字符串,以便MysqL数据库可以存储它.这篇文章也很有帮助:Save PHP array to MySQL?

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...