Welcome Guest, Not a member yet? Register   Sign In
Help with variable
#1

[eluser]Unknown[/eluser]
Hello people!

1º) Sorry for my english, i'm a Brazilian developer!

2º) I have a problem with a damn variable. I need your help! Look it:

In first page(The view), my code is:

print '<td>Cidade</td><td>';
print form_dropdown('nomedacidade', $cidade);
print '</td></tr>';


And in the Model is:

$postado = $this->input->post('nomedacidade');

$query = $this->db->query( 'SELECT * FROM tb_cidade where nome = "'.$postado.'"');

foreach ($query->result() as $rowcidade)
{
$cidade = utf8_decode($rowcidade->id);
}

$this->db->insert( 'tb_lista', array( 'id' => NULL, 'nome' => $this->input->post ('nome'), 'sobrenome' => $this->input->post('sobrenome'),'ddd' => $this->input->post ('ddd'), 'fone' => $this->input->post( 'telefone' ), 'rua' => $this->input->post( 'endereco' ), 'numero' => $this->input->post( 'numero' ), 'cidade_id' => $cidade));


In this mode, the system return the error:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: cidade

Filename: models/listatelmodel.php

Line Number: 31

and

Error Number: 1048

Column 'cidade_id' cannot be null

INSERT INTO `tb_lista` (`id`, `nome`, `sobrenome`, `ddd`, `fone`, `rua`, `numero`, `cidade_id`) VALUES (NULL, 'Test', 'Test', '99', '99999999', 'xxxxxx xxxxx', '222', NULL)

Ok... But, if i set the variable $postado with a name of the city of the DB, ex:

$postado = "name_of_db";

The system inserts the data correctly!

How do I fix the error of the variable "$postado"? Thanks in advance!
#2

[eluser]pbreit[/eluser]
Possibly $query->result() is empty? I not sure what you are trying to do. If you want the insert() to run for each cidade, you need to put it in the foreach loop. I suspect what is happening is that the result is empty so "$cidade = utf8_decode($rowcidade->id);" never happens and thus $cidade is not declared resulting in the NULL value in the insert().

To make your code easier to read on the forum, use the [ code ] [/ code ] tags.




Theme © iAndrew 2016 - Forum software by © MyBB