General developer forum

How to store form data in Moodle database table?

 
Picture of Rajakumar Jillella
How to store form data in Moodle database table?
 

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: -
Davo
Re: How to store form data in Moodle database table?
Group Core developersGroup Particularly helpful MoodlersGroup 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)
Picture of Rajakumar Jillella
Re: How to store form data in Moodle database table?
 

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.

 
Average of ratings: -
Davo
Re: How to store form data in Moodle database table?
Group Core developersGroup Particularly helpful MoodlersGroup 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


 
Average of ratings: -
Picture of Rajakumar Jillella
Re: How to store form data in Moodle database table?
 

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




 
Average of ratings: -
Davo
Re: How to store form data in Moodle database table?
Group Core developersGroup Particularly helpful MoodlersGroup 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.

 
Average of ratings: -