PHP - 值未保存在数组中

时间:2016-09-01 13:46:12

标签: php arrays

道歉,但已经找不到答案......

我有一些遇到错误的PHP代码,我将其回显并保存到数组中:

    echo "File is not an image.";
    $errors[] = "File is not an image";

先前已定义数组:

    $errors = array();

当我调试我的网页时,我可以看到回声(所以我知道它正在发生错误)但数组是空的...截图如下:

screenshot of web page

知道我做错了什么吗?或者如何进一步调试?

谢谢!

西蒙

编辑:我注意到数组被声明了两次。删除了,没有变化。

以下完整代码

<?php
class BanditquotecustomformModuleFrontController extends ModuleFrontController
{

  public function initContent()
  {
    $varCountry = '';
    $varRemarks = '';
    $varContactEmail = '';
    $varCustomerName = '';
    $varPhone = '';
    $varUrgency = '';
    $sql = ''; 
    $user_id ='';
    /* For when I get this working!
    $id_lang = Context::getContext()->language->id;
    $category = Category::searchByName($id_lang,"personalised",true);*/


    /*parent::initContent();*/
    if ($this->context->customer->isLogged())
    {
      $this->setTemplate('customform.tpl');
      $custfirstname = $this->context->customer->firstname;
      $custlastname = $this->context->customer->lastname;
      $custemail = $this->context->customer->email;
      $this->context->smarty->assign('firstname', $custfirstname);
      $this->context->smarty->assign('lastname', $custlastname);
      $this->context->smarty->assign('customer_email', $custemail);

      parent::initContent();
    }
    else
    {
          Tools::redirect('index.php?controller=authentication&back='.urlencode($this->context->link->getModuleLink('banditquote', 'customform')));
    }

  }

  /* adds a new product for every quote */
  private function addProduct($LastId)
  {
    $reference = 'CUST'.$LastId;  
        $name = $reference;
    $product = new Product();
        $languages=Language::getLanguages();
        foreach($languages as $lang){
          $product->name[$lang['id_lang']]=$name;
          $product->link_rewrite[$lang['id_lang']]=$name;
          $product->description[$lang['id_lang']]=$name;
        }   
        $product->reference=$reference;
    $product->active='1';
    $product->available_for_order='1';
        $product->is_virtual='1';
    /* add product to category */
        $product->id_category='2';
        $product->id_category_default='2';
    try{
          $product->save();
        } catch (PrestaShopException $e){
          echo $e->displayMessage();
        }
        $product->addToCategories(array(2));
        $product->save();

        $prod_shopupdate = Db::getInstance()->Execute('
                    UPDATE `'.pSQL(_DB_PREFIX_).'product_shop`
                    SET `active` = "1",`available_for_order` = "1"
                    WHERE `id_product` = "'.((int)$product->id).'"');


    return $product->id;
  }

  /* checking attachment */
  private function checkImage()
  {
    $target_dir = '_THEME_PROD_PIC_DIR_';
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 0;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        $errors[] = $this->module->l('File is an image', 'customform');
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image";
        $errors[] = "File is not an image";
        $uploadOk = 0;
    }

     // Check if file already exists
    if (file_exists($target_file)) {
      $errors[] = $this->module->l('Sorry, file already exists', 'customform');
      $uploadOk = 0;
    }

    // Check file size
    if ($_FILES["fileToUpload"]["size"] > 500000) {
      $errors[] = $this->module->l('Sorry, your file is too large', 'customform');
      $uploadOk = 0;
    }

   // Allow certain file formats
   if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
   && $imageFileType != "gif" ) {
     $errors[] = $this->module->l('Sorry, only JPG, JPEG, PNG & GIF files are allowed', 'customform');
     $uploadOk = 0;
   }

   // Check if $uploadOk is set to 0 by an error 
   if ($uploadOk == 0) {
     $errors[] = $this->module->l('Sorry, your file was not uploaded', 'customform');
     // if everything is ok, try to upload file
   }
   else {
     if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
     }
     else {
        $errors[] = $this->module->l('Sorry, there was an error uploading your file', 'customform');
     }
   }

   return $uploadOk;
  }

  public function postProcess()
  {
      if (Tools::isSubmit('submit'))
      {

          $errors = array();
          $failed = false;
          $varUserId = (int)$this->context->customer->id;
          $varRemarks = Tools::getValue('formRemarks');
          $newRemarks = str_replace("'","''", $varRemarks);
          $varCountry = Tools::getValue('formCountry');
          $varContactEmail = Tools::getValue('formContactEmail');
      $varCustomerName = Tools::getValue('formCustomerName');
      $varPhone = Tools::getValue('formPhone');
      $varUrgency = Tools::getValue('formUrgency');
      if (empty($varRemarks))
      {
        $errors[] = $this->module->l('The message cannot be blank.', 'customform');
        //d($errors);
      }
      else
      {
            $sql = "INSERT INTO `ps_quotes` (`quote_destin`,`quote_remarks`,`quote_email`,`quote_submitter_name`,`quote_phone`,`quote_urg`,`quote_user_id`)
                    VALUES('{$varCountry}','{$newRemarks}','{$varContactEmail}','{$varCustomerName}','{$varPhone}','{$varUrgency}','{$varUserId}');";

            if (!Db::getInstance()->execute($sql))
            {
              die('Error creating Custom Order - Please contact admin@banditbirds.co.uk');
            }
            $quoteid = Db::getInstance()->Insert_ID();
            $prod_id = $this->addProduct(Db::getInstance()->Insert_ID());
            if ($prod_id == null )
            {
              die('Error creating Custom Product for Custom Order - Please contact admin@banditbirds.co.uk');
            }
            $updatesql = "UPDATE `ps_quotes` SET `quote_product_link` = $prod_id WHERE `quote_id` = $quoteid;";
            Db::getInstance()->execute($updatesql);


            //Check the uploaded attachment - if there
            if ( $this->checkImage() )
            {

            // add ps_attachment
            // link ps_attachment to product with ps_product_attachment


            }
            else
            {
              $this->context->smarty->assign('errors', $errors);
              $failed = true;
              d($errors);
            }

            //Mailing
            $confirmation1 = '';
            $template = 'banditquote';
            $template_owner = 'banditquote_owner';
        $template_vars = array(
            '{banditquote_contactaddress}' => '',
            '{banditquote_contacttown}' => '',
            '{shop_url}' => Tools::getShopDomain(true),
                                        /* in case we need to check ssl $this->ssl_enable ? Tools::getShopDomainSsl(true) : Tools::getShopDomain(true);*/
            '{remarks}' => $varRemarks,
            '{shipping_destination}' => $varCountry,
            '{phone}' => $varPhone,
            '{name_cust}' => $varCustomerName,
            '{shipping_quantity}' => '',
            '{paypal}' => '',
            '{first_order}' => '',
            '{urgency}' => $varUrgency,
            '{language}' => (int)$this->context->language->id,
            '{webmaster}' => 'admin@banditbirds.co.uk',
            '{email}' => $varContactEmail,
            '{shop_name}' => Configuration::get('PS_SHOP_NAME'),
            '{owner_email}' => Configuration::get('PS_SHOP_EMAIL'));


        $language = (int)$this->context->language->id;
        $maildir = '/home/banditbi/public_html/modules/banditquote/mails/1.5/';

        if ((Mail::Send($language, $template, Configuration::get('PS_SHOP_NAME'), $template_vars, $varContactEmail, null, null, null, null, null, $maildir,false,null)) &&
                (Mail::Send($language, $template_owner, Configuration::get('PS_SHOP_NAME'), $template_vars, 'admin@banditbirds.co.uk', null, null, null, null, null, $maildir,false,null)))
        $this->context->smarty->assign('confirmation1', 1);
        else
        {
        if (is_null($confirmation1))
        {
          $this->_html .= '<div style="background:#F00; padding:5px; border:thin; border-color:#030; text-align:center"><A HREF="javascript:javascript:history.go(-1)">'.$this->l('CAPTCHA Error, please go back and try once more.').'</A></div><br />';
          return $this->_html;
        } 
        }

          if( !$failed )
          {
            Tools::redirect('index.php');
          }
        }

       $this->context->smarty->assign(array(
            'id_customer' => (int)$this->context->customer->id,
            'errors' => $errors,
            'form_link' => $errors,
           ));

       $this->setTemplate('customform.tpl');
      }
  }


}
?>

1 个答案:

答案 0 :(得分:0)

在尝试将值存储到数组之前,请确保定义数组。

$errors = array();

$errors[] = 'File is not an image';

在向此阵列添加项目后,请勿尝试重新定义阵列。否则,您将有效地重置阵列,删除之前添加的任何项目。