如何使用PHP OOP插入多个单选按钮值 [英] How to insert multiple radio button values with PHP OOP

查看:65
本文介绍了如何使用PHP OOP插入多个单选按钮值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP OOP处理CMS.在此项目中,有一项功能供用户添加新的电报频道.对于此功能,我添加了此表单,该表单还包含操作代码:

I'm working on a CMS with PHP OOP. In this project, there is a feature for users to add new Telegram Channel. For this feature, I added this form which also contains the action codes:

    <?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的类,该类如下所示:

As you can at the top, I have called a class which is called Telegram.class.php and this class goes like this:

    <?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循环(根据此

Because I want to add multiple checkboxes into table, I used this for loop inside the method Add_New (According to this question):

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] 变量添加到插入语句...

I know it's not correct but I don't know the right way to add these $ads[$i] and $key[$i] variables into the insert statement...

因此,如果您知道如何按正确的顺序进行操作,请告诉我..谢谢!

So if you know how to do this in the correct order, please let me know.. thanks!

推荐答案

仅使用PHP的内爆和爆炸功能.保存数据时,请按照以下步骤对其进行爆破:

Simply use implode and explode functions of PHP. While saving data, implode it like following:

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

根据需要替换爆破,"的第一个参数.

Replace first parameter of implode ", " according to your need.

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

For displaying use explode like following: $id = explode(", ", $DbRes->keyboard);

一旦将数组转换为字符串,则无需在类内部循环.请参见以下更新的类代码:

Once you convert your array to string then no need for looping inside your class. See updated code of class as below:

<?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数据库可以存储它.这篇文章也可能会有所帮助:将PHP数组保存到MySQL吗?

This will help you to build your understanding. You need to convert Array to string so that mysql database can store it. This post can also be helpful: Save PHP array to MySQL?

这篇关于如何使用PHP OOP插入多个单选按钮值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