A Database Error Occurred: 1048 cannot insert data - php

I cannot save my data, eventhough my database already same like the code... What must I do??
A Database Error Occurred Error Number: 1048
Column 'image' cannot be null
INSERT INTO gallery (id_gallery, name, image) VALUES
('5bba4390eb0b8', 'nnn', NULL)
Filename: C:/xampp/htdocs/eat/system/database/DB_driver.php
Line Number: 691
Controller: Gallery.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Gallery extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model("gallery_model");
$this->load->library('form_validation');
}
public function index()
{
$data["gallery"] = $this->gallery_model->getAll();
$this->load->view("admin/gallery/list", $data);
}
public function add()
{
$gallery = $this->gallery_model;
$validation = $this->form_validation;
$validation->set_rules($gallery->rules());
if ($validation->run()) {
$gallery->save();
$this->session->set_flashdata('success', 'Berhasil disimpan');
}
$this->load->view("admin/gallery/new_form");
}
public function edit($id = null)
{
if (!isset($id)) redirect('admin/gallery');
$gallery = $this->gallery_model;
$validation = $this->form_validation;
$validation->set_rules($gallery->rules());
if ($validation->run()) {
$gallery->update();
$this->session->set_flashdata('success', 'Berhasil disimpan');
}
$data["gallery"] = $gallery->getById($id);
if (!$data["gallery"]) show_404();
$this->load->view("admin/gallery/edit_form", $data);
}
public function delete($id=null)
{
if (!isset($id)) show_404();
if ($this->gallery_model->delete($id)) {
redirect(site_url('admin/gallery'));
}
}
}
Model: Gallery_model.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Gallery_model extends CI_Model
{
private $_table = "gallery";
public $id_gallery;
public $name;
public $image;
public function rules()
{
return [
['field' => 'name',
'label' => 'Name',
'rules' => 'required']
];
}
public function getAll()
{
return $this->db->get($this->_table)->result();
}
public function getById($id)
{
return $this->db->get_where($this->_table, ["id_gallery" => $id])->row();
}
public function save()
{
$post = $this->input->post();
$this->id_gallery = uniqid();
$this->name = $post["name"];
$this->image = $this->_uploadImage();
$this->db->insert($this->_table, $this);
}
public function update()
{
$post = $this->input->post();
$this->id_gallery = $post["id"];
$this->name = $post["name"];
if (!empty($_FILES["image"]["name"])) {
$this->image = $this->_uploadImage();
} else {
$this->image = $post["old_image"];
}
$this->db->update($this->_table, $this, array('id_gallery' => $post['id']));
}
public function delete($id)
{
$this->_deleteImage($id);
return $this->db->delete($this->_table, array("id_gallery" => $id));
}
private function _uploadImage()
{
$config['upload_path'] = './upload/gallery/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['file_name'] = $this->id_gallery;
$config['overwrite'] = true;
$this->load->library('upload', $config);
if ($this->upload->do_upload('image')) {
return $this->upload->data("file_name");
}
}
private function _deleteImage($id)
{
$gallery = $this->getById($id);
if ($gallery->image != "default.jpg") {
$filename = explode(".", $gallery->image)[0];
return array_map('unlink', glob(FCPATH."upload/gallery/$filename.*"));
}
}
}
In view there is 3 page: list.php, new_form.php, edit_form.php
list.php
<!DOCTYPE html>
<html lang="en">
<head>
<?php $this->load->view("admin/_partials/head.php") ?>
</head>
<body id="page-top">
<?php $this->load->view("admin/_partials/navbar.php") ?>
<div id="wrapper">
<?php $this->load->view("admin/_partials/sidebar.php") ?>
<div id="content-wrapper">
<div class="container-fluid">
<?php $this->load->view("admin/_partials/breadcrumb.php") ?>
<!-- DataTables -->
<div class="card mb-3">
<div class="card-header">
<i class="fas fa-plus"></i> Add New
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-hover" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Name</th>
<th>Photo</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($gallery as $gallery): ?>
<tr>
<td width="150">
<?php echo $gallery->name ?>
</td>
<td>
<img src="<?php echo base_url('upload/gallery/'.$gallery->image) ?>" width="64" />
</td>
<td width="250">
<a href="<?php echo site_url('admin/gallery/edit/'.$gallery->gallery_id) ?>"
class="btn btn-small"><i class="fas fa-edit"></i> Edit</a>
<a onclick="deleteConfirm('<?php echo site_url('admin/gallery/delete/'.$gallery->gallery_id) ?>')"
href="#!" class="btn btn-small text-danger"><i class="fas fa-trash"></i> Hapus</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- /.container-fluid -->
</div>
<!-- /.content-wrapper -->
</div>
<!-- /#wrapper -->
<?php $this->load->view("admin/_partials/scrolltop.php") ?>
<?php $this->load->view("admin/_partials/modal.php") ?>
<?php $this->load->view("admin/_partials/js.php") ?>
<script>
function deleteConfirm(url){
$('#btn-delete').attr('href', url);
$('#deleteModal').modal();
}
</script>
</body>
</html>
new_form.php
<!DOCTYPE html>
<html lang="en">
<head>
<?php $this->load->view("admin/_partials/head.php") ?>
</head>
<body id="page-top">
<?php $this->load->view("admin/_partials/navbar.php") ?>
<div id="wrapper">
<?php $this->load->view("admin/_partials/sidebar.php") ?>
<div id="content-wrapper">
<div class="container-fluid">
<?php $this->load->view("admin/_partials/breadcrumb.php") ?>
<?php if ($this->session->flashdata('success')): ?>
<div class="alert alert-success" role="alert">
<?php echo $this->session->flashdata('success'); ?>
</div>
<?php endif; ?>
<div class="card mb-3">
<div class="card-header">
<i class="fas fa-arrow-left"></i> Back
</div>
<div class="card-body">
<form action="<?php base_url('admin/gallery/add') ?>" method="post" enctype="multipart/form-data" >
<div class="form-group">
<label for="name">Title*</label>
<input class="form-control <?php echo form_error('name') ? 'is-invalid':'' ?>"
type="text" name="name" placeholder="gallery name" />
<div class="invalid-feedback">
<?php echo form_error('name') ?>
</div>
</div>
<div class="form-group">
<label for="image">Photo</label>
<input class="form-control-file <?php echo form_error('image') ? 'is-invalid':'' ?>"
type="file" name="image"/>
<div class="invalid-feedback">
<?php echo form_error('image') ?>
</div>
</div>
<input class="btn btn-success" type="submit" name="btn" value="Save" />
</form>
</div>
<div class="card-footer small text-muted">
* required fields
</div>
</div>
</div>
</div>
</div>
<?php $this->load->view("admin/_partials/scrolltop.php") ?>
<?php $this->load->view("admin/_partials/js.php") ?>
</body>
</html>
edit_form.php
<!DOCTYPE html>
<html lang="en">
<head>
<?php $this->load->view("admin/_partials/head.php") ?>
</head>
<body id="page-top">
<?php $this->load->view("admin/_partials/navbar.php") ?>
<div id="wrapper">
<?php $this->load->view("admin/_partials/sidebar.php") ?>
<div id="content-wrapper">
<div class="container-fluid">
<?php $this->load->view("admin/_partials/breadcrumb.php") ?>
<?php if ($this->session->flashdata('success')): ?>
<div class="alert alert-success" role="alert">
<?php echo $this->session->flashdata('success'); ?>
</div>
<?php endif; ?>
<!-- Card -->
<div class="card mb-3">
<div class="card-header">
<a href="<?php echo site_url('admin/gallerys/') ?>"><i class="fas fa-arrow-left"></i>
Back</a>
</div>
<div class="card-body">
<form action="<?php base_url(" admin/gallery/edit") ?>" method="post"
enctype="multipart/form-data" >
<input type="hidden" name="id" value="<?php echo $gallery->gallery_id?>" />
<div class="form-group">
<label for="name">Name*</label>
<input class="form-control <?php echo form_error('name') ? 'is-invalid':'' ?>"
type="text" name="name" placeholder="gallery name" value="<?php echo $gallery->name ?>" />
<div class="invalid-feedback">
<?php echo form_error('name') ?>
</div>
</div>
<div class="form-group">
<label for="name">Photo</label>
<input class="form-control-file <?php echo form_error('image') ? 'is-invalid':'' ?>"
type="file" name="image" />
<input type="hidden" name="old_image" value="<?php echo $gallery->image ?>" />
<div class="invalid-feedback">
<?php echo form_error('image') ?>
</div>
</div>
<input class="btn btn-success" type="submit" name="btn" value="Save" />
</form>
</div>
<div class="card-footer small text-muted">
* required fields
</div>
</div>
<!-- /.container-fluid -->
</div>
<!-- /.content-wrapper -->
</div>
<!-- /#wrapper -->
<?php $this->load->view("admin/_partials/scrolltop.php") ?>
<?php $this->load->view("admin/_partials/js.php") ?>
</body>
</html>

