Error: "You have an error in your SQL syntax"

0

The error that returns to me is:

A Database Error Occurred

Error Number: 1064

You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near '0) VALUES ('[email protected]', 'ASFKSFKFGA', 'Enviar',
'ASFKSFKFGA')' at line 1

INSERT INTO 'users' ('email', 'contraseña', 'btn_enviar', 0) VALUES
('[email protected]', 'ASFKSFKFGA', 'Enviar', 'ASFKSFKFGA')

Filename:
C:/xampp/htdocs/CodeIgniter-3.1.4/system/database/DB_driver.php

Line Number: 691 

In the model I have the following:

function add_new_users()
{
     $this->load->database();//carga base de datos
     $data_insertar=$this->input->post();
     //unset($data_insertar['btn_enviar']);
    $this->db->insert('users',$data_insertar);

in the controller the following:

public function add_user(){
    $this->load->helper('form');//helper para los formularios
    $this->load->library('form_validation');//cargar libreria de validaciones
    $this->load->model('User');
    if ($this->input->post()){//si ya hay datos cargados o se cargan
        $this->form_validation->set_rules('email','Email','required|valid_email');//que los datos ingresados en el campo sean los necesarios del campo de la base de datos, validar que lo se que se requiere existe..
        $this->form_validation->set_rules('contraseña','Contraseña','required|alpha_numeric|min_length[6]');
        if  ($this->form_validation->run()== TRUE){ //si las validaciones pasan y corren!
            $this->User->add_new_users();
            print_r($this->input->post());//para mostrar ls datos
        }
        else {//si fallan las validaciones
            echo "Esta ingresando algo malo";
            $this->load->view('formulari'); //aca envia del nuevo al formulario
        }
        else{
            $this->load->view('formulari');
        }
    }

and in the view I have:

<?php echo form_open() ?>
<form method="post" action="" accept-charset="">
<?php echo form_error('email'); ?></br>
<?php echo form_label('Email','lblemail') ?> 
<?php echo form_input($input_email) ?> </br>
<?php echo form_error('contraseña');?></br>
<?php echo form_label('Contraseña','lblcontra') ?>
<?php echo form_password($password_contraseña) ?> </br>
<?php echo form_submit ('btn_enviar','Enviar') ?>
<div id="boton">
<button type="submit" class="btn btn-default btn-sm">Enviar </button>
</div>
<?php echo form_close() ?>
    
asked by Angel Gomez 04.06.2017 в 03:29
source

2 answers

1

You can not send 0 as the name of a field in the table.

INSERT INTO 'users' ('email', 'contraseña', 'btn_enviar', 0)<--AQUI VALUES ('[email protected]', 'ASFKSFKFGA', 'Enviar', 'ASFKSFKFGA')
    
answered by 04.06.2017 в 06:45
0

Be careful with this function this->input->post() , because the inserts are done in a different order than you have planned or configured from the sending form or insertion method.

The function

this->input->post()

collects the parameters of the form within an associative array from the controller and its function public function add_user(){}

It would be like that.

$datos = array(
    'input_email' => $this-input->get_post('input_email'),
    'password' => $this-input->get_post('password')
); 
    
answered by 05.06.2017 в 01:19