1

I am trying to insert record into my database using moodle.

I am using version 1.9.19. i am trying the following code :

<?php
  require_once('config.php');
  require_once('uplo.php');

  $mform = new uplo();

  $mform->display();

  if(isset($_POST['submitbutton'])){

  $name = $mform->get_data('name');
  $email = $mform->get_data('email');

  $table='mdl_tet';

  $res=insert_record($table, '$name','$email') ;

 }  
?>

But this is not working correctly. How to do that correctly.

Note : Why am using 1.9.19 means my client using this version so i cant change the version.

Nisanth
  • 323
  • 8
  • 24

1 Answers1

3

The insert_record() function takes two parameters - the name of the table (without the prefix) and an object containing the data to insert into the table.

So, in this case, you should write something like:

$ins = (object)array('name' => $name, 'email' => $email);
$ins->id = insert_record('tet', $ins);

OR:

$ins = new stdClass();
$ins->name = $name;
$ins->email = $email;
$ins->id = insert_record('tet', $ins);

(As an aside - make sure you turn on debugging - https://docs.moodle.org/19/en/Debugging - it will make your life a lot easier).

davosmith
  • 6,037
  • 2
  • 14
  • 23
  • i am trying but it is not working for me. please can you help me – Nisanth Nov 02 '15 at 12:30
  • Assuming you have already created a table called mdl_tet and that $CFG->prefix in your config.php file is set to 'mdl_' (the default), then the insert_record() code I have shown will work. If it doesn't work, then (with debugging) you will almost certainly get a useful error message output (if not, then look in your web server log and report the error message you find there). – davosmith Nov 02 '15 at 15:35
  • i have an error like this : - Catchable fatal error: Object of class stdClass could not be converted to string in D:\xampp\htdocs\moodel\server\moodle\lib\adodb\drivers\adodb-mysql.inc.php on line 150 – Nisanth Nov 03 '15 at 05:05
  • That suggests that $ins has at least one property that is an object - e.g. $ins->name = new stdClass(). Try var_dump($ins) (just before the insert_record line to see what is being inserted). – davosmith Nov 03 '15 at 09:11
  • if i use var_dump means : { ["MAX_FILE_SIZE"]=> string(9) "786432000" ["name"]=> string(2) "ra" ["email"]=> string(13) "jat@gmail.com" ["submitbutton"]=> string(12) "Save changes" } } – Nisanth Nov 03 '15 at 09:57