简体   繁体   中英

SQL where condition is only receiving one word from the value of the field

I need to pass to a where condition a string instead of the id of the row, because of reasons. The problem is that the condition are somehow only catching the first word of the selected value. For example, look at this piece of code:

<select id="brand" name="brand" required>
    <option value="Lorem Ipsum Dolor" selected>Lorem Ipsum Dolor</option>
</select>

Then, by echoing the last_query() , the condition will show:

... WHERE `table.column` = `Lorem` ...

Instead of the whole value .

This is my query:

public function find_id_ano_modelo($marca, $modelo, $ano, $comb)
{
    $this->db
        ->select('ano_modelo.id')
        ->join('modelo', 'modelo.id = ano_modelo.id_modelo')
        ->join('marca', 'marca.id_marca = modelo.id_marca')
        ->where('marca.id_marca', $marca)
        ->where('modelo.modelo', $modelo)
        ->where('ano_modelo.ano', $ano)
        ->where('ano_modelo.combustivel', $comb)
        ->where('marca.tipo = 1');

    $query = $this->db->get($this->table);

    echo $this->db->last_query();

    if ($query) {
        foreach ($query->result_array() as $q) {
            return $q['id'];
        }
    }
}

That is being called in my edit method from my controller:

...
$marca = $this->input->post('id_marca');
$modelo = $this->input->post('id_modelo');
$ano = $this->input->post('ano');
$comb = $this->input->post('combustivel');

$data['id_ano_modelo'] = $this->Modelos_model->find_id_ano_modelo($marca, $modelo, $ano, $comb);
....

After discussing it in chat , the error was solved:

The request was being sent via an ajax call and the dropdown was built with:

modelo.append('<option value=' + v.modelo + '>' + v.modelo + '</option>');

The problem was the missing double-quotes around the value. Without them, each word of the value was transformed into an html property:

For this string:

QQ 1.0 ACT 12V 69cv 5p

This was generated:

<option value="QQ" 1.0="" act="" fl="" 12v="" 69cv="" 5p="">QQ 1.0 ACT FL 12V 69cv 5p</option>

Adding qoutes here solved it:

modelo.append('<option value="' + v.modelo + '">' + v.modelo + '</option>');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM