dousha1831 2014-11-03 04:49
浏览 29
已采纳

发布到db时导致文本值为空的原因是什么

I am appending a form and script that processes an image file upload. I added text fields, a radio button and an additional file-upload.

The image upload and additional file upload portion of the script work fine, as does the radio button. Those values are being saved into my sql db correctly.

However, all the text fields are submitting empty values into sql.

Here's the script up to my SQL statement, which remember is working for the URL of the image, document the value of the radio button. But all text fields like 'title' and 'address' are blank in the database.

html form

      <div class="avatar-view" title="Change the avatar"> <img src="../0images/cropy.jpg" alt="Avatar"> </div>

      <!-- Cropping modal -->
      <div class="modal fade" id="avatar-modal" tabindex="-1" role="dialog" aria-labelledby="avatar-modal-label" aria-hidden="true">
        <div class="modal-dialog modal-lg">
          <div class="modal-content">
            <form class="avatar-form" method="post" action="crop-avatar.php" enctype="multipart/form-data">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h1>Listing Upload</h1>
                <h2 class="modal-title" style="margin-left:25px;" id="avatar-modal-label">Listing Main Image</h2>
              </div>
              <div class="modal-body">
                <div class="avatar-body"> 

                  <!-- Upload image and data -->
                  <div class="avatar-upload">
                    <input class="avatar-src" name="avatar_src" type="hidden">
                    <input class="avatar-data" name="avatar_data" type="hidden">
                    <label for="avatarInput">Local upload</label>
                    <input class="avatar-input" id="avatarInput" name="avatar_file" type="file">
                  </div>

                  <!-- Crop and preview -->
                  <div class="row">
                    <div class="col-md-9">
                      <div class="avatar-wrapper"></div>
                    </div>
                    <div class="col-md-3">
                      <div class="avatar-preview preview-lg"></div>
                      <div class="avatar-preview preview-md"></div>
                      <div class="avatar-preview preview-sm"></div>
                    </div>
                  </div>
                </div>
              </div>
              <div class="content_add">

                <h2>Listing Data</h2>
<input name="title" type="text" /><br />
<input name="address" type="text"  /><br />
<input name="sale_price" type="text"  /><br />
<input name="lease_price" type="text"  /><br />
<input name="build_size" type="text"  /><br />
<input name="lot_size" type="text"  /><br />
<input name="zoning" type="text"  /><br />
<input name="comment" type="text" /><br />
<input name="transaction" type="text"  /><br />
<h2>Listing Flyer</h2>
                <P class="h4" style="display:inline;">Flyer</P>
                <h6 style="display: inline;">Required</h6>
                <br />
                <input name="flyer" type="file"  />
                <br>
                <br />

              </div>
              <div class="modal-footer">
                <button class="btn btn-default" type="button" data-dismiss="modal">Close</button>
                <button class="btn btn-primary avatar-save" type="submit">Save and Post Listing</button>
              </div>
            </form>
          </div>
        </div>
      </div>

php script

        $title = $_POST['title'];
        $address = $_POST['address'];
        $sale_price = $_POST['sale_price'];
        $lease_price = $_POST['lease_price'];
        $build_size = $_POST['build_size'];
        $lot_size = $_POST['lot_size'];
        $zoning = $_POST['zoning'];
        $comment = $_POST['comment'];
        $transaction = $_POST['transaction'];
            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) {
                    $this -> setSrc($src);
                    $this -> setData($data);
                    $this -> setFile($file);
                    $this -> crop($this -> src, $this -> dst, $this -> data);
                }

                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);
        require('../dbcon.php');
        $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"];
                   // escape variables for security

        echo '$title'; echo '$address'; echo '$lot_size'; echo '$ad_link';


        $sql="INSERT INTO listings (listing_img, title, address, lot_size, zoning, build_size, sale_price, lease_price, comment,  ad_link, transaction, date_added) VALUES ('$listing_img', '$title', '$address', '$lot_size', '$zoning', '$build_size', '$sale_price', '$lease_price', '$comment',  '$ad_link', '$transaction', now())";
        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;

            case UPLOAD_ERR_FORM_SIZE:
                $message = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
                break;

            case UPLOAD_ERR_PARTIAL:
                $message = 'The uploaded file was only partially uploaded';
                break;

            case UPLOAD_ERR_NO_FILE:
                $message = 'No file was uploaded';
                break;

            case UPLOAD_ERR_NO_TMP_DIR:
                $message = 'Missing a temporary folder';
                break;

            case UPLOAD_ERR_CANT_WRITE:
                $message = 'Failed to write file to disk';
                break;

            case UPLOAD_ERR_EXTENSION:
                $message = 'File upload stopped by extension';
                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']);
$response = array(
    'state'  => 200,
    'message' => $crop -> getMsg(),
    'result' => $crop -> getResult()
);

echo json_encode($response);
  • 写回答

1条回答 默认 最新

  • doubianxian6557 2014-11-03 09:41
    关注

    If you're executing the mysql_code inside your CropAvatar class then your variables are out of the class scope. You don't seem to show your entire PHP code (missing some closing tags), therefor I can't be sure.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致
  • ¥15 禅道二次开发编辑版本,上传不了发行包