Codeigniter - Problema insertar datos a Mysql desde codeigniter

 
Vista:
sin imagen de perfil

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:

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
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de kip
Val: 53
Oro
Ha mantenido su posición en Codeigniter (en relación al último mes)
Gráfica de Codeigniter

Problema insertar datos a Mysql desde codeigniter

Publicado por kip (21 intervenciones) el 26/07/2017 00:53:00
Hola, los metodos de la clase Input ya sean get() o post() tratalos desde el controlador y luego envialos al metodo del modelo como parametros. No veo donde cargues el modelo, si se te olvido aquello, debes cargarlo para acceder a sus metodos.

Intentalo y nos cuentas.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Problema insertar datos a Mysql desde codeigniter

Publicado por Novato (3 intervenciones) el 26/07/2017 17:44:06
Buenas tardes,

No entiendo lo que quieres decir.

Te indico aquí todos los datos de los ficheros para ver si me puedes indicar.
Controlador:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
	/**
	* @author Iván
	*/
	class News extends CI_Controller{
 
		function __construct(){
			parent::__construct();
			$this->load->model('news_model');
		}
 
		public function index(){
			$data['news'] = $this->news_model->get_news();
			$data['title'] = 'Archivo de noticias';
 
			$this->load->view('templates/header', $data);
			$this->load->view('news/index', $data);
			$this->load->view('templates/header');
		}
 
		public function view($slug){
			$data['news_item'] = $this->news_model->get_news($slug);
 
			if (empty($data['news_item'])) {
				show_404();
			}
 
			$data['title'] = $data['news_item']['title'];
 
			$this->load->view('templates/header', $data);
			$this->load->view('news/view', $data);
			$this->load->view('templates/footer');
		}
 
		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->load->models('news_model');
 
				$sql = "Insert into news (id,title,slug,text) values (default,'" . $this->input->post('title') . "','" . $this->input->post('title') . "','" . $this->input->post('text') . "')";
				$return = $this->news_model->set_news($sql);
				if($return){
					redirect('news');
				}else{
					$this->load->view('templates/header', $data);
					$this->load->view('news/create');
					$this->load->view('templates/footer');
				}
				/*
				$this->news_model->set_news();
				$this->load->view('news/success');
				*/
			}
		}
	}
?>

Modelo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
	/**
	* @author Iván
	*/
	class News_model extends CI_Model
	{
 
		public function __construct(){
			$this->load->database();
		}
		public function get_news($slug = FALSE){
			if ($slug === FALSE) {
				$query = $this->db->get('news');
				return $query->result_array();
			}
			$query = $this->db->get_where('news', array('slug'=>$slug));
 
			return $query->row_array();
		}
		public function set_news($sql){
			$query = $this->db->query($sql);
        	return $query;
 
			/*
			$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);
			*/
		}
	}
?>


Vista:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!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/success') ?>
 
			<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' />
 
		<?php echo form_close() ?>
		<!--</form>-->
	</body>
</html>

Si me dices en base a esto lo que hay que modificar... el manual que sigo o da cosas por hecho o falta y no encuentro nada para solucionarlo.

Muchas gracias de antemano a todos y en este caso gracias Kip
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Problema insertar datos a Mysql desde codeigniter

Publicado por Novato (3 intervenciones) el 26/07/2017 18:15:23
CORRECCIÓN: EL COMENTARIO ANTERIOR SON MODIFICACIONES EL ORIGINAL ES ESTE (perdón por las mayúsculas es para que lo vieseis )
Buenas tardes,

No entiendo lo que quieres decir.

Te indico aquí todos los datos de los ficheros para ver si me puedes indicar.
Controlador:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
	/**
	* 
	*/
	class News extends CI_Controller{
 
		function __construct(){
			parent::__construct();
			$this->load->model('news_model');
		}
 
		public function index(){
			$data['news'] = $this->news_model->get_news();
			$data['title'] = 'Archivo de noticias';
 
			$this->load->view('templates/header', $data);
			$this->load->view('news/index', $data);
			$this->load->view('templates/header');
		}
 
		public function view($slug){
			$data['news_item'] = $this->news_model->get_news($slug);
 
			if (empty($data['news_item'])) {
				show_404();
			}
 
			$data['title'] = $data['news_item']['title'];
 
			$this->load->view('templates/header', $data);
			$this->load->view('news/view', $data);
			$this->load->view('templates/footer');
		}
 
		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->load->models('news_model');

				$sql = "Insert into news (id,title,slug,text) values (default,'" . $this->input->post('title') . "','" . $this->input->post('title') . "','" . $this->input->post('text') . "')";
				$return = $this->news_model->set_news($sql);
				if($return){ 
					redirect('news');
				}else{
					$this->load->view('templates/header', $data);
					$this->load->view('news/create');
					$this->load->view('templates/footer');
				}*/
 
				$this->news_model->set_news();
				$this->load->view('news/success');
 
			}
		}
	}
?>

Modelo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
	/**
	* 
	*/
	class News_model extends CI_Model
	{
 
		public function __construct(){
			$this->load->database();
		}
		public function get_news($slug = FALSE){
			if ($slug === FALSE) {
				$query = $this->db->get('news');
				return $query->result_array();
			}
			$query = $this->db->get_where('news', array('slug'=>$slug));
 
			return $query->row_array();
		}
		public function set_news($sql){
			/*$query = $this->db->query($sql);
			return $query;*/
 
			$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);
 
		}
	}
?>


Vista:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!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/success') ?>
 
			<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' />
 
		<!--<?php //echo form_close() ?>-->
		</form>
	</body>
</html>

Si me dices en base a esto lo que hay que modificar... el manual que sigo o da cosas por hecho o falta y no encuentro nada para solucionarlo.

Muchas gracias de antemano a todos y en este caso gracias Kip
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Problema insertar datos a Mysql desde codeigniter

Publicado por Pablo Dante (1 intervención) el 17/12/2020 16:01:30
Hola Novato.
Buen día
Estoy mirando tu caso y me pasa igual que a vos.
Hago todo lo que indica el ejercicio del manual y no hace nada ni siquiera un error.
Es como que nunca llega a ejecutar la función en el modelo donde hace el inser en el Tabla.
Lo pudiste resolver!!?
Y de ser así me podría indicar cómo lo logaste!!?
Un saludo cordial
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar