Error 1054 in Codeigniter 2.1.3 - codeigniter

I have the 1054 error in Codeigniter and I don't know why. I want to create a login form and check if the user is logged or not.
But I only create a simple view and controller and the following error is displayed:
Error Number: 1054
Unknown column 'user_data' in 'field list'
INSERT INTO `ci_sessions` (`session_id`, `ip_address`, `user_agent`, `last_activity`, `user_data`) VALUES ('c392322ac31b7fac1c2d79cfbde9edf7', '127.0.0.1', 'Opera/9.80 (Windows NT 6.1) Presto/2.12.388 Version/12.15', 1368010716, '')
Filename: C:\wamp\www\..\system\database\DB_driver.php
Line Number: 330
I only created the table session with this script:
CREATE TABLE IF NOT EXISTS `ci_sessions` (
session_id varchar(40) DEFAULT '0' NOT NULL,
ip_address varchar(45) DEFAULT '0' NOT NULL,
user_agent varchar(50) NOT NULL,
last_activity int(10) unsigned DEFAULT 0 NOT NULL,
PRIMARY KEY (session_id)
);
The view:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Anuncios</title>
<link rel="stylesheet" href="/Pruebas/css/estilos.css" type="text/css"
media="screen"/>
</head>
<body>
<div id="contenedor">
<div id="menu">
<label for="home" id="inicio"><a href="http://localhost/Pruebas/index.php/
cindice/">Inicio</a></label>
<label for="acceso" id="login"><a href="http://localhost/Pruebas/index.php/
cindice/publicar">Publicar anuncio</a></label>
<label for="reg" id="registro"><a href="http://localhost/Pruebas/index.php/
cindice/registro">Registro</a></label>
<label for="empresa" id="sobrempresa"><a href="http://localhost/Pruebas/
index.php/cindice/sobempresa">Sobre nosotros</a></label>
<label for="contacto" id="contactar"><a href="http://localhost/Pruebas/
index.php/cindice/contacto">Contáctanos</a></label>
</div>
</div>
</body>
</html>
The controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Cindice extends CI_Controller {
function __construct() {
parent::__construct();
}
public function index()
{
$this->load->view('indice');
}
public function publicar()
{
echo "Aquí se publica el anuncio";
}
public function acceso()
{
echo "Esto es el acceso";
}
}
?>
How can I fix this issue?
Thanks.

The manual states your ci_sessions table should be created like this:
CREATE TABLE IF NOT EXISTS `ci_sessions` (
session_id varchar(40) DEFAULT '0' NOT NULL,
ip_address varchar(45) DEFAULT '0' NOT NULL,
user_agent varchar(120) NOT NULL,
last_activity int(10) unsigned DEFAULT 0 NOT NULL,
user_data text NOT NULL,
PRIMARY KEY (session_id),
KEY `last_activity_idx` (`last_activity`)
);
Note the "user_data" field that's missing from your table.

Related

Laravel - SQLSTATE[42S22]: Column not found: 1054 Unknown column 'hr_leave_type_details.hr_leave_type_id'

