我在代码主体和函数之间共享变量值时遇到了麻烦。
在本例中,我想将图像上传到DB,其值与正在使用的Place的ID相匹配。
我目前使用$_REQUEST
从URL中获取正在使用的位置的ID,并试图将其写入变量'mid',然后我将其发送到createthumnail
函数(包含在functions2.php中),但是'mid'值由于某种原因不会传输。
:
- 进入:http://www.students.bl.rdi.co.uk/stu26984/index.php
- 登录:用户名:Test密码:test123
- Go to My Places
- 点击walkoutsf链接
- 尝试通过"选择文件"按钮上传文件
下面是functions2.php
的全文<?php
function createThumbnail2($filename, $mid) {
include 'base.php';
require 'config.php';
if(preg_match('/[.](jpg)$/', $filename)) {
$im = imagecreatefromjpeg($path_to_image_directory . $filename);
} else if (preg_match('/[.](gif)$/', $filename)) {
$im = imagecreatefromgif($path_to_image_directory . $filename);
} else if (preg_match('/[.](png)$/', $filename)) {
$im = imagecreatefrompng($path_to_image_directory . $filename);
}
$ox = imagesx($im);
$oy = imagesy($im);
$nx = $final_width_of_image;
$ny = floor($oy * ($final_width_of_image / $ox));
$nm = imagecreatetruecolor($nx, $ny);
imagecopyresized($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);
if(!file_exists($path_to_thumbs_directory)) {
if(!mkdir($path_to_thumbs_directory)) {
die("There was a problem. Please try again!");
}
}
imagejpeg($nm, $path_to_thumbs_directory . $filename);
// $cookieUserx=$_SESSION['Username'];
// $checkCustidx=mysql_query("SELECT custid AS id from customers WHERE custUsername='".$cookieUserx."';");
// $rx=mysql_fetch_array($checkCustidx);
// $custidx=$rx['id'];
// $updateimage = mysql_query("UPDATE photos SET name = '".$filename."';")or die ('SQL Error: ' . mysql_error());
$updateimage = mysql_query("INSERT INTO photos (NAME, markerid) VALUES('".$filename."', '".$mid."');")or die ('SQL Error: ' . mysql_error());
if($updateimage)
{
header("Location: /WalkaboutSF/viewplace.php?place_id=%20".$mid."");
}
else
{
echo "<h1>Error</h1>";
}
$tn = '<img src="' . $path_to_thumbs_directory . $filename . '" alt="image" />';
echo $tn;
}
?>
这里也是调用它的代码,来自:
http://www.students.bl.rdi.co.uk/stu26984/viewplace.php?place_id=%20133 <div id="insertphoto">
<?php
require 'config.php';
require 'functions2.php';
$place_id = $_REQUEST['place_id'];
$view_place2 = mysql_query("SELECT * FROM markers WHERE id = '$place_id'");
$place2 = mysql_fetch_array($view_place2);
$mid= $place2['id'];
echo $mid;
if(isset($_FILES['fupload'])) {
if(preg_match('/[.](jpg)|(gif)|(png)$/', $_FILES['fupload']['name'])) {
$filename = $_FILES['fupload']['name'];
$source = $_FILES['fupload']['tmp_name'];
$target = $path_to_image_directory . $filename;
move_uploaded_file($source, $target);
createThumbnail2($filename, $mid);
}
}
?>
<h2>Add Photo</h2>
<form enctype="multipart/form-data" action="<?php print $_SERVER['PHP_SELF'] ?>" method="post">
<input type="file" name="fupload" />
<input type="submit" value="Go!" />
</form>
</div>
有一些事情可能正在发生。首先:
$place2 = mysql_fetch_array($view_place2);
$mid= $place2['id'];
echo $mid;
你确定这里设置的是$mid
吗?是否有可能查询没有返回任何行?
如果你确定这里有一个值,然后检查这个:
function createThumbnail2($filename, $mid) {
include 'base.php';
require 'config.php';
您确定base.php
或config.php
没有覆盖$mid
的值吗?试着把代码改成这样:
function createThumbnail2($filename, $mid) {
echo "mid before includes = $mid<br />'n";
include 'base.php';
require 'config.php';
echo "mid after includes = $mid<br />'n";
这将显示你的包含是否践踏了你的局部变量。
edit:我不想转移问题,但是您的代码对SQL注入是开放的。我可以通过使用错误的值调用viewplace.php
来轻松地清除数据库。至少,这样做:
$place_id = mysql_real_escape_string($_REQUEST['place_id']);
您的insert语句也有同样的问题。考虑使用mysqli
或PDO
预处理语句。
编辑2:你的代码不工作的原因是因为有2个不同的HTTP请求在这里工作:一个显示初始页面,一个处理POST到上传表单。
问题在于第二个请求(POST)。这段代码应该受到责备:
<form enctype="multipart/form-data" action="<?php print $_SERVER['PHP_SELF'] ?>" method="post">
这不是在传递$_REQUEST['place_id']
中的ID。在那行下面加上这行:
<input type="hidden" name="place_id" value=<?php echo HtmlspecialChars($_REQUEST['place_id']); ?>" />
这将在提交表单时将place_id
传递给第二个请求。