<?php
$att = array('id' => 'survey_form', 'role' => 'ajax-form');
echo form_open('welcome/addsurvey', $att);
?>
<div class="span5 offset1" id="form_div">
<?php echo validation_errors('<div class="alert alert-danger reg_error">', '</div>'); ?>
<div class="form-group control-group warning">
<label for="SurveyTitle">Survey Title</label>
<input name="SurveyTitle" type="text" class="form-control" data-validation-required-message="Please enter a Survey Title" id="SurveyTitle" required placeholder="Please enter the Survey Title" value="<?php echo set_value('SurveyTitle'); ?>">
<p class="help-block"></p>
</div>
<div class="form-group control-group warning">
<label for="SurveyIntroduction">Survey introduction</label>
<textarea name="SurveyIntroduction" type="text" class="form-control" id="SurveyIntroduction" placeholder="Enter the Survey Introduction" value="<?php echo set_value('SurveyIntroduction'); ?>"></textarea>
</div>
<button type="submit" class="btn btn-large btn-success">Add Survey</button>
</div>
</form>
I am inserting data via the form above it works fine how do i show the fields on the form after insertion via the set_value as it isn't working for me right now.
Here is my controller function below:
public function addsurvey() {
if (isset($_POST['SurveyTitle']) && isset($_POST['SurveyIntroduction'])) {
$this->load->library('form_validation');
$this->load->helper(array('form', 'url'));
$this->load->database();
$this->form_validation->set_rules('SurveyTitle', 'Survey Title', 'required');
$this->form_validation->set_rules('SurveyIntroduction', 'Survey introduction', 'required');
$this->form_validation->set_message('required', 'Survey Title and Survey Introduction cannot be empty');
if ($this->form_validation->run() == FALSE) {
$this->load->view('SurveyPage');
} else {
$today = date("Y-m-d H:i:s");
$UserId = "m123456789";
$SurveyTitle = $this->input->post('SurveyTitle');
$SurveyIntroduction = $this->input->post('SurveyIntroduction');
$SurveyLink = base_url() + rand() + $SurveyTitle;
$isDisabled = 0;
$db_query = $this->db->query("INSERT into survey(SurveyTitle,SurveyIntro,SurveyLink,DateCreated,CreatedBy,isDisabled) VALUES('" . $SurveyTitle . "','" . $SurveyIntroduction . "','" . $SurveyLink . "','" . $today . "','" . $UserId . "','" . $isDisabled . "')");
if ($this->db->insert_id($db_query)) {
$id = $this->db->insert_id();
$lastid['last_id'] = $id;
} else {
}
$data = $this->displayallsurveys();
$surveydata = $this->getsurveydatabasedonId($id);
$drpquestiontype = $this->displayquestiontypedropdown();
$chkvalidations = $this->displayvalidationscheckboxes();
$chkvalidationsother = $this->displayvalidationscheckboxesother();
$parent_data = array('drpquestiontype' => $drpquestiontype, 'chkvalidations' => $chkvalidations, 'chkvalidationsother' => $chkvalidationsother, 'lastid' => $lastid);
$parent_datasurveypage =array('data' => $data, 'surveydata' => $surveydata);
$this->load->view('SurveyPage', $parent_datasurveypage);
$this->load->view('question_data', $parent_data);
}
}
else
{
$data = $this->displayallsurveys();
$this->load->view('SurveyPage', array('data' => $data));
}
}
Related
Im working on a Opencart project and Im having trouble getting input data into db table. Currently when i submit it enters an empty record into the db.
I have a form that has several inputs and a save button. The query from the model works just fine when entered in phpmyadmin so Im having issues between the view and controller. This is a large pr0ject is overly confusing to me. Any help would be appreciated
html:
" class="btn btn-primary">
<form method="post" enctype="multipart/form-data" id="form-campaign" class="form-horizontal">
<div class="form-group">
<div class="col-sm-5 col-sm-push-1 form-group required>
<label class="col-sm-2 control-label" for="input-campaign-name"><?php echo $entry_name; ?></label>
<input type="text" name="name" value="" placeholder="<?php echo $entry_name; ?>" id="input-campaign-name" class="form-control" />
<?php if (isset($error_name[$language['language_id']])) { ?>
<div class="text-danger"><?php echo $error_name[$language['language_id']]; ?></div>
<?php } ?>
</div>
</div>
<div class="col-sm-5 col-sm-push-1 form-group required>
<label class="col-sm-2 control-label" for="input-campaign-goal"><?php echo $entry_goal; ?></label>
<input type="text" name="goal" value="" placeholder="<?php echo $entry_goal; ?>" id="input-campaign-goal" class="form-control" />
<?php if (isset($error_name[$language['language_id']])) { ?>
<div class="text-danger"><?php echo $error_name[$language['language_id']]; ?></div>
<?php } ?>
</div>
</div>
</form>
php: controller
public function add() {
$this->load->language('campaigns/campaign');
$this->document->setTitle($this->language->get('heading_title'));
$this->load->model('campaigns/campaign');
if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
$this->model_campaigns_campaign->addCampaign($this->request->post);
$this->session->data['success'] = $this->language->get('text_success');
$url = '';
if (isset($this->request->get['filter_campaign_id'])) {
$url .= '&filter_campaign_id=' . $this->request->get['filter_campaign_id'];
}
if (isset($this->request->get['filter_campaign_name'])) {
$url .= '&filter_campaign_name=' . urlencode(html_entity_decode($this->request->get['filter_campaign_name'], ENT_QUOTES, 'UTF-8'));
}
if (isset($this->request->get['filter_campaign_goal'])) {
$url .= '&filter_campaign_goal=' . $this->request->get['filter_campaign_goal'];
}
if (isset($this->request->get['order'])) {
$url .= '&order=' . $this->request->get['order'];
}
if (isset($this->request->get['page'])) {
$url .= '&page=' . $this->request->get['page'];
}
$this->response->redirect($this->url->link('campaigns/campaign', 'token=' . $this->session->data['token'] . $url, true));
}
$this->getForm();
}
php: model
public function addCampaign() {
$this->db->query("INSERT INTO " . DB_PREFIX . "campaigns SET campaign_name = '" . $this->db->escape($data['campaign_name']) . "', campaign_giving_goal = '" . (float)$data['campaign_giving_goal']
. "', campaign_giving_count_goal = '" . (float)$data['campaign_giving_count_goal'] . "', campaign_owner = '" . $this->db->escape($data['campaign_owner']). "'");
$this->cache->delete('campaign');
return $campaign_id;
}
I created registration form and corresponding controller and backend php code.
The registered data is storing correctly . But i am not reciving mail in my email id. Please help me with this..
My html Code
<div class="col-lg-6 col-lg-offset-3 well " style="margin-top:1em; background-color:black; ">
<h4 style="color:white; text-align:center;"> <strong> FILL UP REGISTRAION FORM </strong> </h4>
</div>
<div class="col-lg-6 col-lg-offset-3 well" style="margin-bottom:10em;">
<form name="register" ng-app="TempleWebApp" ng-controller="RegisterCtrl" ng-submit="SignUp(register.$valid)" novalidate>
<!-- First Name -->
<div class="form-group col-lg-6" ng-class="{ 'has-error' : register.fname.$invalid && (register.fname.$dirty || submitted)}">
<label>First Name</label>
<input class="form-control" type="text" name="fname" ng-model="fname" placeholder="First Name" ng-required="true">
<span class="help-block" ng-show="register.fname.$invalid && register.fname.$error.required && (register.fname.$dirty || submitted)">
First Name is required.</span>
</div>
<!-- Last Name -->
<div class="form-group col-lg-6" ng-class="{ 'has-error' : register.lname.$invalid && (register.lname.$dirty || submitted)}">
<label>Last Name</label>
<input class="form-control" type="text" name="lname" ng-model="lname" placeholder="Last Name" ng-required="true">
<span class="help-block" ng-show="register.lname.$invalid && register.lname.$error.required && (register.lname.$dirty || submitted)">
Last Name is required.</span>
</div>
<!-- City -->
<div class="form-group col-lg-6" ng-class="{ 'has-error' : register.city.$invalid && (register.city.$dirty || submitted)}">
<label>City</label>
<input class="form-control" type="text" name="city" ng-model="city" placeholder="City" ng-required="true">
<span class="help-block" ng-show="register.city.$invalid && register.city.$error.required && (register.city.$dirty || submitted)">
City is required.</span>
</div>
<!-- Gender -->
<div class="form-group col-lg-6" ng-class="{ 'has-error' : register.gender.$invalid && (register.gender.$dirty || submitted)}">
<label>Gender</label> <br>
<input type="radio" name="gender" ng-model="gender" value="male" ng-required="true"> Male
<input type="radio" name="gender" ng-model="gender" value="female" ng-required="true" style="margin-left:5em;"> Female
<span class="help-block" ng-show="register.gender.$invalid && register.gender.$error.required && (register.gender.$dirty || submitted)">
Gender is required.</span>
</div>
<!-- Email -->
<div class="form-group col-lg-12" ng-class="{ 'has-error' : register.email.$invalid && (register.email.$dirty || submitted)}">
<label>Email</label>
<input class="form-control" type="text" name="email" ng-model="useremail" placeholder="Email" ng-pattern="/^[^\s#]+#[^\s#]+\.[^\s#]{2,}$/" ng-required="true">
<span class="help-block" ng-show="register.email.$invalid && register.email.$error.required && (register.email.$dirty || submitted)">
Email is required.</span>
<span class="help-block" ng-show="register.email.$error.pattern">
Enter Valid Email .</span>
</div>
<!-- Password -->
<div class="form-group col-lg-6" ng-class="{ 'has-error' : register.password.$invalid && (register.password.$dirty || submitted)}">
<label>Password</label>
<input class="form-control" type="password" name="password" ng-model="userpassword" placeholder="Password" ng-required="true">
<span class="help-block" ng-show="register.password.$invalid && register.password.$error.required && (register.password.$dirty || submitted)">
Password is required.</span>
</div>
<!-- CONFIRM PASSWORD -->
<div class="form-group col-lg-6" ng-class="{ 'has-error' : register.confirmPassword.$invalid && (register.confirmPassword.$dirty || submitted)}">
<label>Confirm Password</label>
<input type="Password" name="confirmPassword" class="form-control" ng-model="confirmPassword" placeholder="Confirm Your Password" ng-compare="password" ng-required="true">
<p ng-show="register.confirmPassword.$error.required && (register.confirmPassword.$dirty || submitted)" class="help-block">confirm password is required.</p>
<p ng-show="register.confirmPassword.$error.compare && (register.confirmPassword.$dirty || submitted)" class="help-block">Confirm password doesnot match.</p>
</div>
<div class="col-lg-12 well " ng-repeat="error in errors" style="background-color:red; margin-top:0.5em;"> {{ error}} </div>
<div class="col-lg-12 well" ng-repeat="msg in msgs" style="margin-top:0.5em;">
<h5 style="color:green;">{{ msg}} </h5>
</div>
<button type="submit" class="btn btn-success col-lg-12">
<span ng-show="searchButtonText == 'REGISTERING'"><i class="glyphicon glyphicon-refresh spinning"></i></span>
{{ searchButtonText }}
</button>
</form>
</div>
My controller
app.controller('RegisterCtrl', function ($scope,$location, $http,$timeout) {
$scope.gender = '';
$scope.errors = [];
$scope.msgs = [];
$scope.searchButtonText = "REGISTER DETAILS";
$scope.test = "false";
$scope.SignUp = function(isValid) {
// Set the 'submitted' flag to true
$scope.submitted = true;
$scope.errors.splice(0, $scope.errors.length); // remove all error messages
$scope.msgs.splice(0, $scope.msgs.length);
if (isValid) {
$http.post('php/register.php',
{ 'fname': $scope.fname,
'lname': $scope.lname,
'city': $scope.city,
'gender': $scope.gender,
'pswd' : $scope.userpassword,
'email': $scope.useremail
})
.success(function(data, status, headers, config) {
if (data.msg != '')
{
$scope.msgs.push(data.msg);
$scope.test = "true";
$scope.searchButtonText = "REGISTERING";
//var goTopayment = function() { $scope.searchButtonText = "REGISTER DETAILS"; $location.path('/login'); };
// $timeout(goTopayment, 3000);
}
else
{
$scope.errors.push(data.error);
}
})
.error(function(data, status) { // called asynchronously if an error occurs or server returns response with an error status.
$scope.errors.push(status);
});
} // closing bracket for IF(isvalid)
} // closing bracket for $scope.SIGNUP = function
}); // closing bracket for register
My php Code is
<?php
$data = json_decode(file_get_contents("php://input"));
$fname = mysql_real_escape_string($data->fname);
$lname = mysql_real_escape_string($data->lname);
$city = mysql_real_escape_string($data->city);
$gender = mysql_real_escape_string($data->gender);
$upswd = mysql_real_escape_string($data->pswd);
$uemail = mysql_real_escape_string($data->email);
$con = mysql_connect('localhost', 'root', '');
mysql_select_db('registraion', $con);
$qry_em = 'select count(*) as cnt from users where Email ="' . $uemail . '"';
$qry_res = mysql_query($qry_em);
$res = mysql_fetch_assoc($qry_res);
if($res['cnt']==0){
$qry = 'INSERT INTO users (Firstname,Lastname,City,Gender,Password,Email) values
("' . $fname . '","' . $lname . '","' . $city . '","' . $gender . '","' . $upswd . '","' . $uemail . '")';
$qry_res1 = mysql_query($qry);
if (!$qry_res1) {
die('Invalid query: ' . mysql_error());
} else {
return mysql_insert_id();
}
$current_id = mysql_insert_id(); //last insert id
if(!empty($current_id)) {
$actual_link = "http://localhost/angular/php/"."activate.php?uid=" . $current_id;
$EmailTo = $uemail ;
$Subject = "User Registration Activation Email";
$Content = "Click this link to activate your account. <a href='" . $actual_link . "'>" . $actual_link . "</a>";
$MailHeaders = "From: Admin\r\n";
$success = mail($EmailTo, $Subject, $Content, $MailHeaders);
if($success ) {
$arr = array('msg' => "You have registered and the activation mail is sent to your email. Click the activation link to activate you account.", 'error' => '');
$jsn = json_encode($arr);
print_r($jsn);
}
}
}
else
{
$arr = array('msg' => "", 'error' => 'User Already exists with same email');
$jsn = json_encode($arr);
print_r($jsn);
}
?>
Finally I solved it. The problem was finding last inserted id value for $current_id variable.Since i was not getting correct value for this variable, value for $Emailto variable is not assigned with email id. So I changed php code to following way.
<?php
$data = json_decode(file_get_contents("php://input"));
$fname = mysql_real_escape_string($data->fname);
$lname = mysql_real_escape_string($data->lname);
$city = mysql_real_escape_string($data->city);
$gender = mysql_real_escape_string($data->gender);
$upswd = mysql_real_escape_string($data->pswd);
$uemail = mysql_real_escape_string($data->email);
$con = mysql_connect('localhost', 'root', '');
mysql_select_db('registraion', $con);
$qry_em = 'select count(*) as cnt from users where Email ="' . $uemail . '"';
$qry_res = mysql_query($qry_em);
$res = mysql_fetch_assoc($qry_res);
if($res['cnt']==0){
$qry = 'INSERT INTO users (Firstname,Lastname,City,Gender,Password,Email) values
("' . $fname . '","' . $lname . '","' . $city . '","' . $gender . '","' . $upswd . '","' . $uemail . '")';
$qry_res1 = mysql_query($qry);
//changed current_id value finding method.
$current_id = mysql_query("select uid from users ORDER BY uid DESC LIMIT 1"); //last insert id
if(!empty($current_id)) {
$actual_link = "http://localhost/angular/php/"."activate.php?uid=" . $current_id;
$EmailTo = $uemail;
$Subject = "User Registration Activation Email";
$Content = "Click this link to activate your account. <a href='" . $actual_link . "'>" . $actual_link . "</a>";
$MailHeaders = "From: Admin\r\n";
if(mail($EmailTo, $Subject, $Content, $MailHeaders) ) {
$arr = array('msg' => "You have registered and the activation mail is sent to your email. Click the activation link to activate you account.", 'error' => '');
$jsn = json_encode($arr);
print_r($jsn);
}
}
}
else
{
$arr = array('msg' => "", 'error' => 'User Already exists with same email');
$jsn = json_encode($arr);
print_r($jsn);
}
?>
As i am newbie to PHP kindly pardon me if i looks silly ,
I created a form in php , while i do the update part of the form the update reflects in db whereas in the form it still shows the same old value . i tried refresh and force refresh but nothing changes .
Whereas if i logout and login again , the form shows the updated value .
I tried using die(); after mysql_close($link); but it logs out the session and needs to re-login .
Kindly help me on viewing the changes while i am still inside the login .
My code is as follows :
<?php
if(isset($_POST['update'])) {
$name_a = $_POST['name'];
$email_a = $_POST['email'];
$pass_a = $_POST['password'];
$sql = "UPDATE admin SET a_name = '$name_a', a_email = '$email_a', password = '$pass_a' where aid='$update_id' ";
$retval = mysql_query($sql,$link);
if(! $retval ) {
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($link);
}else {
?>
<!-- Widget: user widget style 1 -->
<div class="box box-widget widget-user-2">
<!-- Add the bg color to the header using any of the bg-* classes -->
<div class="widget-user-header bg-yellow">
<div class="widget-user-image">
<?php echo '<img src="' . $img . '" class="img-circle" alt="User Image">'; ?>
</div>
<!-- /.widget-user-image -->
<h3 class="widget-user-username"><?php echo "$name"; ?></h3>
<h5 class="widget-user-desc"><?php echo "$role"; ?></h5>
</div>
<div class="box-footer no-padding">
<form role="form" method = "post" action = "<?php $_PHP_SELF ?>">
<div class="box-body">
<div class="form-group">
<label for="exampleInputName1">Name</label>
<input type="text" class="form-control" id="exampleInputName1" name="name" value="<?php echo "$name"; ?>">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" name="email" value="<?php echo "$email"; ?>">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" name="password" value="<?php echo "$password"; ?>">
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" name="update" id="update" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
<!-- /.widget-user -->
<?php
}
?>
SOLUTION
1) use the updated value like $name_a instead of $name because $name_a contain updated value and $name contain old value
2) reload page after update and get new value from database on page load and store that value in $name , $email etc variable (if new data update successfully in database then only you get new value )
3) if You store your data in session or cookie then update session and cookie value also when you update in database
Try this:
<?php
$name = '';
$email = '';
$password = '';
$update_id = '';
//$img = '';
//$role = '';
//$link = null;
if(
isset($_POST['update']) &&
isset($_POST['id']) &&
isset($_POST['name']) &&
isset($_POST['email']) &&
isset($_POST['password'])
) {
$update_id = mysql_real_escape_string($_POST['id']);
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$password = mysql_real_escape_string($_POST['password']);
$sql = 'UPDATE admin SET a_name = \'' . $name . '\', a_email = \'' . $email . '\', password = \'' . $password . '\' WHERE aid = \'' . $update_id . '\'';
$result = #mysql_query($sql, $link);
if(!$result)
die('Could not update data: ' . mysql_error($link));
echo 'Updated data successfully', "\n";
}
elseif(isset($_GET['id'][0])) {
$update_id = mysql_real_escape_string($_GET['id']);
$sql = 'SELECT a_name,a_email,a_password FROM admin WHERE aid = \'' . $update_id . '\'';
$result = #mysql_query($sql, $link);
if($result) {
$result = mysql_fetch_row($result);
$name = $result[0];
$email = $result[1];
$password = $result[2];
}
else {
echo 'Could not find the id.' . "\n";
$update_id = '';
}
}
unset($result);
if(isset($update_id[0])) {
mysql_close($link);
?>
<!-- Widget: user widget style 1 -->
<div class="box box-widget widget-user-2">
<!-- Add the bg color to the header using any of the bg-* classes -->
<div class="widget-user-header bg-yellow">
<div class="widget-user-image">
<img src="<?php echo htmlspecialchars($img); ?>" class="img-circle" alt="User Image">
</div>
<!-- /.widget-user-image -->
<h3 class="widget-user-username"><?php echo htmlspecialchars($name); ?></h3>
<h5 class="widget-user-desc"><?php echo htmlspecialchars($role); ?></h5>
</div>
<div class="box-footer no-padding">
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="id" value="<?php echo htmlspecialchars($update_id); ?>">
<div class="box-body">
<div class="form-group">
<label for="exampleInputName1">Name</label>
<input type="text" class="form-control" id="exampleInputName1" name="name" value="<?php echo htmlspecialchars($name); ?>">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" name="email" value="<?php echo htmlspecialchars($email); ?>">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" name="password" value="<?php echo htmlspecialchars($password); ?>">
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" name="update" id="update" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
<!-- /.widget-user -->
<?php }
else {
$sql = 'SELECT aid,a_name FROM admin';
$result = #mysql_query($sql, $link);
if($result) {
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '' . $row['a_name'] . '<br />' . "\n";
}
}
mysql_close($link);
}
?>
As #DivyeshSavaliya mentioned in the comment the issue is ,
I didn't Used Select query after update . Once done that the issue solved
The new working code is
<?php
if(isset($_POST['update'])) {
$name_a = $_POST['name'];
$email_a = $_POST['email'];
$pass_a = $_POST['password'];
$sql = "UPDATE admin SET a_name = '$name_a', a_email = '$email_a', password = '$pass_a' where aid='$update_id' ";
$retval = mysql_query($sql,$link);
if(! $retval ) {
die('Could not update data: ' . mysql_error());
}
}
$result = mysql_query("SELECT * FROM admin where aid='$update_id' ",$link);
while($row = mysql_fetch_array($result)){
$name = $row['a_name'];
$email = $row['a_email'];
$password = $row['password'];
}
mysql_close($link);
?>
Thanks to #DivyeshSavaliya
I am trying to pass a MySQL query into a variable but it is not storing it correctly. I am wanting the query result to be stored as a string.
Here is my attempt:
$authorid = $db->query("SELECT Author_ID FROM Authors WHERE Author_Name = '$author'");
The query works correctly in PHPMyAdmin so I know it is returning the correct information but I am unsure if this is the correct way to store it.
I am trying to use it for this:
$authoredquery = $db->query("SELECT Author_ID, Article_ID
FROM Articles_Authored
WHERE Author_ID = '$authorid' AND Article_ID ='$articleid'");
All of the query is spelled correctly. I was under the impression that this stored the query result as a string....
Here is a portion of the php code that runs after a form is submitted:
$dbHost = "localhost";
$dbUsername = "admin";
$dbPassword = "qegc0Qfs";
$dbName = "webdb";
// Create connection
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
// Check connection
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
} else {
// Post PHP variables
$journal = $_POST['journal_radio'];
$article_name = $_POST['article_name'];
$author1 = $_POST['author1_name'];
$author1uni = $_POST['author1_university'];
$author2 = $_POST['author2_name'];
$author2uni = $_POST['author2_university'];
$author3 = $_POST['author3_name'];
$author3uni = $_POST['author3_university'];
$author4 = $_POST['author4_name'];
$author4uni = $_POST['author4_university'];
$author5 = $_POST['author5_name'];
$author5uni = $_POST['author5_university'];
$author6 = $_POST['author6_name'];
$author6uni = $_POST['author6_university'];
$year = $_POST['year'];
$security = $_POST['is_security'];
function authorinfoinsert($author, $number, $authoruni, $articleid, $article, $db){
//check if Author already exists
$authorquery = $db->query("SELECT Author_Name FROM Authors WHERE Author_Name = '$author'");
if($authorquery->num_rows != 0) {
echo '<a href="> New Input </a><br />';
echo 'Author ' . $number . ' - ' . $author . ' - already present<br />';
} else {
//insert author if doesnt exist
$authorinsert = "INSERT INTO Authors (Author_Name) VALUES ('$author')";
if ($db->query($authorinsert) === TRUE) {
echo 'Author ' . $number . ' - ' . $author . ' - created successfully.<br />';
} else {
echo 'Error: ' . $authorinsert . '<br />' . $db->error . '<br />';
}
}
// store Author Author_ID value
$authorid = $db->query("SELECT Author_ID FROM Authors WHERE Author_Name = '$author'");
//check if Author University exists
$authoruniquery = $db->query("SELECT University_Name FROM University WHERE University_Name = '$authoruni'");
if($authoruniquery->num_rows != 0) {
echo ' New Input <br />';
echo 'Author ' . $number . ' University already present <br />';
} else {
//insert university if doesnt exist
$uniinsert = "INSERT INTO University (University_Name) VALUES ('$authoruni')";
if ($db->query($uniinsert) === TRUE) {
echo 'Author ' . $number . ' University - ' . $authoruni . ' - created successfully.<br />';
} else {
echo 'Error: ' . $uniinsert . "<br>" . $db->error . '<br />';
}
}
// store Author University_ID value
$authoruniid = $db->query("SELECT University_ID FROM University WHERE University_Name = '$authoruni'");
echo $authoruniid;
//Enter Author and Article in Articles_Authored
$authoredquery = $db->query("SELECT Author_ID, Article_ID FROM Articles_Authored WHERE Author_ID = '$authorid' AND Article_ID ='$articleid'");
if($authoredquery->num_rows != 0) {
echo ' New Input <br />';
echo 'Article Authored entry for Author ' . $number . ' - ' . $author . ' - already present<br />';
} else {
$articlesauthoredinsert = "INSERT INTO Articles_Authored (Article_ID, Author_ID, Name_Authored_As) VALUES ('$articleid','$authorid','$author')";
if ($db->query($articlesauthoredinsert) === TRUE) {
echo 'Article Authored - Author ' . $number . ' - created successfully <br />';
} else {
echo 'Error: ' . $articlesauthoredinsert . "<br>" . $db->error . '<br />';
}
}
//Enter Author University instance in Author_University
$authoruniinsert = "INSERT INTO Author_University (Article_ID, Author_ID, University_ID) VALUES ('$articleid','$authorid','$authoruniid')";
if ($db->query($authoruniinsert) === TRUE) {
echo 'University instance for Author ' . $number . ' created successfully<br />';
} else {
echo 'Error: ' . $authoruniinsert . "<br>" . $db->error . '<br />';
}
}
And I pass the args to the function like this:
authorinfoinsert($author1, 1, $author1uni, $articleid, $article, $db);
Here is all of the code if that helps. Sorry I know its a lot:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Input Form - Research Ranker</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script>
$(function() {
$( "#article_name" ).autocomplete({
</head>
<body>
<?PHP
if(!isset($_POST['submit'])){
print '
<div class="container-fluid ui-widget">
<form class="form-horizontal" method="post" action="test.php" onsubmit="return confirm("Is all of your data correct?");">
<fieldset>
<!-- Article Input -->
<legend>Article Input</legend>
<!-- Journal Radio Buttons (value is journal ISSN)-->
<div class="form-group">
<label class="col-md-4 control-label" for="journal_radio">Journal</label>
<div class="col-md-4">
<div class="radio">
<label for="journal_radio-0">
<input type="radio" name="journal_radio" id="journal_radio-0" value="2162-9730" checked="checked">
MIS Quarterly
</label>
</div>
<div class="radio">
<label for="journal_radio-1">
<input type="radio" name="journal_radio" id="journal_radio-1" value="1047-7047">
Information Systems Research
</label>
</div>
<div class="radio">
<label for="journal_radio-2">
<input type="radio" name="journal_radio" id="journal_radio-2" value="0742-1222">
Journal of Management Information Systems
</label>
</div>
<div class="radio">
<label for="journal_radio-3">
<input type="radio" name="journal_radio" id="journal_radio-3" value="1536-9323">
Journal of the Association for Information Systems
</label>
</div>
<div class="radio">
<label for="journal_radio-4">
<input type="radio" name="journal_radio" id="journal_radio-4" value="1476-9344">
European Journal of Information Systems
</label>
</div>
<div class="radio">
<label for="journal_radio-5">
<input type="radio" name="journal_radio" id="journal_radio-5" value="1365-2575">
Information Systems Journal
</label>
</div>
<div class="radio">
<label for="journal_radio-6">
<input type="radio" name="journal_radio" id="journal_radio-6" value="0963-8687">
Journal of Strategic Information Systems
</label>
</div>
<div class="radio">
<label for="journal_radio-7">
<input type="radio" name="journal_radio" id="journal_radio-7" value="0268-3962">
Journal of Information Technology
</label>
</div>
<div class="radio">
<label for="journal_radio-8">
<input type="radio" name="journal_radio" id="journal_radio-8" value="0167-9236">
Decision Support Systems
</label>
</div>
<div class="radio">
<label for="journal_radio-9">
<input type="radio" name="journal_radio" id="journal_radio-9" value="0378-7206">
Information and Management
</label>
</div>
</div>
</div>
<!-- Article Name Input-->
<div class="form-group">
<label class="col-md-4 control-label" for="article_name">Article Name:</label>
<div class="col-md-5">
<input id="article_name" name="article_name" type="text" placeholder="Article Name" class="form-control input-md" required="">
</div>
</div>
<!-- Author 1-->
<div class="form-group">
<label class="col-md-4 control-label" for="author1_name">Author 1:</label>
<div class="col-md-4">
<input id="author1_name" name="author1_name" type="text" placeholder="Author Name" class="form-control input-md" required="">
</div>
</div>
<!-- Author 1 University -->
<div class="form-group">
<label class="col-md-4 control-label" for="author1_university">University:</label>
<div class="col-md-4">
<input id="author1_university" name="author1_university" type="text" placeholder="Author University" class="form-control input-md" required="">
</div>
</div>
<!-- Author 2-->
<div class="form-group">
<label class="col-md-4 control-label" for="author2_name">Author 2:</label>
<div class="col-md-4">
<input id="author2_name" name="author2_name" type="text" placeholder="" class="form-control input-md">
</div>
</div>
<!-- Author 2 University-->
<div class="form-group">
<label class="col-md-4 control-label" for="author2_university">University:</label>
<div class="col-md-4">
<input id="author2_university" name="author2_university" type="text" placeholder="" class="form-control input-md">
</div>
</div>
<!-- Author 3-->
<div class="form-group">
<label class="col-md-4 control-label" for="author3_name">Author 3:</label>
<div class="col-md-4">
<input id="author3_name" name="author3_name" type="text" placeholder="" class="form-control input-md">
</div>
</div>
<!-- Author 3 University-->
<div class="form-group">
<label class="col-md-4 control-label" for="author3_university">University:</label>
<div class="col-md-4">
<input id="author3_university" name="author3_university" type="text" placeholder="" class="form-control input-md">
</div>
</div>
<!-- Author 4-->
<div class="form-group">
<label class="col-md-4 control-label" for="author4_name">Author 4: </label>
<div class="col-md-4">
<input id="author4_name" name="author4_name" type="text" placeholder="" class="form-control input-md">
</div>
</div>
<!-- Author 4 University-->
<div class="form-group">
<label class="col-md-4 control-label" for="author4_university">University:</label>
<div class="col-md-4">
<input id="author4_university" name="author4_university" type="text" placeholder="" class="form-control input-md">
</div>
</div>
<!-- Author 5-->
<div class="form-group">
<label class="col-md-4 control-label" for="author5_name">Author 5:</label>
<div class="col-md-4">
<input id="author5_name" name="author5_name" type="text" placeholder="" class="form-control input-md">
</div>
</div>
<!-- Author 5 University-->
<div class="form-group">
<label class="col-md-4 control-label" for="author5_university">University:</label>
<div class="col-md-4">
<input id="author5_university" name="author5_university" type="text" placeholder="" class="form-control input-md">
</div>
</div>
<!-- Author 6-->
<div class="form-group">
<label class="col-md-4 control-label" for="author6_name">Author 6:</label>
<div class="col-md-4">
<input id="author6_name" name="author6_name" type="text" placeholder="" class="form-control input-md">
</div>
</div>
<!-- Author 6 University-->
<div class="form-group">
<label class="col-md-4 control-label" for="author6_university">University:</label>
<div class="col-md-4">
<input id="author6_university" name="author6_university" type="text" placeholder="" class="form-control input-md">
</div>
</div>
<!-- Select Year Published -->
<div class="form-group">
<label class="col-md-4 control-label" for="year">Year Published</label>
<div class="col-md-4">
<select id="year" name="year" class="form-control">
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
</select>
</div>
</div>
<!-- Is Security Checkbox -->
<div class="form-group">
<label class="col-md-4 control-label" for="is_security"></label>
<div class="col-md-4">
<label class="checkbox-inline" for="is_security-0">
<input type="checkbox" name="is_security" id="is_security" value="1">
Security Related
</label>
</div>
</div>
<!-- Submit Form Button -->
<div class="form-group">
<div class="col-md-4 control-label">
<button id="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</fieldset>
</form>
</div>';
} else {
$dbHost = "localhost";
$dbUsername = "admin";
$dbPassword = "qegc0Qfs";
$dbName = "webdb";
// Create connection
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
// Check connection
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
} else {
// Post PHP variables
$journal = $_POST['journal_radio'];
$article_name = $_POST['article_name'];
$author1 = $_POST['author1_name'];
$author1uni = $_POST['author1_university'];
$author2 = $_POST['author2_name'];
$author2uni = $_POST['author2_university'];
$author3 = $_POST['author3_name'];
$author3uni = $_POST['author3_university'];
$author4 = $_POST['author4_name'];
$author4uni = $_POST['author4_university'];
$author5 = $_POST['author5_name'];
$author5uni = $_POST['author5_university'];
$author6 = $_POST['author6_name'];
$author6uni = $_POST['author6_university'];
$year = $_POST['year'];
$security = $_POST['is_security'];
function authorinfoinsert($author, $number, $authoruni, $articleid, $article, $db){
//check if Author already exists
$authorquery = $db->query("SELECT Author_Name FROM Authors WHERE Author_Name = '$author'");
if($authorquery->num_rows != 0) {
echo ' New Input <br />';
echo 'Author ' . $number . ' - ' . $author . ' - already present<br />';
} else {
//insert author if doesnt exist
$authorinsert = "INSERT INTO Authors (Author_Name) VALUES ('$author')";
if ($db->query($authorinsert) === TRUE) {
echo 'Author ' . $number . ' - ' . $author . ' - created successfully.<br />';
} else {
echo 'Error: ' . $authorinsert . '<br />' . $db->error . '<br />';
}
}
// store Author Author_ID value
$authorid = $db->query("SELECT Author_ID FROM Authors WHERE Author_Name = '$author'");
//check if Author University exists
$authoruniquery = $db->query("SELECT University_Name FROM University WHERE University_Name = '$authoruni'");
if($authoruniquery->num_rows != 0) {
echo ' New Input <br />';
echo 'Author ' . $number . ' University already present <br />';
} else {
//insert university if doesnt exist
$uniinsert = "INSERT INTO University (University_Name) VALUES ('$authoruni')";
if ($db->query($uniinsert) === TRUE) {
echo 'Author ' . $number . ' University - ' . $authoruni . ' - created successfully.<br />';
} else {
echo 'Error: ' . $uniinsert . "<br>" . $db->error . '<br />';
}
}
// store Author University_ID value
$authoruniid = $db->query("SELECT University_ID FROM University WHERE University_Name = '$authoruni'");
echo $authoruniid;
//Enter Author and Article in Articles_Authored
$authoredquery = $db->query("SELECT Author_ID, Article_ID FROM Articles_Authored WHERE Author_ID = '$authorid' AND Article_ID ='$articleid'");
if($authoredquery->num_rows != 0) {
echo ' New Input <br />';
echo 'Article Authored entry for Author ' . $number . ' - ' . $author . ' - already present<br />';
} else {
$articlesauthoredinsert = "INSERT INTO Articles_Authored (Article_ID, Author_ID, Name_Authored_As) VALUES ('$articleid','$authorid','$author')";
if ($db->query($articlesauthoredinsert) === TRUE) {
echo 'Article Authored - Author ' . $number . ' - created successfully <br />';
} else {
echo 'Error: ' . $articlesauthoredinsert . "<br>" . $db->error . '<br />';
}
}
//Enter Author University instance in Author_University
$authoruniinsert = "INSERT INTO Author_University (Article_ID, Author_ID, University_ID) VALUES ('$articleid','$authorid','$authoruniid')";
if ($db->query($authoruniinsert) === TRUE) {
echo 'University instance for Author ' . $number . ' created successfully<br />';
} else {
echo 'Error: ' . $authoruniinsert . "<br>" . $db->error . '<br />';
}
}
//check if article exist
$articlequery = $db->query("SELECT Article_Title, ISSN FROM Articles WHERE Article_Title = '$article_name' AND ISSN = '$journal'");
if($articlequery->num_rows != 0) {
echo ' New Input <br />';
echo "<script type= 'text/javascript'>alert('Article Already Present');</script>";
} else {
//insert article if doesn't exist
$articleinsert = "INSERT INTO Articles (ISSN, Year_Published, Is_Security, Article_Title)
VALUES ('$journal','$year','$security','$article_name')";
if ($db->query($articleinsert) === TRUE) {
echo 'New Article - ' . $article_name . ' - created successfully<br />';
} else {
echo 'Error: ' . $articleinsert . "<br>" . $db->error . '<br />';
}
// store Article_ID value
$articleid = $db->query("SELECT Article_ID FROM Articles WHERE Article_Title = '$article_name' AND ISSN = '$journal'");
//insert author 1 info
authorinfoinsert($author1, 1, $author1uni, $articleid, $article, $db);
//check if Author 2 has input
if (isset($author2)){
authorinfoinsert($author2, 2, $author2uni, $articleid, $article, $db);
}
//check if Author 3 has input
if (isset($author3)){
authorinfoinsert($author3, 3, $author3uni, $articleid, $article, $db);
}
//check if Author 4 has input
if (isset($author4)){
authorinfoinsert($author4, 4, $author4uni, $articleid, $article, $db);
}
//check if Author 5 has input
if (isset($author5)){
authorinfoinsert($author5, 5, $author5uni, $articleid, $article, $db);
}
//check if Author 6 has input
if (isset($author6)){
authorinfoinsert($author6, 6, $author6uni, $articleid, $article, $db);
} else{
$db->close();
}
}
}
}
?>
</body>
</html>
Okay so from what you've told me, you're trying to use the object as a string, which is why it's not working.
$authorid is just $db->query(); which returns an object that you need to do something with. In this case, you need to grab the results from it like so:
$result = $db->query("SELECT Author_ID FROM Authors WHERE Author_Name = '$author'");
$row = $result->fetch_assoc();
$authorid = $row['Author_ID'];
Try that :)
My best guess is that it's returning an array rather than a string, which is why "echo" and "print" won't work.
You might try print_r($authoruniid) to see the structure of the array, then access your data through there.
I think you need to use mysqli fetch_assoc() to getting result of query
you must change your code to:
$dbHost = "localhost";
$dbUsername = "admin";
$dbPassword = "qegc0Qfs";
$dbName = "webdb";
// Create connection
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
// Check connection
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
} else {
// Post PHP variables
$journal = $_POST['journal_radio'];
$article_name = $_POST['article_name'];
$author1 = $_POST['author1_name'];
$author1uni = $_POST['author1_university'];
$author2 = $_POST['author2_name'];
$author2uni = $_POST['author2_university'];
$author3 = $_POST['author3_name'];
$author3uni = $_POST['author3_university'];
$author4 = $_POST['author4_name'];
$author4uni = $_POST['author4_university'];
$author5 = $_POST['author5_name'];
$author5uni = $_POST['author5_university'];
$author6 = $_POST['author6_name'];
$author6uni = $_POST['author6_university'];
$year = $_POST['year'];
$security = $_POST['is_security'];
function authorinfoinsert($author, $number, $authoruni, $articleid, $article, $db) {
//check if Author already exists
$authorquery = $db->query("SELECT Author_Name FROM Authors WHERE Author_Name = '$author'");
if ($authorquery->num_rows != 0) {
echo ' New Input <br />';
echo 'Author ' . $number . ' - ' . $author . ' - already present<br />';
} else {
//insert author if doesnt exist
$authorinsert = "INSERT INTO Authors (Author_Name) VALUES ('$author')";
if ($db->query($authorinsert) === TRUE) {
echo 'Author ' . $number . ' - ' . $author . ' - created successfully.<br />';
} else {
echo 'Error: ' . $authorinsert . '<br />' . $db->error . '<br />';
}
}
// store Author Author_ID value
$authorid = $db->query("SELECT Author_ID FROM Authors WHERE Author_Name = '$author'");
while ($row = $authorid->fetch_assoc()) {
/
echo $row['Author_ID'];
}
//check if Author University exists
$authoruniquery = $db->query("SELECT University_Name FROM University WHERE University_Name = '$authoruni'");
if ($authoruniquery->num_rows != 0) {
echo ' New Input <br />';
echo 'Author ' . $number . ' University already present <br />';
} else {
//insert university if doesnt exist
$uniinsert = "INSERT INTO University (University_Name) VALUES ('$authoruni')";
if ($db->query($uniinsert) === TRUE) {
echo 'Author ' . $number . ' University - ' . $authoruni . ' - created successfully.<br />';
} else {
echo 'Error: ' . $uniinsert . "<br>" . $db->error . '<br />';
}
}
// store Author University_ID value
$authoruniid = $db->query("SELECT University_ID FROM University WHERE University_Name = '$authoruni'");
while ($row = $authorid->fetch_assoc()) {/// better use this in a while loop to getting all records
echo $row['University_ID'];
}
//Enter Author and Article in Articles_Authored
$authoredquery = $db->query("SELECT Author_ID, Article_ID FROM Articles_Authored WHERE Author_ID = '$authorid' AND Article_ID ='$articleid'");
if ($authoredquery->num_rows != 0) {
echo ' New Input <br />';
echo 'Article Authored entry for Author ' . $number . ' - ' . $author . ' - already present<br />';
} else {
$articlesauthoredinsert = "INSERT INTO Articles_Authored (Article_ID, Author_ID, Name_Authored_As) VALUES ('$articleid','$authorid','$author')";
if ($db->query($articlesauthoredinsert) === TRUE) {
echo 'Article Authored - Author ' . $number . ' - created successfully <br />';
} else {
echo 'Error: ' . $articlesauthoredinsert . "<br>" . $db->error . '<br />';
}
}
//Enter Author University instance in Author_University
$authoruniinsert = "INSERT INTO Author_University (Article_ID, Author_ID, University_ID) VALUES ('$articleid','$authorid','$authoruniid')";
if ($db->query($authoruniinsert) === TRUE) {
echo 'University instance for Author ' . $number . ' created successfully<br />';
} else {
echo 'Error: ' . $authoruniinsert . "<br>" . $db->error . '<br />';
}
}
for more information about mysql_fetch_assoc() click here
When the admin user unchecks all the user groups does not clear the permissions from db and throws error. If no permissions are selected and the form is submitted it should clear the permissions column belonging to that id. But does not for some reason?
What is best way to make it work?
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE user_group_id = '10'' at line 4
UPDATE user_group SET name = 'Demonstration', permission = WHERE user_group_id = '10'
Filename: C:\Xampp\htdocs\codeigniter-project\system\database\DB_driver.php
Line Number: 330
User Group Model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users_group_model extends CI_Model {
public function addUserGroup($data) {
$this->db->query("INSERT INTO " . $this->db->dbprefix . "user_group SET name = " . $this->db->escape($data['name']) . ", permission = " . (isset($data['permission']) ? $this->db->escape(serialize($data['permission'])) : '') . " ");
}
public function editUserGroup($user_group_id, $data) {
$this->db->query("UPDATE " . $this->db->dbprefix . "user_group SET
name = " . $this->db->escape($data['name']) . ",
permission = " . (isset($data['permission']) ? $this->db->escape(serialize($data['permission'])) : '') . "
WHERE
user_group_id = '" . (int)$user_group_id . "'
");
}
public function getUserGroup($user_group_id) {
$query = $this->db->query("SELECT DISTINCT * FROM " . $this->db->dbprefix . "user_group WHERE user_group_id = '" . (int)$user_group_id . "' ");
$user_group = array(
'name' => $query->row('name'),
'permission' => unserialize($query->row('permission'))
);
return $user_group;
}
}
View Form
<?php
if (trim(!$user_group_id)) {
echo form_open('admin/users_group/add', array('class' => 'form-horizontal', 'role' => 'form', 'id' => "form-user-group"));
} else {
echo form_open('admin/users_group/edit/' . $user_group_id, array('class' => 'form-horizontal', 'role' => 'form','id' => "form-user-group"));
}
;?>
<div class="form-group required">
<label class="col-sm-2 control-label" for="input-name"><?php echo $entry_name; ?></label>
<div class="col-sm-10">
<input type="text" name="name" value="<?php echo $name; ?>" placeholder="<?php echo $entry_name; ?>" id="input-name" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label"><?php echo $entry_access; ?></label>
<div class="col-sm-10">
<div class="well well-sm" style="height: 150px; overflow: auto;">
<?php foreach ($permissions as $permission) { ?>
<div class="checkbox">
<label>
<?php if (in_array($permission, $access)) { ?>
<input type="checkbox" name="permission[access][]" value="<?php echo $permission; ?>" checked="checked" />
<?php echo $permission; ?>
<?php } else { ?>
<input type="checkbox" name="permission[access][]" value="<?php echo $permission; ?>" />
<?php echo $permission; ?>
<?php } ?>
</label>
</div>
<?php } ?>
</div>
<a onclick="$(this).parent().find(':checkbox').prop('checked', true);"><?php echo $text_select_all; ?></a> / <a onclick="$(this).parent().find(':checkbox').prop('checked', false);"><?php echo $text_unselect_all; ?></a>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label"><?php echo $entry_modify; ?></label>
<div class="col-sm-10">
<div class="well well-sm" style="height: 150px; overflow: auto;">
<?php foreach ($permissions as $permission) { ?>
<div class="checkbox">
<label>
<?php if (in_array($permission, $modify)) { ?>
<input type="checkbox" name="permission[modify][]" value="<?php echo $permission; ?>" checked="checked" />
<?php echo $permission; ?>
<?php } else { ?>
<input type="checkbox" name="permission[modify][]" value="<?php echo $permission; ?>" />
<?php echo $permission; ?>
<?php } ?>
</label>
</div>
<?php } ?>
</div>
<a onclick="$(this).parent().find(':checkbox').prop('checked', true);"><?php echo $text_select_all; ?></a> /
<a onclick="$(this).parent().find(':checkbox').prop('checked', false);"><?php echo $text_unselect_all; ?></a></div>
</div>
</form>
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users_group extends MX_Controller {
private $error = array();
public function __construct() {
parent::__construct();
$this->lang->load('admin/user/users_groups', 'english');
$this->load->model('admin/user/users_group_model');
if(!$this->user->logged()) {
redirect('admin');
} elseif(!$this->user->hasPermissionAccess()) {
redirect('admin/error');
} else {
return true;
}
}
public function index() {
$this->document->setTitle($this->lang->line('heading_title'));
$this->getList();
}
public function add() {
if ($this->request->server['REQUEST_METHOD'] == 'POST') {
$this->load->model('admin/user/users_group_model');
$this->users_group_model->addUserGroup($this->request->post);
redirect('admin/users_group');
}
$this->getForm();
}
public function edit() {
$this->load->model('admin/user/users_group_model');
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
$user_group_id = $this->uri->segment(4);
$this->users_group_model->editUserGroup($user_group_id, $this->request->post);
$this->session->set_flashdata('success', $this->lang->line('text_success'));
redirect('admin/users_group');
}
protected function getForm() {
$this->load->model('admin/user/users_group_model');
$this->load->library('request');
$data['heading_title'] = $this->lang->line('heading_title');
$data['text_select_all'] = $this->lang->line('text_select_all');
$data['text_unselect_all'] = $this->lang->line('text_unselect_all');
$data['entry_name'] = $this->lang->line('entry_name');
$data['entry_access'] = $this->lang->line('entry_access');
$data['entry_modify'] = $this->lang->line('entry_modify');
$data['button_save'] = $this->lang->line('button_save');
$data['button_cancel'] = $this->lang->line('button_cancel');
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->lang->line('text_home'),
'href' => site_url('admin/dashboard')
);
$data['breadcrumbs'][] = array(
'text' => $this->lang->line('heading_title'),
'href' => site_url('admin/users_group')
);
$data['cancel'] = site_url('admin/users_group');
if (!empty($this->error['warning'])) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
if (!empty($this->session->flashdata('success'))) {
$data['success'] = $this->session->flashdata('success', $this->lang->line('text_success'));
} else {
$data['success'] = '';
}
$user_group_id = $this->uri->segment(4);
$data['user_group_id'] = $user_group_id;
if (isset($user_group_id)) {
$data['action'] = site_url('user/users_group/add');
} else {
$data['action'] = site_url('admin/users_group/edit/' . $user_group_id);
}
if (!empty($user_group_id) && $this->request->server['REQUEST_METHOD'] != 'POST') {
$user_group_info = $this->users_group_model->getUserGroup($user_group_id);
}
if (isset($this->request->post['name'])) {
$data['name'] = $this->request->post['name'];
} elseif (!empty($user_group_info)) {
$data['name'] = $user_group_info['name'];
} else {
$data['name'] = '';
}
$ignore = array(
'blank',
'error',
'register',
'dashboard',
'column_left',
'menu',
'startup',
'login',
'logout',
'forgotten',
'reset',
'not_found',
'permission',
'footer',
'header'
);
$data['permissions'] = array();
$files = glob(APPPATH . 'modules/admin/' . 'controllers/*/*.php');
foreach ($files as $file) {
$part = explode('/', dirname($file));
$permission = basename($file, '.php');
if (!in_array($permission, $ignore)) {
$data['permissions'][] = $permission;
}
}
if (isset($this->request->post['permission']['access'])) {
$data['access'] = $this->request->post['permission']['access'];
} elseif (isset($user_group_info['permission']['access'])) {
$data['access'] = $user_group_info['permission']['access'];
} else {
$data['access'] = array();
}
if (isset($this->request->post['permission']['modify'])) {
$data['modify'] = $this->request->post['permission']['modify'];
} elseif (isset($user_group_info['permission']['modify'])) {
$data['modify'] = $user_group_info['permission']['modify'];
} else {
$data['modify'] = array();
}
$this->load->view('user/users_group_form', $data);
}
There seems to the problem on the permissions when its not being set it breaks up the query. I suggest just the active record on CI on this one. Example:
public function editUserGroup($user_group_id, $data)
{
$permission = (isset($data['permission']) ? serialize($data['permission']) : null);
$update = array(
'name' => $data['name'],
'permission' => $permission,
);
$this->db->where('user_group_id', $user_group_id);
$this->db->update($this->db->dbprefix . 'user_group', $update);
}
Or much cleaner using ->set() (personal preference):
public function editUserGroup($user_group_id, $data)
{
$permission = isset($data['permission']) ? serialize($data['permission']) : null;
$this->db->set('permission', $permission);
$this->db->set('name', $data['name']);
$this->db->where('user_group_id', $user_group_id);
$this->db->update($this->db->dbprefix . 'user_group', $update);
}