
Problema insertar datos a Mysql desde codeigniter
Publicado por Novato (3 intervenciones) el 23/07/2017 18:04:57
Buenas a todos,
Tengo los siguientes ficheros:
news.php que es un controlador con la función:
Después el modelo news_model.php con la función:
La vista create.php con el siguiente código:
Siguiendo un manual me he quedado ahí, ya que cuando pulso a crear me redirige a una página en blanco y no añade nada a la base de datos.
Soy novato en esto :(
Un saludo y muchas gracias de antemano.
Tengo los siguientes ficheros:
news.php que es un controlador con la función:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public function create(){
//se carga el helper form y bibliotecas de validación
$this->load->helper('form');
$this->load->library('form_validation');
//titulo
$data['title'] = 'Crear un item de noticias';
//reglas de validación para el formulario
$this->form_validation->set_rules('title', 'Titulo', 'required');
$this->form_validation->set_rules('text', 'Texto', 'required');
if ($this->form_validation->run() === FALSE) {
$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
}
else{
$this->news_model->set_news();
$this->load->view('news/success');
}
}
Después el modelo news_model.php con la función:
1
2
3
4
5
6
7
8
9
10
11
12
public function set_news(){
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text')
);
return $this->db->insert('news', $data);
}
La vista create.php con el siguiente código:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<h2>Crear un item de noticias</h2>
<?php echo validation_errors(); ?>
<?php echo form_open('news/create'); ?>
<label for='title'>Título</label>
<input type="input" name="title" /><br />
<label for="text">Texto</label>
<textarea name="text"></textarea><br />
<input type="submit" name="submit" value='Crear ítem de noticias' />
</form>
</body>
</html>
Siguiendo un manual me he quedado ahí, ya que cuando pulso a crear me redirige a una página en blanco y no añade nada a la base de datos.
Soy novato en esto :(
Un saludo y muchas gracias de antemano.
Valora esta pregunta


0