gpt4 book ai didi

php - Codeigniter 将数据插入数据库

转载 作者:行者123 更新时间:2023-11-29 13:06:22 24 4
gpt4 key购买 nike

无法将数据插入 mysql 数据库。我很困惑哪里的错误代码。我是 Codeigniter 的新手,无法真正弄清楚如何解决这个问题。

下面查看 tambah_berita.php

<form name="form" action="<?php echo base_url();?>index.php/admin/berita/tambah_berita" method="post">

<div class="two fields">
<div class="field">
<label>ID_Berita</label>
<div class="ui small left icon input">
<input type="text" placeholder="ID" name="id_berita">
<i class="text file outline icon"></i>
</div>
</div>
</div>

<div class="fours fields">
<div class="field">
<div class="ui vertical segment">
<div class="date field">
<label>Tanggal</label>
<div class="ui small icon input left">
<input type="text" placeholder="xx/xx/xxxx" name="tanggal">
<i class="calendar icon"></i>
</div>
</div>
</div>
</div>
</div>

<div class="two fields">
<div class="field">
<label>Judul</label>
<div class="ui small left icon input">
<input type="text" placeholder="Nama Profil" name="judul_berita">
<i class="text file outline icon"></i>
</div>
</div>
</div>

<div class="field">
<label>Isi Berita</label>
<textarea placeholder="Text" name="content"></textarea>
</div>

<input class="ui small blue submit button" name="submit" type="submit" value="Save">
<input class="ui small basic button" type="reset" value="Reset">
</form>

和模型 mberita.php

function get_berita()
{
$this->db->order_by('id_berita','asc');
$data = $this->db->get('berita_ukm');
return $data->result();
}

//untuk menambah berita
function insert_berita($data)
{
print_r($data);
$this->db->insert('berita_ukm', $data);
}

和 Controller berita.php

function index()
{
$this->data['berita'] = $this->mberita->get_berita();
//var_dump($this->mberita->get_berita());
$this->data['title'] ='UKM Taekwondo | berita';
$this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
$this->data['contents'] = $this->load->view('admin/berita/view_berita', $this->data, true);
$this->load->view('template/wrapper/admin/wrapper_ukm',$this->data);
}

function tambah_berita()
{

$this->form_validation->set_rules('id_berita', 'Id Berita', 'required|numeric');
$this->form_validation->set_rules('tanggal', 'Tanggal', 'required');
$this->form_validation->set_rules('judul_berita', 'Judul Berita', 'required');
$this->form_validation->set_rules('content', 'Content', 'required');

if ($this->form_validation->run() == FALSE)
{
$this->data['contents'] = $this->load->view('admin/berita/tambah_berita', '', true);

}else{

$this->load->model('mberita');

$data = array(
'id_berita' => $this->input->post('id_berita'),
'tanggal' => $this->input->post('tanggal'),
'judul_berita' => $this->input->post('judul_berita'),
'content' => $this->input->post('content')

);

$this->mberita->insert_berita($data);
}

$this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
$this->load->view('template/wrapper/admin/wrapper_ukm',$this->data);
}

请帮我看看该怎么办。谢谢

最佳答案

请使用这个表达方式:

echo form_open('admin/berita/tambah_berita');

而不是

<form name="form" action="<?php echo base_url();?>index.php/admin/berita/tambah_berita" method="post">

但在此之前,请在 config/autoload.php 中加载助手“表单”,或者您可以像这样手动加载 Controller :

$this->load->helper('form');

关于php - Codeigniter 将数据插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22654526/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com