PHP类没有返回结果

时间:2014-04-12 11:25:05

标签: php oop error-handling

我正在开发一个Item Inventory Web App。我希望用户能够添加项目并将其分配给用户。每个用户一次有权获得一个项目。例如,如果用户已经分配了一个项目并且您想要添加更多项目,系统应该提出一个错误,告诉您 撤消该项目是发布一个新项目,但错误不会被提交到error[]数组中,即使它显示该数组不为空。它只回显序列号a = 1 and a++,但文本不存在。

class.inc.php

class Summary {
    public $result;
    public $conn;
    public $SQ;
    public $q;
    public $updateDB;
    public $checkDB;
    public $returned_result;
    public $a;
    public $data;
    public $col;

    public function __construct(){
        $this->conn = new PDO('mysql:host=localhost; dbname=dB', 'root', '');
        $this->conn->setAttribute(PDO:: ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    public function updateDB($coloumn, $data, $id){
        $SQ = "UPDATE mytable SET $coloumn = ? WHERE staffID = ?";
        $q = $this->conn->prepare($SQ) or die("ERROR: " . implode(":", $this->conn->errorInfo()));
        $q->bindParam(1, $data);
        $q->bindParam(2, $id);
        if ($q->execute()){
            $success = 'Record updated successfully';
        };
        return $success;
    }

    public function checkDB($col, $data){
        $status = 'Active';
        $SQ = "SELECT surname FROM mytable WHERE $col = ? AND status = ?";
        $q = $this->conn->prepare($SQ) or die("ERROR: " . implode(":", $this->conn->errorInfo()));
        $q->bindParam(1, $data);
        $q->bindParam(2, $status);
        $q->execute();
        if($result = $q->fetch(PDO::FETCH_BOTH)){
            $a = $result[0];
            if ($a == ''){
                $this->returned_result = 'N';
            }
            else {
                $this->returned_result = "This item (". $data . ") is in use by ". $a . ". Please widthraw the item";
            }
        }
        return $this->returned_result;
    }
}

的index.php:

include('class.inc.php');
$summary = new Summary;
$error = array();
if(isset($_POST['saveRecord']) ) {
$system_name = strtoupper ( $_POST['system_name'] );
$result =$summary->checkDB('systemName', $system_name); //check if the item is in use
if ( $result == 'N' ){
    $summary->updateDB('systemName', $system_name, $id);
    $update = $summary->updateDB;
}
else $error[] = $result;


$system_serial_number = strtoupper ( $_POST['system_serial_number'] );

$result =$summary->checkDB('CPUSerial', $system_serial_number); //check if the item is in use
if ( $result == 'N' ){
    $summary->updateDB('CPUSerial', $system_serial_number, $id);
    $update = $summary->updateDB;
}
else $error[] = $result;
}

if(isset($_POST['saveRecord']) && !empty( $error ) ) {
echo "<div class = 'text-error'>";
$a = 1;
foreach ($error as $err){
    echo '<p>' . $a . '. ' .$err . '</p>';
    $a++;
}
echo "</div>";
}

任何帮助将不胜感激。关于OOP编程方式,我做错了什么?

0 个答案:

没有答案