[英]How to correctly execute sql in scripts with private classes
调试处理表单数据的脚本:多个文本字段,两次文件上传(一个图像,另一个文档)。 该脚本正在与我的js文件一起使用,以允许在上传之前裁剪图像。
我在下面的脚本中设置了两个sql语句,第一个sql语句将文本字段数据插入数据库,然后检索插入的唯一且自动递增的行号($ id)。 第二条sql语句应将图像和文档的url插入sql,并插入到第一条sql语句向其提交数据的同一行中。
第一个sql语句正在运行,并且文件已使用此脚本保存到服务器。
第二条sql语句不起作用,我认为这是因为$ id的值没有从CropAvatar类的外部传递。
任何建议如何使第二条sql语句在此处执行?
<?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, address, sale_price, 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();
}
}
}
}
}
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) {
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);
?>
您正在混合数据库库:
$sale_pricee = mysql_real_escape_string($sale_price);
^----
mysqli_query($con,$sql);
^---
mysql
(没有“ i”)和mysqli
(带有“ i”)库是不可互换的,您不能像这样混合/匹配。 由于您从不费心检查查询是否真正成功,因此只需假设任何问题都不会出错。 坏坏坏坏的假设。 总是假设事情会失败,检查该失败,并将成功视为惊喜。
假设您真的要使用“ no-i”版本,那么至少应具有以下内容:
$result = mysql_query($sql) or die(mysql_error());
^^^^^^^^^^^^^^^^^^^^^^
并请注意,mysql已过时。 您不应该再使用它,而应该使用mysqli或PDO。
通过直接在更新查询中使用$ _FILES的['name']
参数,您仍然容易受到sql注入攻击。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.