In my Laravel-5.8 application,I am trying to view dynamic input record on index.
This is the index view blade:
public function index()
{
$userCompany = Auth::user()->company_id;
$leavetypes = HrLeaveType::where('company_id', $userCompany)->get();
return view('hr.leave_types.index')->with('leavetypes', $leavetypes);
}
TABLES:
CREATE TABLE `hr_leave_types` (
`id` int(11) UNSIGNED NOT NULL,
`company_id` int(11) DEFAULT NULL,
`leave_type_name` varchar(100) NOT NULL,
`leave_type_code` varchar(20) DEFAULT NULL,
`description` longtext DEFAULT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `hr_leave_types`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `hr_leave_types_uniq1` (`company_id`,`leave_type_name`),
ADD UNIQUE KEY `hr_leave_types_uniq2` (`company_id`,`leave_type_code`);
ALTER TABLE `hr_leave_types`
MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT;
CREATE TABLE `hr_leave_type_details` (
`id` int(11) NOT NULL,
`leave_type_id` int(11) NOT NULL,
`company_id` int(11) NOT NULL,
`employment_type_id` int(11) NOT NULL,
`no_of_days` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `hr_leave_type_details`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `hr_leave_type_details_uniq1` (`company_id`,`leave_type_id`,`employment_type_id`);
ALTER TABLE `hr_leave_type_details`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
I am developing a dynamic input field. This involves two tables:
class HrLeaveType extends Model
{
public $timestamps = false;
protected $table = 'hr_leave_types';
protected $primaryKey = 'id';
protected $fillable = [
'company_id',
'leave_type_name',
'number_of_days',
'leave_type_code',
];
public function leavetypedetail()
{
return $this->hasMany('App\Models\Hr\LeaveTypeDetail');
}
}
class HrLeaveTypeDetail extends Model
{
public $timestamps = false;
protected $table = 'hr_leave_type_details';
protected $primaryKey = 'id';
protected $fillable = [
'leave_type_id',
'company_id',
'employment_type_id',
'no_of_days',
];
protected $casts = [
'data' => 'array',
];
public function leavetype()
{
return $this->belongsTo('App\Models\Hr\HrLeaveType');
}
public function employmenttype()
{
return $this->belongsTo('App\Models\Hr\HrEmploymentType');
}
}
view
<tbody>
#foreach($leavetypes as $key => $leavetype)
<td>
{{$leavetype->leave_type_name ?? '' }}
</td>
<td>
{{$leavetype->leave_type_code ?? '' }}
</td>
<td>
{!! Str::words($leavetype->description, 20, ' ...') !!}
</td>
<td>
#foreach($leavetype->leavetypedetail as $key => $leavetypedetail)
<ul class="list-unstyled">
<li>
{{$key+1}}. {{$leavetypedetail->employmenttype->employment_type ?? '' }}
</li>
</ul>
#endforeach
</td>
<td>
#foreach($leavetype->leavetypedetail as $key => $leavetypedetail)
<ul class="list-unstyled">
<li>
{{$key+1}}. {{$leavetypedetail->employmenttype->employment_type ?? '' }}
</li>
</ul>
#endforeach
</td>
<td>
#foreach($leavetype->leavetypedetail as $key => $leavetypedetail)
<ul class="list-unstyled">
<li>
{{$key+1}}. {{$leavetypedetail->no_of_days ?? '' }}
</li>
</ul>
#endforeach
</td>
</tr>
#endforeach
</tbody>
When I tried to view the index view blade, I got this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'hr_leave_type_details.hr_leave_type_id' in 'where clause' (SQL: select * from hr_leave_type_details where hr_leave_type_details.hr_leave_type_id = 1 and hr_leave_type_details.hr_leave_type_id is not null)
However, when I remove this portion of the code:
<td>
#foreach($leavetype->leavetypedetail as $key => $leavetypedetail)
<ul class="list-unstyled">
<li>
{{$key+1}}. {{$leavetypedetail->employmenttype->employment_type ?? '' }}
</li>
</ul>
#endforeach
</td>
<td>
#foreach($leavetype->leavetypedetail as $key => $leavetypedetail)
<ul class="list-unstyled">
<li>
{{$key+1}}. {{$leavetypedetail->no_of_days ?? '' }}
</li>
</ul>
#endforeach
</td>
The error vanished.
The leave_type_id is in the table. I don't know where it's getting hr_leave_type_id from
How do I resolve this?
Thanks
I got it resolved when I changed the Eloquent model relationship to:
public function leavetype()
{
return $this->belongsTo('App\Models\Hr\HrLeaveType', 'leave_type_id', 'id');
}
public function employmenttype()
{
return $this->belongsTo('App\Models\Hr\HrEmploymentType', 'employment_type_id', 'id' );
}

building a wordpress plugin AJAX based messenger

public function intelFrontendChat(){
ob_start();
?>
<html>
<html lang="en">
<form id="frmmain" name="frmmain" method="post">
<head>
<meta charset="utf-9">
<h5> Unit Briefing </h5>
</head>
<body>
<?php
global $current_user;
get_currentuserinfo();
$user = wp_get_current_user();
$role = ucwords(str_replace("_"," ",$user->roles[0]));
$user_id_for_post = $current_user->display_name;
?>
<div id="div_user_name" class="div_user_class">
<input name="user_id_message" id="user_id_message" type="hidden" value="<?php echo $user_id_for_post?>, <?php echo $role?>" />
</div>
<div id="div_chat_display" class="chat_display"></div>
<div class = "submition_for_chat_int" id="div_send">
<p>
<input type="text" id="txt_message_id" name="txt_message" style="width: 100%" /><br></p>
<p><input type="button" name="btn_send_chat" id="btn_send_chat" value="Send" /></p>
</div>
</form>
</body>
</html>
<?php
}
public function asIntelChatTable(){
//This is the table for mission briefings and general chat
global $wpdb;
$table = $wpdb->prefix . "intel_chat_table";
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`userid` varchar(255),
`message` varchar(255),
PRIMARY KEY (`id`)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
public function intelChatRoomCallBack(){
global $wpdb;
$table_name = $wpdb->prefix . "intel_chat_table";
$ChatsOne = $_POST['chat_insert_data'];
$ChatsOneUser = $_POST['chat_insert_user'];
$rows_affected = $wpdb->insert( $table_name, array(
//'id' => 1,
'userid' => $ChatsOneUser,
'message' => $ChatsOne
));
die();
}
public function aSCustomDataDisplayChatFromTable(){
global $wpdb;
$table_name = $wpdb->prefix."intel_chat_table" ;
$chatretrieve_datas = $wpdb->get_results( "SELECT * FROM $table_name" );
foreach ($chatretrieve_datas as $chatretrieve_data){
$intel_chat_data = array(
$chatretrieve_data->userid, ': ',
$chatretrieve_data->message, '<br/>'
);
}
wp_send_json($intel_chat_data);
die();
}
}
This is my javascript
var formId = document.getElementById("frmmain");
if (formId){
var autoRefreshMessages = function(){
jQuery.ajax({
url : intel_chat_local_script.ajaxurl,
type : 'post',
data : {
action : 'display_message_in_table',
currentChat : 'intel_chat_data',
},
success : function(data) {
console.log(data);
jQuery("#div_chat_display").append(data);
}
});
//setTimeout(autoRefreshMessages, 3000);
};
autoRefreshMessages();
btnSendChat.addEventListener("click", function(){
var fieldTxtInput = document.querySelector('.submition_for_chat_int [name="txt_message"]').value
var userTxtInput = document.querySelector('.div_user_class [name="user_id_message"]').value
jQuery.ajax({
url : intel_chat_local_script.ajaxurl,
type : 'post',
data : {
action : 'insert_message_in_table',
chat_insert_data : fieldTxtInput,
chat_insert_user : userTxtInput
},
success : function() {
document.getElementById("txt_message_id").value = '';
document.getElementById("btn_send_chat").reset;
}
});
});
};
});
I am trying to build a messaging system wordpress plugin. However, I am really struggling with building a auto-refresh messages functions that will list all the messages in the table.
I set up a function to wp_send_json and a javascript function to auto-refresh but it only displays the latest table entry. I also tried to set up two contrasting variables so that if they didn't equal each other to update the table, but I didn't have any luck with that either.

