在PHP中将变量传递给私有类的问题

时间:2014-11-04 23:08:46

标签: php mysql private public

我正在处理流程的脚本表单数据:3个文本字段和两个文件上传(一个是图像,另一个是pdf文档)。该脚本成功保存并将两个文件命名为服务器,并将3个文本字段保存到sql db。但是,图像和文档的网址未保存到sql。

该脚本使用两个sql statememtns:第一个用于文本字段,第二个用于文件url。第一个sql在我的类CropAvatar之外执行。我的理由是传递第一次sql执行后获得的var $ id(在sql中是自动递增),然后将其传递给第二个语句。我以为我是通过在构造函数中定义$ id并在第二个sql语句中使用它来实现的。

没有成功,第二个sql语句似乎没有执行。有关如何获取提交给sql的这两个文件的URL的任何建议?或者有关于调试的任何建议吗?

我不高兴我在这里发布了150多行代码,我不知道哪里出错了。我仍然试图绕过这些私有函数的正确使用,我感觉我的错误已经包含在其中的某个地方。提前感谢您的任何建议。

    <?php
$title = $_POST['title'];
$titlee = mysql_real_escape_string($title);
$address = $_POST['address'];
$addresse = mysql_real_escape_string($address);
$sale_price = $_POST['sale_price'];
    $sale_pricee= mysql_real_escape_string($sale_price);
