Welcome Guest, Not a member yet? Register   Sign In
[Fixed] Weird Database Problem
#1

[eluser]n8m[/eluser]
Hi all,

I'm just having a really weird database error. The problem occures when I try to put data from a post into my normal SQL Query. For example:

when i use:
Code:
$data1 = 'test';

$sql = "UPDATE tablename SET testcol1 ='{$data1}', testcol2='test2' where cond1='ok' and cond2='ok' ";

$this->db->query($sql);

works fine.

but as soon as I use the Post data:
Code:
$data1 = $_POST['test'];

$sql = "UPDATE tablename SET testcol1 ='{$data1}', testcol2='test2' where cond1='ok' and cond2='ok' ";

$this->db->query($sql);

it will only update testcol2.

any idea ?

Regards,
n8m
#2

[eluser]pistolPete[/eluser]
Is $_POST['test'] empty?

But besides that, you should not use the $_POST array for security reasons, rather use the Input Class:
Code:
$this->input->post('test')
#3

[eluser]n8m[/eluser]
Hey pistolPete,

thnx for your quick answer! $_POST['test'] is not empty. Also ... when I print out the actual query every field is filled. Oddly enough, when I use this query directly on the database, Its working fine - the only thing thats not working is the $this->db->query($sql);

Sad

R
n8m
#4

[eluser]pistolPete[/eluser]
So what is in your $_POST[‘test’] variable? anything wich might break the sql?
I don't know if it's a typo, but in the example above you were missing a double quote at the end...
#5

[eluser]n8m[/eluser]
Na, was a typo (thanks for pointing that out btw, fixed it now). I try to update something else and see if that is working.



Regards,
n8m
#6

[eluser]n8m[/eluser]
Nope ... looks like the other updates in the system are not working either. Can it be that this is something general ?
#7

[eluser]TheFuzzy0ne[/eluser]
What's with the curly brackets?

I would try something like this:
Code:
$this->db->where(array('cond1' => 'ok', 'cond2' => 'ok');
$this->db->update('table_name', array('testcol1' => $this->input->post('test'), 'testcol2' => 'test2'));
The code above is untested.
#8

[eluser]n8m[/eluser]
nope didn't work either. I guess I have some majour screw up somewhere in the app. I tested the model on a different app and its working fine there.
#9

[eluser]TheFuzzy0ne[/eluser]
The only thing that could stop it from working is if $_POST['test'] was empty. Are you validating this field to ensure it's there? Quite often, I find that a typo in the form itself can mess the whole thing up, for example, the name of the input in the form itself is named "test", but you thought you named it "test1", or you spelt "name" incorrectly, or missed the equals sign that follows, etc... Basically, check on the server side that the this information is being posted. Something like:
Code:
echo $this->input->post('test1');
should help, even if it temporarily breaks you application.
#10

[eluser]n8m[/eluser]
Here's a snippet where this thing also happens:

first i made a var_dump (for test reasons):
array(12) { ["firma2"]=> string(5) "Testd" ["strasse2"]=> string(4) "test" ["plz2"]=> string(4) "test" ["stadt2"]=> string(4) "test" ["tel2"]=> string(4) "test" ["fax2"]=> string(4) "test" ["email2"]=> string(4) "test" ["internet2"]=> string(4) "test" ["ansprech2"]=> string(0) "" ["ansprechkon2"]=> string(0) "" ["ansprechkon3"]=> string(0) "" ["ansprechkon1"]=> string(0) "" }


after that ... I use the db helper to do the following:

Code:
$data = array('firma2' => $this->input->post('firma2'));
$where = "id =".$shopid;
$update = $this->db->update_string('shops', $data, $where);
        
$this->db->query($update);

when i print out $update i get the following:
Code:
UPDATE `shops` SET `firma2` = 'Testd' WHERE id =14

Which works fine when I submit this exact query with phpMyAdmin f.e.

Dang, I think I either miss something completely obvious, or I found a bug (which is unlikely).




Theme © iAndrew 2016 - Forum software by © MyBB