CodeIgniter - On click create table in database

I have an input field and a button. when I click on the button, a table must be created in the database, and its name should be the input from the input field.
CONTROLLER:
function create()
{
$table = $this->input->post('table');
$this->M_users->create($table);
}
MODEL:
function create($table)
{
$sql = "CREATE TABLE ".$table." (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
$query = $this->db->query($sql);
return $query;
}
VIEW:
<form method="post" action="<?php echo base_url('create');?>">
<input type="text" name="table">
<input type="submit" name="">
</form>
If you want to do it with forge class, refer the following link:
https://www.codeigniter.com/user_guide/database/forge.html

wamp not saving data using codeigniter

hi i am trying to insert data into mysql database from my codeigniter form but its not working, instead it redirects me back to wampserver homepage. I am using wampserver and windows 2008 operating system. earlier on I had changed the httpd.conf port listen to 8080 since wamp was not working.
my database is;
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) NOT NULL,
`email` varchar(32) NOT NULL,
`password` varchar(32) NOT NULL,
`gender` varchar(8) NOT NULL,
`registered` varchar(16) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
my model is:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function add_user()
{
$data=array(
'username'=>$this->input->post('username'),
'email'=>$this->input->post('email'),
'password'=>md5($this->input->post('password')),
'gender'=>$this->input->post('gender'),
'registred'=>time()
);
$this->db->insert('users',$data);
return true;
}
}
my controller is:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller {
public function index()
{
$this->load->view('register_view');
}
public function register()
{
$this->load->view('register_view');//loads the register_view.php file in views folder
}
public function do_register()
{
if($this->input->post('register'))//$_POST["register"];
{
$this->load->model('user_model');//loads the user_model.php file in models folder
if($this->user_model->add_user())
{
echo "hi ".$this->input->post('username')." Registred successfully" ;
}
else
{
echo "Registration failed";
}
}
}
}
my view is:
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf- 8"></head>
<body>
<div class="container">
<table>
<tr>
<form action="<?=site_url('user/do_register')?>" method="post">
<td><label for="username">User Name</label>
<input type="text" name="username"/></td>
<tr><td><label for="email">Email</label>
<input type="text" name="email"/></td></tr>
<tr><td><label for="password">Password</label>
<input type="password" name="password"/></td><tr>
<tr><td><label for="gender">Gender</label>
<input type="radio" name="gender" value="male"/>male
<input type="radio" name="gender" value="female"/>female</tr></td>
<tr><td><input type="submit" value="Sign up" name="register"/>
</tr></td>
</tr>
</form>
</table>
</div>
</body>
</html>
kindly assist me remove this error

Related select in Magento how I do

