MySQL problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AshrakTheWhite
Forum Commoner
Posts: 69
Joined: Thu Feb 02, 2006 6:47 am

MySQL problem

Post 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']);";
    }
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post 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]
Last edited by Charles256 on Mon Jun 26, 2006 4:56 am, edited 1 time in total.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post 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.
Last edited by andym01480 on Mon Jun 26, 2006 6:43 am, edited 4 times in total.
AshrakTheWhite
Forum Commoner
Posts: 69
Joined: Thu Feb 02, 2006 6:47 am

Post by AshrakTheWhite »

got it to work, used the ".' method
Post Reply