require('../dbcon.php');
$sql="INSERT INTO listings (title, comment,transaction, date_added) VALUES ('$titlee', '$addresse, '$sale_pricee, now())";
mysqli_query($con,$sql);
$id = mysqli_insert_id();
    class CropAvatar {
        private $src;
        private $data;
        private $file;
        private $dst;
        private $type;
        private $extension;
        private $srcDir = '../0images/listimg/orig';
        private $dstDir = '../0images/listimg/mod';
        private $msg;
function __construct($src, $data, $file, $id) {
    $this -> setSrc($src);
    $this -> setId($id);
    $this -> setData($data);
    $this -> setFile($file);
    $this -> crop($this -> src, $this -> dst, $this -> data);
}
        private $id;
        public function setId($id) {
                 $this->id = $id;
}
        private function setSrc($src) {
            if (!empty($src)) {
                $type = exif_imagetype($src);
                if ($type) {
                    $this -> src = $src;
                    $this -> type = $type;
                    $this -> extension = image_type_to_extension($type);
                    $this -> setDst();
                }
            }
        }
        private function setData($data) {
            if (!empty($data)) {
                $this -> data = json_decode(stripslashes($data));
            }
        }
        private function setFile($file) {
            $errorCode = $file['error'];
            if ($errorCode === UPLOAD_ERR_OK) {
                $type = exif_imagetype($file['tmp_name']);

                if ($type) {
                    $dir = $this -> srcDir;

                    if (!file_exists($dir)) {
                        mkdir($dir, 0777);
                    }
$currdate=date('YmdHis');
                    $extension = image_type_to_extension($type);
                    $src = $dir . '/' . $currdate . $extension;
                    if ($type == IMAGETYPE_GIF || $type == IMAGETYPE_JPEG || $type == IMAGETYPE_PNG) {

                        if (file_exists($src)) {
                            unlink($src);
                        }
                        $result = move_uploaded_file($file['tmp_name'], $src);
$listing_img="http://www.website.com/0images/listimg/mod/" . $currdate . $extension;
$allowedExtsf = array("pdf");
$tempf = explode(".", $_FILES["flyer"]["name"]);
$extensionf = end($tempf);

if (($_FILES["flyer"]["type"] == "application/pdf")
&& ($_FILES["flyer"]["type"] <2000000000)
&& in_array($extensionf, $allowedExtsf)) 
{
    $flyername=$_FILES["flyer"]["name"];

    if ($_FILES["flyer"]["error"] > 0) 
    {
    echo "Return Code: " . $_FILES["flyer"]["error"] . "<br>";
    }   
        else 
        {
            if (file_exists("../flyers/" . $_FILES["flyer"]["name"])) 
            {
             echo $_FILES["flyer"]["name"] . " already exists. ";
            }
                else 
                {
                move_uploaded_file($_FILES["flyer"]["tmp_name"],"../flyers/" . $_FILES["flyer"]["name"]);
                 }
        }
      $ad_link="http://www.website.com/flyers/" . $_FILES["flyer"]["name"];
      require('../dbcon.php');
$sql="update listings SET ad_link='$ad_link', listing_img='$listing_img' WHERE id=$ID";
mysqli_query($con,$sql);
mysqli_close($con);
}
                        if ($result) {
                            $this -> src = $src;
                            $this -> type = $type;
                            $this -> extension = $extension;
                            $this -> setDst();
                        } else {
                             $this -> msg = 'Failed to save file';
                        }
                    } else {
                        $this -> msg = 'Please upload image with the following types: JPG, PNG, GIF';
                    }
                } else {
                    $this -> msg = 'Please upload image file';
                }
            } else {
                $this -> msg = $this -> codeToMessage($errorCode);
            }
        }

        private function setDst() {
            $dir = $this -> dstDir;

            if (!file_exists($dir)) {
                mkdir($dir, 0777);
            }
            $this -> dst = $dir . '/' . date('YmdHis') . $this -> extension;
        }
        private function crop($src, $dst, $data) {
            if (!empty($src) && !empty($dst) && !empty($data)) {
                switch ($this -> type) {
                    case IMAGETYPE_GIF:
                        $src_img = imagecreatefromgif($src);
                        break;
                    case IMAGETYPE_JPEG:
                        $src_img = imagecreatefromjpeg($src);
                        break;
                    case IMAGETYPE_PNG:
                        $src_img = imagecreatefrompng($src);
                        break;
                }
                if (!$src_img) {
                    $this -> msg = "Failed to read the image file";
                    return;
                }
                $dst_img = imagecreatetruecolor(220, 220);
                $result = imagecopyresampled($dst_img, $src_img, 0, 0, $data -> x, $data -> y, 220, 220, $data -> width, $data -> height);
                if ($result) {
                    switch ($this -> type) {
                        case IMAGETYPE_GIF:
                            $result = imagegif($dst_img, $dst);
                            break;

                        case IMAGETYPE_JPEG:
                            $result = imagejpeg($dst_img, $dst);
                            break;

                        case IMAGETYPE_PNG:
                            $result = imagepng($dst_img, $dst);
                            break;
                    }

                    if (!$result) {
                        $this -> msg = "Failed to save the cropped image file";
                    }
                } else {
                    $this -> msg = "Failed to crop the image file";
                }
                imagedestroy($src_img);
                imagedestroy($dst_img);
            }
        }

        private function codeToMessage($code) {
            switch ($code) {
                case UPLOAD_ERR_INI_SIZE:
                    $message = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
                    break;               
                default:
                    $message = 'Unknown upload error';
            }
            return $message;
        }
        public function getResult() {
            return !empty($this -> data) ? $this -> dst : $this -> src;
        }
        public function getMsg() {
            return $this -> msg;
        }
    }
$crop = new CropAvatar($_POST['avatar_src'], $_POST['avatar_data'], $_FILES['avatar_file'], $id);
    $response = array(
        'state'  => 200,
        'message' => $crop -> getMsg(),
        'result' => $crop -> getResult()
    );
    echo json_encode($response);
?>

1 个答案:

答案 0 :(得分:1)

我浏览了你的代码,你应该打印你的quires以确保,但在:

$sql="update listings SET ad_link='$ad_link', listing_img='$listing_img' WHERE id=$ID";

我无法查看变量$ ID的来源,因此更新无效。 你可以,并且应该使用mysqli_error,以便下次更容易找到你的sql错误。

我希望能解决它。