It's possibile, in Magento, create two or more related select?
For example Country / Region
I try with jQuery but seem doesn't work.
Best Regards
I try with this code: in Mymodule output
app/design/frontend/base/default/template/mymodule/mymodule.phtml
<div id="continenti">
<?php
include_once 'option.class.php';
$obj = new Option();
$obj->ShowContinenti();
?>
</div>
<div id="nazioni">
Seleziona una nazione:<br>
<select id="sel_nazioni" name="sel_nazioni"><option value="no">Scegli...</option>
</select>
</div>
<div id="result"></div>
In my app/design/frontend/base/default/template/page/html/head.phtml
<script type="text/javascript" src="./jquery/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#sel_continenti').change(function(){
var cont = $('#sel_continenti').attr('value');
$.post("selection.php", {id_cont:cont}, function(data){
$("#sel_nazioni").empty();
$("div#result").empty();
$("#sel_nazioni").prepend(data);
});
});
$('#sel_nazioni').change(function(){
var id_naz = $('#sel_nazioni').attr('value');
$.post("result.php", {id:id_naz}, function(data){
$("div#result").empty();
$("div#result").prepend(data);
});
});
});
</script>
In my app/code/local/frontname/mymodule/sql/mysql4-install-0.1.0
<?php
$installer = $this;
$installer->startSetup();
$installer->run("
-- DROP TABLE IF EXISTS {$this->getTable('continenti')};
CREATE TABLE {$this->getTable('continenti')}
`id` INT(2) UNSIGNED NOT NULL AUTO_INCREMENT,
`continente` VARCHAR(40) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
INSERT INTO `continenti` (`id`, `continente`) VALUES
(1, 'Europa'),
(2, 'Africa'),
(3, 'Sud America');
-- DROP TABLE IF EXISTS {$this->getTable('nazioni')};
CREATE TABLE {$this->getTable('nazioni')}
`id` INT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
`id_cont` INT(2) UNSIGNED NOT NULL,
`nazione` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=14 ;
INSERT INTO {$this->getTable('nazioni')} (`id`, `id_cont`, `nazione`) VALUES
(1, 1, 'Spagna'),
(2, 1, 'Francia'),
(3, 1, 'Italia'),
(4, 1, 'Germania'),
(5, 1, 'Belgio'),
(6, 2, 'Egitto'),
(7, 2, 'Marocco'),
(8, 2, 'Tunisia'),
(9, 2, 'Uganda'),
(10, 3, 'Argentina'),
(11, 3, 'Cile'),
(13, 3, 'Brasile');
")
;
$installer->endSetup();
File result.php
<?php
include_once 'option.class.php';
$obj = new Option();
$obj->ShowResult();
?>
File selection.php
<?php
include_once 'option.class.php';
$obj = new Option();
$obj->ShowNazioni();
?>
The first select appear with the content of Region Table but, if I click and choise one region, the second select doesn't load data inside itself.
EDIT: resolved!
I change my head.phtml in
<script type="text/javascript" src="./jquery/jquery-1.3.2.js"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('#sel_continenti').change(function(){
var cont = jQuery('#sel_continenti').attr('value');
jQuery.post("selection.php", {id_cont:cont}, function(data){
jQuery("#sel_nazioni").empty();
jQuery("div#result").empty();
jQuery("#sel_nazioni").prepend(data);
});
});
jQuery('#sel_nazioni').change(function(){
var id_naz = jQuery('#sel_nazioni').attr('value');
jQuery.post("result.php", {id:id_naz}, function(data){
jQuery("div#result").empty();
jQuery("div#result").prepend(data);
});
});
});
</script>
I change $ with jQuery word!
Magento already has this kind of selects in checkout addresses phtmls, just that it uses Prototype.
<div class="field">
<label for="billing:region_id" class="required"><em>*</em><?php echo $this->__('State/Province') ?></label>
<div class="input-box">
<select id="billing:region_id" name="billing[region_id]" title="<?php echo $this->__('State/Province') ?>" class="validate-select" style="display:none;">
<option value=""><?php echo $this->__('Please select region, state or province') ?></option>
</select>
<script type="text/javascript">
//<![CDATA[
$('billing:region_id').setAttribute('defaultValue', "<?php echo $this->getAddress()->getRegionId() ?>");
//]]>
</script>
<input type="text" id="billing:region" name="billing[region]" value="<?php echo $this->escapeHtml($this->getAddress()->getRegion()) ?>" title="<?php echo $this->__('State/Province') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('region') ?>" style="display:none;" />
</div>
<div class="field">
<label for="billing:country_id" class="required"><em>*</em><?php echo $this->__('Country') ?></label>
<div class="input-box">
<?php echo $this->getCountryHtmlSelect('billing') ?>
</div>
var billingRegionUpdater = new RegionUpdater('billing:country_id', 'billing:region', 'billing:region_id', <?php echo $this->helper('directory')->getRegionJson() ?>, undefined, 'billing:postcode');

Resources