-2

I am doing an insertion operation in PHP using functions. The error seems very trivial but i couldn't identify its source.

<?php

class DB_Insert{

public function insertLec($lecId,$csId,$date,$time,$update,$export)
{
    $con=mysql_connect("localhost","root","");
    mysql_select_db("attenmandb");
    $retval=mysql_query("INSERT INTO lecturetb(LecID, CSID, Date, Time, Updation, Export) VALUES($lecId,'$csId','$date','$time','$update','$export')",$con);
    if(! $retval)
    {
        echo 'ERROR!';
    }
    else
    {
        echo 'Success';
    }
}

$this->insertLec(1,'CST5','27-9-2014','11.30 to 12.30','No','No');
}

?>

Error looks like this:

Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in DB_Insert.php on line 20
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • There is **no more support** for `mysql_*` functions, they are [**officially deprecated**](https://wiki.php.net/rfc/mysql_deprecation), **no longer maintained** and will be [**removed**](http://php.net/manual/en/function.mysql-connect.php#warning) in the future. You should update your code with [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) to ensure the functionality of your project in the future. – Kermit Oct 03 '14 at 18:01

2 Answers2

4
$this->insertLec(1,'CST5','27-9-2014','11.30 to 12.30','No','No');

Is just floating in your class. It needs to be in the code that calls your class.

$insert = new DB_Insert();
$insert->insertLec(1,'CST5','27-9-2014','11.30 to 12.30','No','No');

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

You are also wide open to SQL injections

Community
  • 1
  • 1
John Conde
  • 217,595
  • 99
  • 455
  • 496
0

Your have sequential code with your class definition, but not within a function. You need to move that function call outside the class:

} // end of class definition
  $dbInsert= new DB_Insert();
  $dbInsert->insertLec(1,'CST5','27-9-2014','11.30 to 12.30','No','No');

//edited to acknowledge previous answer as correct! Yeah, like John said!

ben.hamelin
  • 181
  • 1
  • 4