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>
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;
}
}
?>
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;
}
}
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!