gpt4 book ai didi

php - 通过表单操作运行 php 时保持与数据库的连接

转载 作者:行者123 更新时间:2023-11-29 21:35:37 25 4
gpt4 key购买 nike

我使用 dropzone.js 来运行我的图像 uploader ,并使用 medoo 来运行我的数据库查询。这个想法是让一个随机生成的字符串与图像相关联。然后将图像调整为各种尺寸,并将它们全部上传到我可以通过与随机字符串关联的 50x50、100x100 等标签调用所有图像的位置。另外,“照片”表中还有一个长字符串,我可以将其分解为该特定产品的所有关联图像的列表(e5tg、23rfg、5rgf 到数组中)。所有这些工作都很好,直到加载 imageuploader.php 后我尝试连接到数据库。我也不确定函数generateRandomString()是否传递给imageuploader.php文件。我确信这可能是一个相对简单的修复,但我似乎无法理解它。这是我的代码。

首先是表格:

require_once 'medoo.php';
//Connect to DB
$link = mysqli_connect("127.0.0.1", "root", "", "main");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}

//DB query string
$database = new medoo([
'database_type' => 'mysql',
'database_name' => 'main',
'server' => 'localhost',
'username' => 'root',
'password' => '',
'charset' => 'utf8'
]);
<link href="css/dropzone.css" type="text/css" rel="stylesheet" />
<script src="js/dropzone.js"></script>
<form action="include/imageuploader.php?oid=1" class="dropzone"></form>

imageuploader.php

<?php

$photos = $database->get("orders", ["photos"],["oid" => $_GET['oid']]);
$rng = generateRandomString();
$ds = DIRECTORY_SEPARATOR; //1
$storeFolder = '../uploads'; //2

if (!empty($_FILES)) {

$tempFile = $_FILES['file']['tmp_name']; //3

$targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds; //4

$targetFile = $targetPath. $rng . "-full.jpg"; //5

move_uploaded_file($tempFile,$targetFile); //6

$src = imagecreatefromjpeg($targetFile);
list($width,$height)=getimagesize($targetFile);

$newwidth=350;
$newheight=350;
$tmp=imagecreatetruecolor($newwidth,$newheight);

$newwidth1=200;
$newheight1=200;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);

$newwidth2=100;
$newheight2=100;
$tmp2=imagecreatetruecolor($newwidth2,$newheight2);

$newwidth3=50;
$newheight3=50;
$tmp3=imagecreatetruecolor($newwidth3,$newheight3);

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
imagecopyresampled($tmp2,$src,0,0,0,0,$newwidth2,$newheight2,$width,$height);
imagecopyresampled($tmp3,$src,0,0,0,0,$newwidth3,$newheight3,$width,$height);


$filename = "../uploads/" . $rng . "-350x350.jpg";
$filename1 = "../uploads/" . $rng . "-200x200.jpg";
$filename2 = "../uploads/" . $rng . "-100x100.jpg";
$filename3 = "../uploads/" . $rng . "-50x50.jpg";

imagejpeg($tmp,$filename,100);
imagejpeg($tmp1,$filename1,100);
imagejpeg($tmp2,$filename2,100);
imagejpeg($tmp3,$filename3,100);

imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
imagedestroy($tmp2);
imagedestroy($tmp3);
}

$new_photos = $photos['photos'] . "," . $rng;
$database->update("orders",["photos"=> $new_photos],["oid" => $_GET['oid']]);

?>

一旦您通过表单操作加载 PHP,是否就无法连接到您的数据库了?

最佳答案

为了在imageuploader.php中使用$database,您必须包含填充该变量的文件(在您的情况下是表单PHP文件,它是愚蠢)。
您应该编写一个简单的 PHP 脚本,该脚本除了打开数据库连接或确保连接仍然打开之外什么也不做。然后,您只需在需要处理数据库的任何地方包含该文件即可。

关于php - 通过表单操作运行 php 时保持与数据库的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34942422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com