将数据从表单插入到Drupal数据库

时间:2019-07-02 16:22:05

标签: php mysql drupal-8

drupal相当新,尝试将数据从表单插入数据库时​​遇到了问题。

表单本身是从主题文件夹

中的js生成的
$('.register')
  .find('.title_block')
  .addClass('reg-details')
  .append('<div id="emailRegister" class="modal"><div class="modal-content"> 
    <span class="close">&times;</span><br><br><p>We have detected that you 
      are on a mobile device. It can 
      take up to 2 minutes to fill out the required profile.</p><p>If you 
      would like to finish this 
      profile at a later time, we can email it to you.</p><p>Otherwise, 
      select Close to proceed!</p> 
      <form method="post" action="/register.php" enctype="multipart/form- 
      data">First name:<br><input 
      type="text" class="emailFields" name="firstname" value=""><br>Last 
      name:<br><input type="text" 
      class="emailFields" name="lastname" value=""><br>Email:<br><input 
      type="email" 
      class="emailFields" id="regEmail" name="email"><br><input type="submit" 
      value="Submit"><br> 
      </form><button type="submit" id="closeModal" 
      style="background:grey">Close</button>')
  .insertBefore('.lms-main-container');

操作页面位于我的文件夹的根目录

<?php

require 'vendor/autoload.php';
 use Drupal\docebo_login\FormaNotification;

 $email = $fname = $lname = "";
 $email = $_POST["email"];
 $fname = $_POST["firstname"];
 $lname = $_POST["lastname"];

 if ($email == "" || $fname == "" || $lname == "") {
   header("Location: http://example.com");
   exit();
 }

 $noti = new FormaNotification();

 $result = $noti->getRegister($fname, $lname, $email);

 $auth = array('api_key' => 'some=key=here');

 # The unique identifier for this smart email
 $smart_email_id = 'email-id';

 # Create a new mailer and define your message
 $wrap = new CS_REST_Transactional_SmartEmail($smart_email_id, $auth);
 $message = array(
     "To" => $email,
     "Data" => array(
         'variableName' => 'variableNameTestValue',
         'x-apple-data-detectors' => 'x-apple-data-detectorsTestValue',
         'href^="tel"' => 'href^="tel"TestValue',
         'href^="sms"' => 'href^="sms"TestValue',
         'owa' => 'owaTestValue',
         'role=section' => 'role=sectionTestValue',
         'style*="font-size:1px"' => 'style*="font-size:1px"TestValue',
     ),
 );

 # Add consent to track value
 $consent_to_track = 'no'; # Valid: 'yes', 'no', 'unchanged'

  # Send the message and save the response
 $result = $wrap->send($message, $consent_to_track);
 if ($result) {
   header("Location: /");
   exit();
 }

如您所见,我从主题模块中调用了一个函数

   public function getRegister($fname, $lname, $email) {

        return parent::insertRegister($fname, $lname, $email);
    }

然后将变量传递给其父级函数

 public function insertRegister($fname, $lname, $email) {
       db_set_active('docebo');
       $this->setConnection('dev-db');
      $result = $this->getConnection()->insert('register')
       ->fields([
         'fistName' => $fname,
         'lastName' => $lname,
         'email' => $email,
         'dateAdded' => REQUEST_TIME,
       ])
       ->execute();
       db_set_active();
     }

现在我可能很幼稚,但我认为就是那样。但是,当我提交表格时,我得到的只是一个500错误通知。当我从表单操作中删除插入内容时,表单将按预期工作。

我尝试获取更好的错误消息来帮助我,例如,检查我的settings.php以确保错误报告详细,甚至尝试将其设置在ini.php中或将

 ini_set('display_errors', 1);
 ini_set('display_startup_errors', 1);
 error_reporting(E_ALL);

在表单操作页面本身上,仍然一无所获。我的日志中似乎也没有任何内容。所以我很沮丧。任何帮助将不胜感激。谢谢!!!

0 个答案:

没有答案