我正在处理的脚本处理表单数据:3个文本字段和两个文件上载(一个图像,另一个是pdf文档)。该脚本已成功保存并将两个文件命名为服务器,并将3个文本字段保存为sql db。但是,图像和文档的url不保存到sql。向PHP中的私有类传递变量的问题
脚本使用两个sql statememtns:第一个用于文本字段,第二个用于文件url。第一个sql是在我的类CropAvatar之外执行的。我的理由是在第一次执行sql后获得var $ id(这是sql中的自动增量),然后将它传递给第二条语句。我以为我通过在构造函数中定义$ id并在第二个sql语句中使用它来实现这一点。
没有成功,第二个sql语句似乎没有执行。有关如何获取这两个文件的URL提交给SQL的任何建议?或者在这里调试的任何建议?
我不高兴我在这里发布了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);
?>
**警告**:使用'mysqli'时,应该使用参数化查询和['bind_param'](http://php.net/manual/en/mysqli-stmt.bind-param.php)到将用户数据添加到您的查询。 **不要**使用字符串插值来实现此目的,因为您将创建严重的[SQL注入漏洞](http://bobby-tables.com/)。 – tadman 2014-11-04 23:11:38
看着这个谢谢@tadman – rhill45 2014-11-04 23:15:56
你的整个代码都不好。你不应该因为使用类。一个类包装某些功能并将其隔离。然后开发人员使用这个类(和它的* public *方法)来执行**特定的**作品。你正在混合一切。您也不知道访问控制对方法的作用。你在这150行左右所做的是将程序代码封装到一个大多不是真正可用的类中。我建议你使用PDO进行数据库访问,并取消这段代码并重新开始。这个想法是让代码**更容易阅读和维护。不难。 – 2014-11-04 23:21:01