October 11, 2012

How to use Insert into query for joomla 2.5?

Question by Durgesh Sonawane

Iam using query for joomla.

$query = "INSERT INTO '#__demo'( 'id', 'fname', 'mname', 'lname' ) VALUES ( '$val', '$post['fname']', '$post['Mname']', '$post['Lname']' );";

It is giving error

syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING 

Answer by Starx

There are two mistakes on your query.

  1. You haven’t escaped your quotes in $_POST values.

    '$post['fname']'
    //     ^ here and other places
    
  2. You are using single quotes ' to represent tables and field names.

     .. INTO '#__demo'( ..       
    //       ^ here and other places
    

Now after removing all such problems. You query becomes:

$query = "INSERT INTO `#__demo` ( `id`, `fname`, `mname`, `lname` ) VALUES ( '$val', '$post[fname]', '$post[Mname]', '$post[Lname]' );";

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!