I am using query for joomla.
$query = \"INSERT INTO \'#__demo\'( \'id\', \'fname\', \'mname\', \'lname\' ) VALUES ( \'$val\', \'$post[\'fname\']\', \'$post[\
There are two mistakes on your query.
You haven't escaped your quotes in $_POST
values.
'$post['fname']'
// ^ here and other places
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]' );";