未定义的变量错误

时间:2017-04-21 14:19:35

标签: php mysql

我正在尝试将不同的交易类型插入我的数据库,例如信用卡,支票,借记卡。当我的视图页面上的文本框被填充时,我能够将该数据插入到我的数据库中。但是,如果我提交表单并且没有填充文本框,则会收到此错误消息。请有人提供解决方案。

错误消息

Notice: Undefined variable: error in E:\xampp\htdocs\CIT2318\Relational database and web integration\controllers\LoanController.php on line 100

贷款控制方

 <?php
session_start();
require_once("../models/LoanModel.php");
require_once("../models/DvdModel.php");
require_once("../models/PaymentModel.php");
require_once("../views/LoanView.php");

$fr = new LoanModel;
$dm = new DvdModel;
$pm = new PaymentModel;

$empnin = $_SESSION['userid'];

$error = "";

if (isset($_POST["Submit"])) {
$dvdid = enhance($_POST["dvdID"]);
$custid = enhance($_POST["custID"]);
$payoptions = $_POST["payOptions"];

if($payoptions == "2"){
  $chequenum = enhance($_POST["chqnum"]);
  $chequebanknum = enhance($_POST["bnknum"]);
  $chequebankname = enhance($_POST["bnkname"]);
}
if($payoptions == "3"){
  $debitnum = enhance($_POST["dcnum"]);
  $debittype = enhance($_POST["dctype"]);
  $dcexpiry = enhance($_POST["dcexpr"]);
}
if($payoptions == "4"){
  $creditnum = enhance($_POST["ccnum"]);
  $credittype = enhance($_POST["cctype"]);
  $creditexpiry = enhance($_POST["ccexpr"]);
}

if(empty($dvdid)){
  $error .= "DVD ID is required </br>";
} else if(empty($custid)){
  $error .= "Customer ID is required </br>";
} else if(empty($payoptions)){
  $error .= "No Payment type selected </br>";
} else {
  if(!preg_match("/^[0-9]*$/",$dvdid)){
    $error .= "DVD ID: only numbers allowed </br>";
  } else if(!preg_match("/^[0-9]*$/",$custid)){
    $error .= "Customer ID: only numbers allowed </br>";
  } else if(payoptions_error_check($payoptions)){
    $data = $dm->getDVD($dvdid);
    if($data->num_rows > 0){
      $row = $data->fetch_assoc();
      $filmid = $row["filmid"];
      $cur_returns = $fr->getCurrentReturns($dvdid);
      if($cur_returns->num_rows==0){
        $pm->setPayment('3.99', $empnin, $custid, '3', $payoptions);

        $maxID = $pm->getMaxID()->fetch_assoc();
        $maxdata = $maxID['ID'];

        if($payoptions == "1"){
          $pm->setCash($maxdata);
        } else if($payoptions == "2"){
          $pm->setCheque($maxdata, $chequenum, $chequebanknum, $chequebankname);
        } else if($payoptions == "3"){
          $pm->setDebitCard($maxdata, $debitnum, $debittype, $dcexpiry);
        } else if ($payoptions == "4") {
          $pm->setCreditCard($maxdata, $creditnum, $credittype, $creditexpiry);
        }

        $fr->setRental($dvdid, $filmid, $empnin, $custid, $maxdata, 3.99);
        $error = "Rented dvd: " . $dvdid . " to customer: " . $custid;
      }else {
        $error = "Cannot not rent dvd";
      }
    } else {
      $error = "ID: " . $dvdid . " does not exist";
    }
  } else {
    $error = "Incorrect form submission";
  }
}

echo "<hr>" . $error;
}

echo "<hr>" . "<a href=\"../views/MenuView.php\">back";


function payoptions_error_check($po){
$count = 0;
if($po == "2"){
  if(empty($_POST["chqnum"])){$error .= "Cheque number required" . "</br>"; $count++;}
  if(empty($_POST["bnknum"])){$error .= "Bank number required" . "</br>";$count++;}
  if(empty($_POST["bnkname"])){$error .= "Bank name required" . "</br>";$count++;}
} else if($po == "3"){
  if(empty($_POST["dcnum"])){$error .= "Debit Card number required" . "</br>";$count++;}
  if(empty($_POST["dctype"])){$error .= "Debit Card type required" . "</br>";$count++;}
  if(empty($_POST["dcexpr"])){$error .= "Debit Card expiration date is required" . "</br>";$count++;}
} else if($po == "4"){
  if(empty($_POST["creditnum"])){$error .= "Credit number required" . "</br>";$count++;}
  if(empty($_POST["credittype"])){$error .= "Credit type is required" . "</br>";$count++;}
  if(empty($_POST["creditexpiry"])){$error .= "Credit expiry date is required" . "</br>";$count++;}
}
if($count > 0){return false;} else {return true;}

}

function enhance($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>

付款方式

<?php
require_once('DAO.php');

class PaymentModel extends DAO{

protected $target = "frs_Payment";

public function __construct(){
  parent::__construct();
}

public function getMaxID(){
  $sql = "SELECT MAX(payid) AS ID FROM {$this->target}";
  return parent::query($sql);
}

public function setPayment($amount, $empnin, $custid, $pstatusid, $ptype){
  $amount = parent::escape($amount);
  $empnin = parent::escape($empnin);
  $custid = parent::escape($custid);
  $pstatusid = parent::escape($pstatusid);
  $ptype = parent::escape($ptype);
  $sql = "INSERT INTO {$this->target}
          (`payid`, `amount`, `paydatetime`, `empnin`, `custid`,
          `pstatusid`, `ptid`)
          VALUES
          ((SELECT MAX('payid') FROM {$this->target} id), '{$amount}', NOW(), '{$empnin}', '{$custid}',
          '{$pstatusid}', '{$ptype}')";
  return parent::query($sql);
}

//sets the  cash amount
public function setCash($payid){
  $payid = parent::escape($payid);
  $sql = "INSERT INTO frs_Cash (`payid`)
          VALUES ('{$payid}')";
  return parent::query($sql);
}
//set the Cheque information
public function setCheque($payid, $chequeno, $bankno, $bankname){
  $payid = parent::escape($payid);
  $chequeno = parent::escape($chequeno);
  $bankno = parent::escape($bankno);
  $bankname = parent::escape($bankname);
  $sql = "INSERT INTO frs_Cheque
          (`payid`, `chequeno`,`bankno`,`bankname`)
          VALUES
          ('{$payid}', '{$chequeno}','{$bankno}','{$bankname}')";
  return parent::query($sql);
}

//set debit card
public function setDebitCard($payid, $dcno, $dctype, $dcexpr){
  $payid = parent::escape($payid);
  $dcno = parent::escape($dcno);
  $dctype = parent::escape($dctype);
  $dcexpr = parent::escape($dcexpr);
  $sql = "INSERT INTO frs_DebitCard
          (`payid`, `dcno`,`dctype`,`dcexpr`)
          VALUES
          ('{$payid}', '{$dcno}','{$dctype}','{$dcexpr}')";
  return parent::query($sql);
}
//set credit card
public function setCreditCard($payid, $ccno, $cctype, $ccexpr){
  $payid = parent::escape($payid);
  $ccno = parent::escape($ccno);
  $cctype = parent::escape($cctype);
  $ccexpr = parent::escape($ccexpr);
  $sql = "INSERT INTO frs_CreditCard
          (`payid`, `ccno`,`cctype`,`ccexpr`)
          VALUES
          ('{$payid}', '{$ccno}','{$cctype}','{$ccexpr}')";
  return parent::query($sql);
}
}
?>

0 个答案:

没有答案