How to store form data in Moodle database table?

How to store form data in Moodle database table?

by Rajakumar Jillella -
Number of replies: 5

I'm using Moodle 3.2 version.


I have a custom form  in the file, and created a table in database.

After submitting the form, how can i store the form fields in to the database table?


Here is the code i have written.

<?php

require_once('../config.php');

global $DB;

if(isset($_POST['btn-signup'])){

$userid = $USER->id;

$courseName = $Course->fullname;

$text = $_POST['name'];

$extdbuser1 = (object)array('userId'=>$userid, 'courseName'=>$courseName, 'journalText'=>$text);

$extdbuser1->id = $DB->insert_record('end_journalcontent', $extdbuser1);

}


userId, courseName, journalText are column names in the table.

end_journalcontent my table name

What is the query to store data into DB table?


Thanks for your help in Advance.


Average of ratings: -
In reply to Rajakumar Jillella

Re: How to store form data in Moodle database table?

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

There are a number of issues you should address in your code.

  1. Try to use the Moodle Forms API to handle all your forms - this produces consistently-styled forms and helps with validation of the data.
  2. Never use $_POST (or $_GET) - use the required_param() / optional_param() functions provided by Moodle to get params.
  3. Make sure you use the XMLDB Editor to define the database tables for your plugin - this will ensure you create database tables that follow the Moodle guidelines (including not having capital letters in the field names).
  4. Is your table actually called 'end_journalcontent' or is it called 'mdl_end_journalcontent' (or whatever you have configured $CFG->prefix to be, if it is not the default 'mdl_')? Moodle database functions all add $CFG->prefix to the start of the tablename before trying to access the database, so the function call you have written will try to update table 'mdl_end_journalcontent'.
  5. Have you switched Debugging on? If you do so, you will get much more helpful error messages.
Average of ratings: Useful (2)
In reply to Davo Smith

Re: How to store form data in Moodle database table?

by Rajakumar Jillella -

Thanks for the reply,

i will modify my table prefix with "mdl_end_journalcontent".

Please give me some code to insert data, i will go through that.


Thanks in advance for the help.

In reply to Rajakumar Jillella

Re: How to store form data in Moodle database table?

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

The basic code you have written for inserting into the database looks correct (with the changes I've already suggested), in general it should look something like:

$ins = (object)['fielda' => $valuea, 'fieldb' => $valueb];
$ins->id = $DB->insert_record('table name without prefix', $ins);

See https://docs.moodle.org/dev/Data_manipulation_API#Inserting_Records


In reply to Davo Smith

Re: How to store form data in Moodle database table?

by Rajakumar Jillella -

This is the code cfter modifying.

<?php

require_once('../config.php');

global $DB;

if(isset($_POST['btn-signup'])){

$userid = $USER->id;   // Current logged in user id

$courseName = $Course->fullname;  //  Current course

$text = $_POST['name'];

// Insert data on external table.

$ins = (object)['userId' => $userid, 'courseName' => $courseName, 'journalText' => $text];

$ins->id = $DB->insert_record('mdl_end_journalcontent', $ins);

}

?>


// This is the form i have

<form method="post" action="">

<div class="form-group">

<div class="input-group">

<span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>

<input type="text" name="name" class="form-control" placeholder="Enter Name" maxlength="50" />

</div>

</div>

<div class="form-group">

<button type="submit" class="btn btn-block btn-primary" name="btn-signup">Register</button>

</div>

</form>


But, getting the error. Please Smith, Help me with this.


Thanks in advance

Attachment 1.png
Attachment 2.png
Attachment 3.png
In reply to Rajakumar Jillella

Re: How to store form data in Moodle database table?

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Please re-read my initial message - field names must not include any capital letters. Use the XMLDB editor to create the DB table definition for your plugin, do not manually create the database table.

Unless your database table is now called 'mdl_mdl_end_journalcontent', you need to fix the call to $DB->insert_record() - you should not add the 'mdl_' prefix yourself, it is automatically added by $DB->insert_record() (but if your table has not been created through the XMLDB editor definitions, it might not be named with the prefix, so $DB->insert_record() won't work).

You also still need to address the use of $_POST in your code.

You have an undefined variable $Course in there as well - I assume you meant $COURSE, but that won't work unless you've called require_login($course) at the top of the code - providing details of which course the user is visiting the page as part of.

I note, from the error message, that you have not enabled Debugging either, as I suggested in my first post.