我一直盯着这个问题好几个小时。对于我的生活,我找不到解释。 PHP无法找到userid和subgenre元素。找到所有其他人。
HTML表单
<form action="php/writers.php" method="post" enctype="multipart/form-data" target="upload-target" onsubmit="startUpload();" >
<h1>Writers</h1>
<p>
<input id="title" name="title" class="form-control" placeholder="Title" required autofocus="true" />
</p>
<p>
<label for="work-type" class="fixed50">Form:</label>
<select id="work-type" name="work-type">
<option value="fiction">Fiction</option>
<option value="non-fiction">Non-Fiction</option>
<option value="screenplay">Screenplay</option>
<option value="play">Play</option>
</select>
</p>
<p>
<p>For an explanation of the genres shown here, see <a target="_blank" href="https://en.wikipedia.org/wiki/List_of_genres">List of genres</a></p>
<label for="genre" class="fixed50">Genre:</label>
<select id="genre" name="genre">
</select>
</p>
<p>
<label for="subgenre" class="fixed50">Sub-Genre:</label>
<select id="subgenre" name="subgenre">`
<option value="0">None</option>
</select><br/>
</p>
<p>
<label for="nbrPages" class="fixed50">Number of pages:</label>
<input id="nbrPages" name="nbrPages" required style="width: 48px" placeholder="Pages" /><br/>
</p>
<p>For a limied time, writers can upload their sysnopsis or query letterr for <span style="color: #f00; font-weight:bold">FREE</span>. We reserve the right to change this policy without notice.</p>
<div id="tips">The objective of a sysnopsis or query letter is to entice enablers into requesting your manuscript.
It must be brief and to the point, now more than twp pages and, of course very well written.
</div>
<div class="form-group">
<p id="file-warning">Your synopsis or query letter must be a PDF file.
<a target="_blank" href="https://www.freepdfconvert.com/" target="_blank">Free file conversion to PDF</a></p>
</div>
<div class="form-group">
<label for="file2upload">PDF to Upload</label>
<input id="file2upload" name="file2upload" type="file" required value="File to upload" />
</div>
<div id="recaptcha-elements"></div>
<div class="form-group">
<button type="submit" id="writers-submit" class="btn btn-default">Submit</button>
</div>
<input id="userid" name="userid" type="hidden" />
<iframe id="upload-target" name="upload-target" src="#" ></iframe>
</form>
以下JS函数将正确的值写入控制台。
JS
function startUpload() {
$("#userid").val(window.localStorage.getItem('user-id'));
console.log("jquery val=" + $("#subgenre").val());
console.log("getElementById value=" + document.getElementById('subgenre').value);
showMessage(1, "Uploading...");
return true;
}
的console.log
userid = 51 VM39985:122
jquery val = 0 VM39985:123
getElementById value = 0
/php/writers.php:1 POST 500(内部服务器 误差)
PHP
$uploadDirectory = '/home/deje/public_html/writers-tryst/uploads/'; //specify upload directory ends with / (slash)
require_once "dbconnect.php";
$result = 0;
$File_Name = basename($_FILES['file2upload']['name']);
$File_Ext = substr($File_Name, strrpos($File_Name, '.')); //get file extention
$Random_Number = rand(0, 9999999999); //Random number to be added to name.
$NewFileName = $Random_Number.$File_Ext; //new file name
var_dump($_REQUEST);
$target_path = $uploadDirectory . $NewFileName;
if (@move_uploaded_file($_FILES['file2upload']['tmp_name'], $target_path)) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime_type = finfo_file($finfo, $uploadDirectory . $NewFileName);
finfo_close($finfo);
if ($mime_type != 'application/pdf') {
unlink($UploadDirectory . $NewFileName);
$data = array('File MUST be a PDF!');
$result = 0;
} else $result = 1;
}
if (!isset($_REQUEST["title"]) || empty(trim($_REQUEST["title"])))
throw new Exception('You must enter a title.');
else {
$title = filter_var(trim($_REQUEST["title"]), FILTER_SANITIZE_STRING);
$title = htmlspecialchars_decode($title, ENT_QUOTES);
}
if (!isset($_REQUEST["userid"]) || empty(trim($_REQUEST["userid"])))
throw new Exception('Userid is missing.');
else {
$userrid = filter_var(trim($_REQUEST["userid"]), FILTER_SANITIZE_STRING);
$userid = htmlspecialchars_decode($userid, ENT_QUOTES);
}
if (!isset($_REQUEST["work-type"]) || empty(trim($_REQUEST["work-type"])))
throw new Exception('You must enter a work type.');
else {
$worktype = filter_var(trim($_REQUEST["work-type"]), FILTER_SANITIZE_STRING);
$worktype = htmlspecialchars_decode($worktype, ENT_QUOTES);
}
if (!isset($_REQUEST["genre"]) || empty(trim($_REQUEST["genre"])))
throw new Exception('You must enter a title.');
else {
$genre = filter_var(trim($_REQUEST["genre"]), FILTER_SANITIZE_STRING);
$genre = htmlspecialchars_decode($genre, ENT_QUOTES);
}
if (!isset($_REQUEST["subgenre"]) || empty(trim($_REQUEST["subgenre"])))
throw new Exception('You must enter a sub-genre.');
else {
$subgenre = filter_var(trim($_REQUEST["subgenre"]), FILTER_SANITIZE_STRING);
$subgenre = htmlspecialchars_decode($subgenre, ENT_QUOTES);
}
if (!isset($_REQUEST["nbrPages"]) || empty(trim($_REQUEST["nbrPages"])))
throw new Exception('You must enter the number of pages your work contains.');
else {
$nbrPages = filter_var(trim($_REQUEST["nbrPages"]), FILTER_SANITIZE_STRING);
$nbrPages = htmlspecialchars_decode($nbrPages, ENT_QUOTES);
}
$dbh = connect2DB();
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare(
"INSERT Writers(fkAccounts, Title, WorkType, Genre, SubGenre, Filename)
VALUES(:fk, :title, :worktype, :genre, :subgenre, :filename)"
);
$stmt->bindParam(':fk', $userid, PDO::PARAM_INT, 10);
$stmt->bindParam(':title', $title, PDO::PARAM_STR, 255);
$stmt->bindParam(':worktype', $worktype, PDO::PARAM_STR, 30);
$stmt->bindParam(':genre', $genre, PDO::PARAM_STR, 100);
$stmt->bindParam(':subgenre', $subgenre, PDO::PARAM_STR, 100);
$stmt->bindParam(':filename', $NewFileName, PDO::PARAM_STR, 30);
$stmt->execute();
echo "<script type='text/javascript'>stopUpload(" . $result . ");</script>";
sleep(1);
exit();
$data = array();
require_once 'cookies.php';
if (isset($_REQUEST['files'])) {
$error = false;
$files = array();
foreach ($_FILES as $file) {
if ($file["size"] > $filesize_limit) {
// $data = array('Maximum file size (' . $filesize_limit . ') exeeded.');
// $error = true;
} else {
$File_Name = basename($file['name']);
$File_Ext = substr($File_Name, strrpos($File_Name, '.')); //get file extention
$Random_Number = rand(0, 9999999999); //Random number to be added to name.
$NewFileName = $Random_Number.$File_Ext; //new file name
if (move_uploaded_file($file['tmp_name'], $UploadDirectory . $NewFileName)) {
$files[] = $UploadDirectory . $file['name']; //$NewFileName; //
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime_type = finfo_file($finfo, $UploadDirectory . $NewFileName);
//$data = array('Mime-Type (' . $mime_type . ')');
finfo_close($finfo);
if ($mime_type != 'application/pdf') {
unlink($UploadDirectory . $NewFileName);
$data = array('File MUST be a PDF!');
$error = true;
};
} else {
$error = true;
}
}
}
if ($error) {
$data = array('error' => $data);
} else {
$data = array('files' => $files);
}
} else {
$data = array('success' => 'Form was submitted', 'formData' => $_REQUEST);
};
echo json_encode($data);
PHP error_log
[17-May-2016 00:59:49 UTC] PHP注意:未定义的变量:userid in 第34行/home/deje/public_html/writers-tryst/php/writers.php
[17-May-2016 00:59:49 UTC] PHP Stack trace:
[17-May-2016 00:59:49 UTC] PHP 1. {main}() /home/deje/public_html/writers-tryst/php/writers.php:0
[17-May-2016 00:59:49 UTC] PHP致命错误:未捕获的异常 &#39;异常&#39;有消息&#39;您必须输入子流派。&#39;在 /home/deje/public_html/writers-tryst/php/writers.php:49
堆栈追踪:
在第49行的/home/deje/public_html/writers-tryst/php/writers.php中抛出0 {main}
答案 0 :(得分:2)
首先,您在这两行上将一个不存在的变量传递给一个函数
$userrid = filter_var(trim($_REQUEST["userid"]), FILTER_SANITIZE_STRING);
$userid = htmlspecialchars_decode($userid, ENT_QUOTES);
当您对$userid
进行调用时未定义htmlspecialchars_decode
,这可能是您打算键入的错字
$userrid = filter_var(trim($_REQUEST["userid"]), FILTER_SANITIZE_STRING);
$userid = htmlspecialchars_decode($userrid, ENT_QUOTES);
第二个错误是PHP正在抛出这一行
if (!isset($_REQUEST["subgenre"]) || empty(trim($_REQUEST["subgenre"])))
throw new Exception('You must enter a sub-genre.');
那是因为情况很糟糕
名为subgenre
的select元素的值为0
,而PHP empty(trim("0"));
中的值为thruthy,因此if条件进入,并抛出异常。