Related

Foreach Loop in a view doesn't work in CI3 view

Hi I'm trying to do foreach loop on a view but it's not showing. I've checked the database and there's data. I've checked the QueryBuilder on the model and run the query on phpmyadmin and its working. Do you guys know what happened?
view:
<div class="content-wrapper">
<section class="content">
<div class="row">
<?php
foreach ($node as $d) {
?>
<div class="col-lg-6 col-md-6 col-xs-6">
<div class="box box-device box-solid" id="device_<?php echo $d['id_device']; ?>">
<div class="box-header">
<div class="row">
<div class="col-sm-3 col-xs-3">
<button aria-pressed="false" data-device="<?php echo $d['id_device']; ?>" class="btn btn-onoff btn-sm btn-toggle<?php echo ($d['status_device'] == 1) ? ' active': ''; ?>" data-toggle="button" type="button">
<div class="handle"></div>
</button>
</div>
<div class="col-sm-9 col-xs-9">
<h3 class="box-title<?php echo ($d['rule'] == 0) ? ' notactive': ''; ?>"><?php echo $d['nama_device']; ?></h3>
</div>
</div>
</div>
<div class="box-body text-center" onclick="window.location.href = '<?php echo site_url('Setting/rule/' . $d['id_device']); ?>'">
<img alt="<?php echo $d['nama_device']; ?>" src="<?php echo base_url('uploads/device/' . $d['foto']); ?>" />
<h4><?php echo $d['nama_device']; ?></h4>
<p><?php echo $d['id_device']; ?></p>
</div>
<div class="box-footer">
<a class="btn btn-default btn-block btn-lg btn-detail" href="<?php echo site_url('Setting/rule/' . $d['id_device']); ?>">View Rule</a>
</div>
</div>
</div>
<?php
}
?>
</div>
</section>
<?php $this->load->view('components/version'); ?>
</div>
Controller:
public function rulenode() {
$this->data['sub_title'] = 'Setting - Rule';
$this->load->model('Mrule');
$this->data['node'] = $this->Mrule->device();
$this->load->view('setting/rulenode', $this->data);
}
Model:
public function device() {
$this->db->select('id_device, nama_device, foto, status_device');
$this->db->from('tb_device');
$this->db->join('arduino_rule', 'id_device = id_node');
return $this->db->get()->result_array();
// var_dump($this->db->last_query());
}
<?php
$node = $this->db->get('table_name');
foreach ($node as $d) {
?>

Can't update mysql php

I'm trying to update data from mysql database use php.
I can get all data from db into table, then I click edit and I can get the data, but when I edit the form and click the submit button it's always said that the field is empty. Or sometimes it's edited into "blank row" on mysql db
heres the code:
ktgr_soal.php
<?php
include('../koneksi/koneksi.php');
if ((isset($_GET['aksi'])) && (isset($_GET['data']))) {
if ($_GET['aksi'] == 'hapus') {
$kode_ktgr = $_GET['data'];
//hapus data
$sql_ktgr = "delete from `kategori_soal`
where `id_kategori` = '$kode_ktgr'";
mysqli_query($koneksi, $sql_ktgr);
}
}
?>
<head>
<?php include("includes/head.php") ?>
</head>
<body>
<!-- S I D E B A R -->
<?php include("includes/sidebar.php") ?>
<!-- ENDS I D E B A R -->
<?php include("includes/header.php") ?>
<div class="content-wrap">
<div class="main">
<div class="container-fluid">
<div class="row">
<div class="col-lg-8 p-r-0 title-margin-right">
<div class="page-header">
<div class="page-title">
<h1>Data Kategori Soal</h1>
</div>
</div>
</div>
<!-- /# column -->
<div class="col-lg-4 p-l-0 title-margin-left">
<div class="page-header">
<div class="page-title">
<ol class="breadcrumb">
<li class="breadcrumb-item">Dashboard</li>
<li class="breadcrumb-item active">Kategori Soal</li>
</ol>
</div>
</div>
</div>
<!-- /# column -->
</div>
<!-- /# row -->
<!-- M A I N C O N T E N T -->
<section id="main-content">
<div class="row">
<div class="col-lg-12">
<div class="card">
<div class="card-title">
<a href="add_kategori.php">
<button type="button" class="btn btn-primary btn-flat btn-addon m-b-10 m-l-5 float-right"><i class="ti-plus"></i>Tambah Data</button>
</a>
<!-- Search form -->
<form class="form-inline d-flex md-form form-sm">
<input class="form-control form-control-sm mr-3 w-25" type="text" placeholder="Search" aria-label="Search">
<i class="ti-search" aria-hidden="true"></i>
</form>
</div>
<div class="card-body">
<div class="col-sm-12">
<?php if (!empty($_GET['notif'])) { ?>
<?php if ($_GET['notif'] == "tambahberhasil") { ?>
<div class="alert alert-success" role="alert">
Data Berhasil Ditambahkan</div>
<?php } else if ($_GET['notif'] == "editberhasil") { ?>
<div class="alert alert-success" role="alert">
Data Berhasil Diubah</div>
<?php } ?>
<?php } ?>
</div>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>No</th>
<th>Kategori Soal</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
$batas = 10;
if (!isset($_GET['halaman'])) {
$posisi = 0;
$halaman = 1;
} else {
$halaman = $_GET['halaman'];
$posisi = ($halaman - 1) * $batas;
}
?>
<?php include('../koneksi/koneksi.php') ?>
<?php
//menampilkan data hobi
$sql_ktgr = "SELECT * FROM `kategori_soal` ";
if (isset($_GET["katakunci"])) {
$katakunci_jurusan = $_GET["katakunci"];
$sql_ktgr .= " where `kategori` LIKE '%$katakunci_jurusan%'";
}
$sql_ktgr .= " order by `kategori` limit $posisi, $batas ";
$query_ktgr = mysqli_query($koneksi, $sql_ktgr);
$no = $posisi + 1;
while ($data_ktgr = mysqli_fetch_row($query_ktgr)) {
$kode_ktgr = $data_ktgr[0];
$kategori = $data_ktgr[1];
?>
<tr>
<th scope="row"><?php echo $no; ?></th>
<td><?php echo $kategori; ?></td>
<td>
<a href="edit_kategori.php?data=<?php echo $kode_ktgr; ?>">
<button type="button" class="btn btn-warning btn-sm m-b-10 m-l-5 "><i class="ti-pencil-alt"></i></button>
</a>
<a href="javascript:if(confirm('Anda yakin ingin menghapus data <?php echo $kategori; ?>?'))
window.location.href = 'ktgr_soal.php?aksi=hapus&data=<?php echo $kode_ktgr; ?>'" class="btn btn-danger btn-sm m-b-10 m-l-5"><i class="ti-trash"></i>
</a>
</td>
</tr>
<?php
$no++;
} ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- /# column -->
<!-- FOOTER -->
<?php include("includes/footer.php") ?>
</section>
</div>
</div>
</div>
<!-- script -->
<?php include("includes/script.php") ?>
</body>
</html>
edit_kategori.php
<?php
session_start();
include('../koneksi/koneksi.php');
if (isset($_GET['data'])) {
$kode_kat = $_GET['data'];
$_SESSION['kode_ktgr'] = $kode_kat;
//get data kategori soal
$sql_d = "select `kategori` from `kategori_soal` where `id_kategori` = '$kode_kat'";
$query_d = mysqli_query($koneksi, $sql_d);
while ($data_d = mysqli_fetch_row($query_d)) {
$kategori = $data_d[0];
}
}
?>
<head>
<?php include("includes/head.php") ?>
</head>
<body>
<!-- S I D E B A R -->
<?php include("includes/sidebar.php") ?>
<!-- ENDS I D E B A R -->
<?php include("includes/header.php") ?>
<div class="content-wrap">
<div class="main">
<div class="container-fluid">
<div class="row">
<div class="col-lg-8 p-r-0 title-margin-right">
<div class="page-header">
<div class="page-title">
<h1>Edit Kategori Soal</h1>
</div>
</div>
</div>
<!-- /# column -->
<div class="col-lg-4 p-l-0 title-margin-left">
<div class="page-header">
<div class="page-title">
<ol class="breadcrumb">
<li class="breadcrumb-item">Dashboard</li>
<li class="breadcrumb-item active">Home</li>
</ol>
</div>
</div>
</div>
<!-- /# column -->
</div>
<!-- /# row -->
<!-- M A I N C O N T E N T -->
<section id="main-content">
<div class="row">
<div class="col-lg-12">
<div class="card">
<div class="card-title">
<h3>Edit Kategori Soal </h3>
</div>
<div class="card-body">
<div class="basic-form">
<?php
if (!empty($_GET['notif'])) {
if ($_GET['notif'] == "editkosong") {
?>
<div class="alert alert-danger" role="alert">Maaf data wajib di isi</div>
<?php
}
}
?>
<form action="konf_edit_ktgr.php" action="post">
<div class="form-group">
<label for="kategori">Kategori</label>
<input type="text" id="kategori" name="kategori" class="form-control input-focus" value="<?php echo $kategori; ?>">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
</div>
</div>
<!-- /# column -->
<!-- FOOTER -->
<?php include("includes/footer.php") ?>
</section>
</div>
</div>
</div>
<!-- script -->
<?php include("includes/script.php") ?>
</body>
</html>
konf_edit_ktgr.php
<?php
session_start();
include('../koneksi/koneksi.php');
if (isset($_SESSION['kode_ktgr'])) {
$kode_kat = $_SESSION['kode_ktgr'];
$kategori = $_POST['kategori'];
if (empty($kategori)) {
header("Location:edit_kategori.php?data=" . $kode_kat . "&notif=editkosong");
} else {
$sql = "update `kategori_soal` set `kategori` = '$kategori' where `id_kategori` = '$kode_kat'";
mysqli_query($koneksi, $sql);
header("Location:ktgr_soal.php?notif=editberhasil");
}
}
try to check again "$kode_kat" which is in the file "konf_edit_ktgr.php" whether you have got the value or not. If not, insert "$kode_kat" in the Edit form like this:
<form action="konf_edit_ktgr.php" action="post">
<div class="form-group">
<label for="kategori">Kategori</label>
<input type="text" id="kategori" name="kategori" class="form-control input-focus" value="<?php echo $kategori; ?>">
</div>
<input type="hidden" name="id" value="<?php echo $kode_kat; ?>">
<button type="submit" class="btn btn-primary">Submit</button>
</form>

why every user can see Chat conversations in codeigniter

I have programmed a simple Chat conversations between 2 users , but the problem is every one can see the conversation , like all the users in the same rooms , but me i want just the conversation private between 2 users like facebook . this is a capture for my script .
my entire code of controller :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Chat extends CI_Controller {
Public function __construct() {
parent::__construct();
$this->load->model('chats');
$this->load->model('regis');
}
public function index() {
}
public function send() {
date_default_timezone_set('Asia/Jakarta');
$date = date('Y-m-d H:i:s');
$message = array(
'sender' => $this->session->userdata('user'),
'time' => $date,
'text' => $this->input->post('message')
);
$this->db->insert('chat', $message);
redirect (base_url('chat'));
}
public function open() {
return $this->chats->main(array('status'=>TRUE));
}
public function maintenance() { return $this->chats->main(array('status'=>FALSE)); }
public function pending() {
if ($this->session->userdata('sesi') == FALSE) {
$this->session->set_flashdata('login', 'You Must Login!');
redirect(base_url());
} else {
$data['orang'] = $this->regis->orang();
$data['status'] = $this->chats->get_stats()->result();
$this->load->view('header');
$this->load->view('pending', $data);
$this->load->view('footer');
}
}
}
my models:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Chats extends CI_Model {
public function __construct() { $this->load->database(); }
public function isi_chat(){ return $this->db->select('*')->order_by('time','ASC')->get('chat'); }
public function main($status) {
$this->db->update('status', $status);
redirect(base_url('chat'));
}
public function get_stats() { return $this->db->get('status'); }
}
my view :
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<br><br><br>
<?= $this->session->flashdata('done'); ?>
<div class="panel panel-default">
<?php foreach($status as $s): ?>
<div class="panel-heading">
<strong><?= $this->session->userdata('nama'); ?></strong>
Exit
<?php
if ($this->session->userdata('akses') == 'ADMIN') {
echo "<i class=\"glyphicon glyphicon-user\"></i> User Pending";
if($s->status == TRUE) {
echo "<i class=\"glyphicon glyphicon-lock\"></i> Maintenance";
} else {
echo "<i class=\"glyphicon glyphicon-ok\"></i> Buka Chat";
}
}
?>
</div>
<?php endforeach ?>
<?php if ($s->status == TRUE): ?>
<div class="panel-body" style="height: 300px; overflow-y: scroll">
<?php foreach ($chat as $c){ ?>
<?php if($c->sender == $this->session->userdata('user')){ ?>
<div class="col-md-12">
<div class="panel panel-success panel-comment pull-right">
<div class="panel-heading" >
<strong style="opacity: .5; font-size: 12px; color: #4BD239">ME : </strong>
<small><?php echo date("d-M-Y H:i:s", strtotime($c->time)); ?></small><br/>
<?= $c->text ?>
</div>
</div>
</div>
<?php } else { ?>
<div class="col-md-12">
<div class="panel panel-warning panel-comment pull-left">
<div class="panel-heading" >
<strong style="opacity: .5; font-size: 12px; color: #DCD15B"><?= $c->sender ?>:</strong>
<small><?php echo date("d-M-Y H:i:s", strtotime($c->time)); ?></small><br/>
<?= $c->text ?>
</div>
</div>
</div>
<?php } ?>
<?php } ?>
</div>
<?php endif ?>
<?php if ($s->status == FALSE): ?>
<div class="panel-body">
<h4 class="text-center" style="color: #FF0000">SORRY<br> MAINTENANCE<br><br></h4>
</div>
<?php endif ?>
</div>
<?php if ($s->status == TRUE): ?>
<div class="row">
<div class="col-md-12 ">
<form method="post" action="chat/send">
<div class="col-md-12">
<div class="input-group">
<input type="text" name="message" class="form-control" placeholder="Masukan Teks">
<span class="input-group-btn">
<input class="btn btn-success" type="submit" value="Send">
</span>
</div>
</div>
</form>
</div>
</div>
<?php endif ?>
</div>
</div>
</div>
now the problem is i want the conversation private between 2 users ! can some one help me please because it's more than 5 days i try to solve this problem without any result . thank you

How to get the exact value from function shuffle in codeigniter?

Hello im just a newbie in codeigniter and i just want to ask on how to fix this problem...
i just want to make a simple quiz system and i want to shuffle all the questions from my database and display it.. the problem is when i compare the questions choices it gives me a value of unshuffle how can i solve this?
this is my controller to display my questions
public function quiz()
{
if(isset($_SESSION['username'])) {
$this->load->model('quizmodel');
$this->data['questions'] = $this->quizmodel->getQuestions();
$this->load->view('client/quiz', $this->data);
}else{
$this->load->view('home');
}
}
this is the getQuestions function from my quizmodel
public function getQuestions()
{
$this->db->select("cropscience_id, question, choice1, choice2, choice3, answer");
$this->db->from("cropscience");
$query = $this->db->get();
return $query->result();
$num_data_returned = $query->num_rows;
if ($num_data_returned < 1) {
echo "There is no data in the database";
exit();
}
}
this is my quiz view
<div class="panel-body">
<form method="post" action="<?php echo base_url();?>index.php/client_controller/resultdisplay">
<?php shuffle($questions); ?>
<?php foreach($questions as $row) { ?>
<?php $ans_array = array($row->choice1, $row->choice2, $row->choice3, $row->answer);
shuffle($ans_array); ?>
<div class="alert alert-success">
<p><?=$row->question?></p>
<br>
<div class="radio radio-success radio-inline">
<input type="radio" name="<?php echo $row->cropscience_id ?>" value="<?=$ans_array[0]?>" required>
<label for="inlineRadio1"> <?=$ans_array[0]?> </label>
</div>
<div class="radio radio-success radio-inline">
<input type="radio" name="<?php echo $row->cropscience_id ?>" value="<?=$ans_array[1]?>">
<label for="inlineRadio1"> <?=$ans_array[1]?> </label>
</div>
<div class="radio radio-success radio-inline">
<input type="radio" name="<?php echo $row->cropscience_id ?>" value="<?=$ans_array[2]?>">
<label for="inlineRadio1"> <?=$ans_array[2]?> </label>
</div>
<div class="radio radio-success radio-inline">
<input type="radio" name="<?php echo $row->cropscience_id ?>" value="<?=$ans_array[3]?>">
<label for="inlineRadio1"> <?=$ans_array[3]?> </label>
</div>
</div>
<?php } ?>
<div align="center" >
<div class="btn btn-primary btn-rounded">
<i class="fa fa-check"></i><input class="btn btn-primary btn-rounded" type="submit" value="Submit!">
</div>
</div>
</form>
</div>
this is my resultdisplay function from my controller
public function resultdisplay()
{
if(isset($_SESSION['username'])) {
$this->load->model('quizmodel');
$qID = $this->quizmodel->getQuizID();
$this->data['checks'] = $this->input->post($qID);
$this->load->model('quizmodel');
$this->data['results'] = $this->quizmodel->resultsScore();
$this->load->view('client/result_display', $this->data);
}else{
$this->load->view('home');
}
}
this is my result_display view
div class="wrapper wrapper-content">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="panel panel-primary">
<div class="panel-heading">
<h1 style="color: white;">Results</h1>
</div>
<div class="panel-body">
<?php $score = 0; ?>
<?php $array1= array(); ?>
<?php $array2= array(); ?>
<?php $array3= array(); ?>
<?php $array4= array(); ?>
<?php $array5= array(); ?>
<?php $array6= array(); ?>
<?php $array7= array(); ?>
<?php $array8= array(); ?>
<?php $count = 0; ?>
<?php foreach($checks as $checkans) { ?>
<?php echo $checkans; ?>
<?php $array1[$count] = $checkans;
$count++; ?>
<?php }?>
<br><br>
<?php foreach($results as $res) { ?>
<?php $array2[] = $res->answer;
$array3[] = $res->cropscience_id;
$array4[] = $res->question;
$array5[] = $res->choice1;
$array6[] = $res->choice2;
$array7[] = $res->choice3; ?>
<?php } ?>
<?php for ($x=0; $x <= $array3[$x]; $x++) { ?>
<?php echo $array4[$x]; ?>
<?php if ($array2[$x] != $array1[$x]) { ?>
<div class="alert alert-danger">
<p><i class="fa fa-times"></i></p>
<p><span style="background-color: #FF9C9E"><?=$array1[$x]?></span></p>
<p><span style="background-color: #ADFFB4"><?=$array2[$x]?></span></p>
</div>
<?php } else { ?>
<div class="alert alert-success">
<p><i class="fa fa-check"></i></p>
<p><span style="background-color: #ADFFB4"><?=$array1[$x]?></span></p>
</div>
<?php $score = $score + 1 ?>
<?php } ?>
<?php } ?>
<div align="center">
<input type="hidden" name="score" value="<?=$score?>">
<input type="button" class="btn btn-primary" data-toggle="modal" data-target="#scoremodal" value="View Your Score">
<!-- Score Modal Body -->
<div class="modal inmodal fade" id="scoremodal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-body" align="center">
<h2>Your Score is: </h2>
<h1><?=$score?>/100</h1>
</div>
<div class="modal-footer">
<?php echo form_open('client_controller/save_score'); ?>
<form method="get">
<div align="center">
<input type="hidden" name="score" value="<?=$score?>">
<input type="submit" class="btn btn-primary" value="Ok">
</div>
</form>
<?php echo form_close(); ?>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
this is my getQuizID() function model
public function getQuizID()
{
$this->db->select("cropscience_id");
$this->db->from("cropscience");
$query = $this->db->get();
}
this is my resultsScore() function model
public function resultsScore()
{
$this->db->select("cropscience_id, question, choice1, choice2, choice3, answer");
$this->db->from("cropscience");
$query = $this->db->get();
return $query->result();
$num_data_returned = $query->num_rows;
}
please help thank you
make a query like this:
$this->db->order_by('id', 'RANDOM');
or
$this->db->order_by('rand()');
$query = $this->db->get('cropscience');
return $query->result_array();

display image to view from url database using code igniter getting errors

Hey guys I'm trying to display and image thats in my database but getting an error. The wired part about it if i upload the image it uploads ok and the image shows and the errors goes away. please help me get rid of the errors for sure. I think the problem is with sessions but i don't know please have a look.
This is my profile_view:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="<?php echo base_url('css/mystyle.css'); ?>" type="text/css"/>
<link rel="stylesheet" type="text/css" href="<? echo base_url('assets/css/mystyle.css');?>" />
<link rel="stylesheet" href="<?php echo base_url()?>assets/css/mystyle.css" type="text/css">
<?php
$autoload['helper'] = array('css_js');?>
</head>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<div class="mascot">
<a class="navbar-brand" href="#">
<img src="<?php echo base_url()?>assets/img/monstercode.png" alt="Green Monster Mascot" style="width:160px;height:32px;"></a>
</div>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li>Projects</li>
<li>Developers</li>
<li>Employers</li>
<li>Contact</li>
<?php if ($this->session->userdata('login')){ ?>
<li class="right nav navbar-nav navbar-right"><p class="navbar-text">Hello <?php echo $this->session->userdata('uname'); ?></p></li>
<li class="right nav navbar-nav navbar-right">Log Out</li>
<?php } else { ?>
<li>Login</li>
<li>Signup</li>
<?php } ?>
</ul>
</div>
</div>
</nav>
<div class="container-fluid text-center">
<div class="row content">
<div class="col-sm-2 sidenav">
<div class="well">
<p>User Picture <br/><br/><br/><br/><br/><br/><br/><br/><br/></p>
</div>
<?php echo form_open_multipart('profile/do_upload'); ?>
<?php echo form_upload('userfile'); ?><br />
<?php echo form_submit('upload', 'Upload');?>
<?php echo form_close(); ?>
<div class="well">
<p>User Info<br/> Name: <?php echo $uname; ?><br/> Email: <?php echo $uemail; ?> <br/><br/><br/><br/><br/><br/><br/><br/><br/></p>
</div>
</div>
<div class="col-sm-8 text-left">
<div class="container">
<div class="row">
<div class="col-md-4">
<h4>Profile Summary</h4>
<hr/>
<form method="post" action="<?php echo site_url('profile/update');?>">
<p>Name: <input type="text" name='name'value="<?php echo $uname; ?>"></p>
<p>Email: <input type="text" name='email' value="<?php echo $uemail; ?>"></p>
<button type="submit" class="btn btn-default">Update</button>
</form>
<form method="post" action="<?php echo site_url('profile/delete');?>">
<button type="submit" class="btn btn-default">Delete</button>
</form>
<p>...</p>
</div>
<div class="col-md-8">
<p>lorem ipsum dolum</p>
<p>lorem ipsum dolum</p>
<p>lorem ipsum dolum</p>
<p>...</p>
</div>
</div>
</div>
<hr>
<h3>More User Info</h3>
<p>Lorem ipsum...</p>
</div>
<div class="col-sm-2 sidenav">
<div class="well">
<p>Job Offers <br/><br/><br/><br/><br/><br/><br/><br/><br/></p>
</div>
<div class="well">
<p>Job Offers <br/><br/><br/><br/><br/><br/><br/><br/><br/></p>
</div>
</div>
</div>
</div>
<img alt="Your uploaded image" src="<?=base_url(). 'assets/img/' . $upload_data['file_name'];;?>">
<script type="text/javascript" src="<?php echo base_url("assets/js/jquery-1.10.2.js"); ?>"></script>
<script type="text/javascript" src="<?php echo base_url("assets/js/bootstrap.js"); ?>"></script>
</body>
</html>
This is my profile controller:
<?php
class Profile extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('url', 'html', 'form'));
$this->load->library(array('session', 'form_validation'));
$this->load->database();
$this->load->model('user_model');
}
function index()
{
$details = $this->user_model->get_user_by_id($this->session->userdata('uid'));
$data['uname'] = $details[0]->firstname . " " . $details[0]->lastname;
$data['uemail'] = $details[0]->email;
$this->load->view('profile_view', $data);
}
function delete()
{
$this->user_model->delete_row($this->session->userdata('uid'));
redirect('Site/index');
}
function update()
{
$this->user_model->update($this->input->post("name"), $this->session->userdata('uid'));
$this->user_model->update($this->input->post("email"), $this->session->userdata('uid'));
$this->user_model->add_image($this->input->post('userfile'), $this->session->userdata('uid'));
redirect('profile/index');
}
public function index1()
{
$this->load->view('profile_view', array('error' => ' '));
}
public function do_upload()
{
$config['upload_path'] = './assets/img';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$details = $this->user_model->get_user_by_id($this->session->userdata('uid'));
$data['uname'] = $details[0]->firstname . " " . $details[0]->lastname;
$data['uemail'] = $details[0]->email;
if (!$this->upload->do_upload('userfile')) {
$data['error'] = $this->upload->display_errors();
$this->load->view('profile_view', $data);
} else {
$upload_data = $this->upload->data();
$images_name = $upload_data['file_name'];
//update user photo intro database
$this->user_model->add_image($images_name, $this->session->userdata('uid'), $upload_data['file_name']);
$data['upload_data'] = $upload_data;
$data['uid'] = $this->session->userdata('uid');
$this->load->view('profile_view', $data);
}
}
}
This is my user model:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class user_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
function get_user($email, $password)
{
$this->db->where('email', $email);
$this->db->where('password', $password);
$query = $this->db->get('user');
/* Select * from users where email = $_Post['email'] and password = $_post['password'] */
return $query->result();
}
// get user
function get_user_by_id($id)
{
$this->db->where('id', $id);
//select id from users where id = $_Post['id']
$query = $this->db->get('user');
return $query->result();
}
// insert
function insert_user($data)
{
return $this->db->insert('user', $data);
}
//delete row
function delete_row($id)
{
$this->db->where('id', $id);
$this->db->delete('user');
}
function update($uemail, $id)
{
$this->db->where('id', $id);
$this->db->set('email', $uemail);
$this->db->update('user');
}
function add_image($uimage, $id){
$this->db->where('id', $id);
$this->db->set('image', $uimage);
$this->db->update('user');
}
}
?>
update: this is the error please check link http://imgur.com/gFC7A6q
It's telling you that $upload_data['file_name'] doesn't exist.
Which I would assume is because you have not uploaded anything, then when you do - it exists and all is good.
So, you could swap out
<img alt="Your uploaded image" src="<?=base_url(). 'assets/img/' . $upload_data['file_name'];;?>">
for:
<?php if(isset( $upload_data['file_name'] )){ ?>
<img alt="Your uploaded image" src="<?=base_url(). 'assets/img/' . $upload_data['file_name'];?>">
<?php } ?>
To check if it has been set before trying to apply it.
Found the problem this should fix it in profile controller :)
function index()
{
if (!isset($details[0])){
$details = $this->user_model->get_user_by_id($this->session->userdata('uid'));
$data['uname'] = $details[0]->firstname . " " . $details[0]->lastname;
$data['uemail'] = $details[0]->email;
$this->load->view('profile_view', $data);}

Categories