views:

366

answers:

1

Hello There,

I am working on a bug(?) for a few hours now, but couln’t fix it.

This is my code:

 if(!$this->db->get_where('merken',array('m_merken' => $brand))->count_all_results()){

      $insetData = array('m_name' => $brand);
      $this->db->insert('merken', $insetData);

 }

$brand contains ‘Acer’ in this preview.

 A Database Error Occurred

    Error Number: 1054

    Unknown column ‘Acer’ in ‘where clause’

    SELECT * FROM (`merken`) WHERE `m_name` = Acer

I want to check if it already exists, but it won’t work very well.

A: 

Without quotes, your statement:

SELECT * FROM (`merken`) WHERE `m_name` = Acer

Acer refers to a column-name. If your intent is a string literal, put it in single-quotes, as in:

SELECT * FROM (`merken`) WHERE `m_name` = 'Acer'

Also, as a matter of good programming practice, avoid SELECT *, better to SELECT each column you want to return, even if the list is lengthy.

-- EDIT --

I suspect I'm missing the point... the SQL is generated. Two things to check:

Is m_name correctly declared as a string/varchar/char field? Failing that, try literally setting the brand name to 'Acer', with the quote marks. I doubt this is a reasonable solution, though.

Bob Kaufman
The m_name is declarated as a varchar(255), i've try this code too: if($this->db->get_where("merken",array("name" => '$brand'))->count_all_results() == 0){ $insetData = array("name" => '$brand'); $this->db->insert("merken", $insetData); echo "succes"; }else{ echo "already exists"; }But when I try that nothing returns, so there is something wrong but don't know what. No error and no succes/exists.
Kees
if($this->db->get_where("merken",array("name" => $brand))->count_all_results() == 0){ $insetData = array("name" => $brand); $this->db->insert("merken", $insetData);echo "gelukt"; }else{ echo "bestaat al?"; }
Kees
It's seems to work now, thanks! The singe-quotes and change the name m_name to 'name' (in the code and Database) has worked. Thanks for your time!
Kees