需要有关“未定义变量”错误的帮助

时间:2019-06-25 12:23:58

标签: php symfony yaml

我目前正在从事一个项目,该项目的最终目标是能够创建一个网页,您可以在该网页中从excel文件中写入数据库而不会影响原始文件。当我在页面上书写时,它会写在数据库中,但是保存后原始的excel文件不会更改。 我正在按照该教程进行操作,有时,我被要求将以下脚本放置在phph文件中:

<?php
$req = $bdd->prepare('INSERT INTO [dbo].[T_PRODUIT_PRO]([SCA_ID], [PRO_CODE], [PRO_LIBELLE], [PRO_PRIX], [PRO_REFERENCE], [PRO_DESCRIPTION1], [PRO_DESCRIPTION2], [PRO_LIBELLE2], [ESB_ID], [TBR_ID], [FPL_ID], [PRO_APARTIR_DE], [PRO_DESCRIPTION_DETAILLEE1], [PRO_DESCRIPTION_DETAILLEE2])');
$req->execute(array(
        'id' => $id,
        'code' => $code,
        'libelle' => $libelle,
        'prix' => $prix,
        'reference' => $reference,
        'description1' => $description1,
        'description2' => $description2,
        'libelle2' => $libelle2,
        'esbid' => $esbid,
        'prix' => $prix,
        'tbrid' => $tbrid,
        'fplid' => $fplid,
        'apartirde' => $apartirde,
        'descriptiondetaille' => $descriptiondetaille,
        'descriptiondetaille2' => $descriptiondetaille2,
        ));

echo 'The file has been added';
?>

因此,我为此更改了控制器:

<?php
// src/Controller/DefaultController.php
// src/Controller/Readfile.php

namespace App\Controller;

//require 'vendor/autoload.php';

use App\Entity\Product;
use App\Form\ProductType;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Bundle\FrameworkBundle\Controller\Readfile;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\File\Exception\FileException;

class DefaultController extends AbstractController
{
    public function index()
    {
        return $this->render('Advert/index.html.twig');
    }

    public function upload() 
    {
        return $this->render('Advert/form.html.twig');
    }
        public function bdd() 
    {
            return $this->render('Advert/write.html.twig');
    }

public function submit() {

  $file_mimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

  if(isset($_FILES['file']['name']) && in_array($_FILES['file']['type'], $file_mimes)) {

      $arr_file = explode('.', $_FILES['file']['name']);
      $extension = end($arr_file);

      if('csv' == $extension) {
          $reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
      } else {
          $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
      }

      $spreadsheet = $reader->load($_FILES['file']['tmp_name']);

      $sheetData = $spreadsheet->getActiveSheet()->toArray();
      print_r($sheetData);
      //return $this->render('Advert/write.html.twig');

  }
}
    /**
     * @Route("/product/new", name="app_product_new")
     */
    public function nouveau(Request $request)
    {
        $product = new Product();
        $form = $this->createForm(ProductType::class, $product);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            // $file stores the uploaded PDF file
            /** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */
            $file = $product->getBrochure();

            $fileName = $this->generateUniqueFileName().'.'.$file->guessExtension();

            // Move the file to the directory where brochures are stored
            try {
                $file->move(
                    $this->getParameter('brochures_directory'),
                    $fileName
                );
            } catch (FileException $e) {
                // ... handle exception if something happens during file upload
            }

            // updates the 'brochure' property to store the PDF file name
            // instead of its contents
            $product->setBrochure($fileName);

            // ... persist the $product variable or any other work

            return $this->redirect($this->generateUrl('app_product_list'));
        }

        return $this->render('product/new.html.twig', [
            'form' => $form->createView(),
        ]);
    }

    /**
     * @return string
     */
    private function generateUniqueFileName()
    {
        // md5() reduces the similarity of the file names generated by
        // uniqid(), which is based on timestamps
        return md5(uniqid());
    }
}

我也为我服务:

# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.

# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
    brochures_directory: '%kernel.project_dir%/public/uploads/brochures'

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/*'
        exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

    # controllers are imported separately to make sure services can be injected
    # as action arguments even if you don't extend any base controller class
    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']

    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones

    App\Controller\test\:
            resource: '../src/Controller/script.php'
            tags: ['controller.service_arguments']

和路径:

#app_test_page:
#    path: /test
#    controller: App\Controller\DefaultController::index

hello_the_world:
    path:       /home
    controller: App\Controller\DefaultController::index

upload_fichier:
    path:       /upload_file
    controller: App\Controller\DefaultController::upload

submit_fichier:
    path:       /read_file
    controller: App\Controller\DefaultController::submit

submit_fichier:
    path:       /write_file
    controller: App\Controller\DefaultController::bdd

所有这些,当我尝试运行命令“ php bin / console server:run”时遇到的错误是:

In FileLoader.php line 166:                                                                                                                                                                        

  Notice: Undefined variable: bdd in C:\xampp\htdocs\importservicesetproduits\config/services.yaml (which is loaded in resource "C:\xampp\htdocs\importservicesetproduits\config/services.yaml").


In script.php line 2:

  Notice: Undefined variable: bdd

如果您需要查看项目的特定页面,请告诉我,我将向您展示,但是我的问题是:我该如何解决我的问题?而且,如果您对我的项目有更多的想法,除此以外,我还有什么要做的事情吗?

0 个答案:

没有答案