在PHP中的私有类中执行第二个SQL语句的问题

时间:2014-11-09 14:59:01

标签: php sql pdo

我正在处理一个处理表单数据的脚本:一些文本字段和两个文件,一个是图像,一个是文档。我在页面上放了两个sql语句,第一个插入文本字段数据并检索行的id。第二个用两个文件的URL更新该行。

第二个sql语句没有执行,尽管文件正在保存到服务器。这是我第一次在PHP中使用私有类,也是第一次使用PDO。

我显然做错了什么,并且想知道是否有人能看到我的问题所在。

   <?php
require('../dbcon2.php');
//Connection 1
try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = $conn->prepare("INSERT INTO listings (title, address, transaction, date_added) VALUES (:title, :address, :transaction, now())");
          $stmt->bindParam(':title', $_POST['title']); 
          $stmt->bindParam(':address', $_POST['address']);
          $stmt->bindParam(':transaction', $_POST['transaction']);
        $stmt->execute();
        $lastid = $conn->lastInsertId();
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
//Create class
    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, $lastid) {
    $this -> setSrc($src);
    $this -> setId($lastid);
    $this -> setData($data);
    $this -> setFile($file);
    $this -> crop($this -> src, $this -> dst, $this -> data);
}
        private $lastid;
        public function setId($lastid) {
                 $this->id = $lastid;
}
        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;
 //Process file upload 
                    $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>";
                        }   
                                if (file_exists("../flyers/" . $_FILES["flyer"]["name"])) 
                                {
                                    unlink("../flyers/" . $_FILES["flyer"]["name"]);
                                    $ad_link="http://www.WEBSITE.com/flyers/" . $_FILES["flyer"]["name"];
                                }
                                    move_uploaded_file($_FILES["flyer"]["tmp_name"],"../flyers/" . $_FILES["flyer"]["name"]);
                                    $ad_link="http://www.website.com/flyers/" . $_FILES["flyer"]["name"];
                        }                   
      //Connection 2
      require('../dbcon2.php');
      try {
          global $last_id;
          $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
          $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
          $sql="UPDATE listings SET ad_link='$ad_link', listing_img='$listing_img' WHERE id=$this->id";
          $conn->exec($sql);
          }
      catch(PDOException $e)
          {
          echo $sql . "<br>" . $e->getMessage();
          }
      $conn = null;

      //Error handling
                        if ($result) {
                            $this -> src = $src;
                            $this -> type = $type;
                            $this -> extension = $extension;
                            $this -> setDst();
                        } 
                    } 
                }
            }
        }
        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;
                    }
                }
                imagedestroy($src_img);
                imagedestroy($dst_img);
            }
        }
        private function codeToMessage($code) {
            switch ($code) {

                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'], $lastid);
    $response = array(
        'state'  => 200,
        'message' => $crop -> getMsg(),
        'result' => $crop -> getResult()
    );
    echo json_encode($response);
?>

0 个答案:

没有答案