Page 1 of 1

MySQL problem

Posted: Mon Jun 26, 2006 3:49 am
by AshrakTheWhite
Hello Gents :)

I have a small problem with a SQL query it says Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING


Heres the query:

Code: Select all

public function addUser($arr)
    {
        $sql = "INSERT INTO 
              'user'
                ('LOCKED', 'LASTNAME', 'FORNAME', 'USERNAME', 'PASSWORD', 'ACCESS', 'ACTIVE', 'EMAIL', 'PHONE', 'JOB', 'UNTAXED_MINIMUM', 'UNEMPLOYMENT_INSURANCE', 'PENSION', 'IDCODE', 'BANK', 'ACCOUNT')
              VALUES(\"0\", $arr['lname'], $arr['name'], $arr['username'], $arr['password'], $arr['access'], \"1\", $arr['email'], $arr['phone'], $arr['job'], $arr['min'], $arr['un'], $arr['pens'], $arr['idcode'], $arr['bank'], $arr['account']);";
    }

Posted: Mon Jun 26, 2006 4:37 am
by Charles256

Code: Select all

<?php
$sql = "INSERT INTO user(`LOCKED`, `LASTNAME`,`FORNAME`,`USERNAME`,`PASSWORD`,`ACCESS`,`ACTIVE`,`EMAIL`,`PHONE`,`JOB`,`UNTAXED_MINIMUM`,`UNEMPLOYMENT_INSURANCE`,`PENSION`,`IDCODE`,`BANK`,`ACCOUNT`)
              VALUES(\"0\", ".$arr['lname'].", ".$arr['name'].", ".$arr['username'].", ".$arr['password'].", ".$arr['access'].", \"1\", ".$arr['email'].", ".$arr['phone'].", ".$arr['job'].", ".$arr['min'].", ".$arr['un'].", ".$arr['pens'].", ".$arr['idcode'].", ".$arr['bank'].", ".$arr['account'].")";
try that. two things, use back ticks to quote your field names. second. arrays <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> off msql queries when you do them the way you did, you have to break out of the quote ( i.e. ") then access the array element, tie it into the string by going ".$arr['element']." of course that only works if you're quoting your statement with double quotes :-D[/u]

Posted: Mon Jun 26, 2006 4:40 am
by andym01480

Code: Select all

$sql = "INSERT INTO user(`LOCKED`, `LASTNAME`,`FORNAME`,`USERNAME`,`PASSWORD`,`ACCESS`,`ACTIVE`,`EMAIL`,`PHONE`,`JOB`,`UNTAXED_MINIMUM`,`UNEMPLOYMENT_INSURANCE`,`PENSION`,`IDCODE`,`BANK`,`ACCOUNT`) 
              VALUES('0', '$arr['lname']', '$arr['name']', '$arr['username']','$arr['password']', '$arr['access']', '1', '$arr['email']', '$arr['phone']', '$arr['job']', '$arr['min']', '$arr['un']', '$arr['pens']', '$arr['idcode']', '$arr['bank']', '$arr['account']')";
Smurf? Not sure I understand that. Surely if the variable $sql is double quoted the array will be parsed and the values inserted into $sql, so each array variable only needs single quotes as expected by mysql. Always works for me! Also you had an extra semilcolon.

Posted: Mon Jun 26, 2006 6:17 am
by AshrakTheWhite
got it to work, used the ".' method