所以这源于昨天的一个问题,由于错误不寻常,该问题迅速失控。这个问题仍然存在,但问题被搁置了,在这里,我被要求修改一个现在与当前问题相关的新问题。然后我在这里问了一个问题,事情很快就跑题了。我决定接受解决原始问题的答案并继续前进。我现在相信我们已经将问题缩小到一个非常具体的问题。
这个问题现在对我来说是 0 意义。我有以下代码
jQuery
$('#projects').click(function (e) {
$.trim(aid);
alert(aid);
$.ajax({
url:'core/functions/projects.php',
type: 'post',
data: {'aid' : aid},
done: function(data) {
// this is for testing
}
}).fail (function() {
alert('error');
}).always(function(data) {
alert(data);
$('#home_div').hide();
$('#pcd').fadeIn(1000);
$('#project_table').html(data);
});
});
PHP
<?php
include "$_SERVER[DOCUMENT_ROOT]/core/init.php";
if(isset($_POST['aid'])) {
$aid = $_POST['aid'];
try {
$query_projectInfo = $db->prepare("
SELECT projects.account_id,
projects.project_name,
projects.pm,
//..irrelevant code
FROM projects
WHERE account_id = ?
");
$query_projectInfo->bindValue(1, $aid, PDO::PARAM_STR);
$query_projectInfo->execute();
$count = $query_projectInfo->rowCount();
if ($count > 0) {
echo "<table class='contentTable'>";
echo "<th class='content_th'>" . "Job #" . "</th>";
echo "<th class='content_th'>" . "Project Name" . "</th>";
//..irrelevant code
while ($row = $query_projectInfo->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td class='content_td'>" . "<a href='#'>" . $row['account_id'] . "</a>" . "</td>";
echo "<td class='content_td'>" . $row['project_name'] . "</td>";
//..irrelevant code
echo "</tr>";
}
echo "</table>";
}
} catch(PDOException $e) {
die($e->getMessage());
}
} else {
echo 'could not load projects table';
}
?>
当我通过按“#projects”运行此代码时,我收到 2 个警报。第一个警报显示“6”,这是变量“aid”的值,是预期的。第二个警报是空白的。
现在这是我非常困惑的地方。如果我通过将PHP
文件更改为如下所示来简化事情并找到问题的根源
<?php
if(isset($_POST['aid'])) {
$aid = $_POST['aid'];
echo $aid;
} else {
echo 'fail';
}
响应现在是“6”!这意味着该文件正在接收我的 $_POST['aid'] 变量并将其正确设置在PHP
. 现在,如果我将代码改回来,我又什么也没有收到。
但是,如果我将原始PHP
文件更改为此
<?php
include "$_SERVER[DOCUMENT_ROOT]/core/init.php";
$aid = '6';
try {
$query_projectInfo = $db->prepare("
SELECT projects.account_id,
projects.project_name,
projects.pm,
//..irrelevant code
FROM projects
WHERE account_id = ?
");
$query_projectInfo->bindValue(1, $aid, PDO::PARAM_STR);
$query_projectInfo->execute();
$count = $query_projectInfo->rowCount();
if ($count > 0) {
echo "<table class='contentTable'>";
echo "<th class='content_th'>" . "Job #" . "</th>";
echo "<th class='content_th'>" . "Project Name" . "</th>";
//..irrelevant code
while ($row = $query_projectInfo->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td class='content_td'>" . "<a href='#'>" . $row['account_id'] . "</a>" . "</td>";
echo "<td class='content_td'>" . $row['project_name'] . "</td>";
//..irrelevant code
echo "</tr>";
}
echo "</table>";
}
} catch(PDOException $e) {
die($e->getMessage());
}
?>
然后PHP
直接运行文件查询成功,页面加载其动态创建的表。这让我头晕目眩,我想呕吐。
所以在审核中。援助变量设置为6,毫无疑问。如果我简化我的PHP
文件,它会接收 $_POST['aid'] 变量,获取数据,设置PHP
$aid 变量并回显它。但是,如果我尝试将其插入到我的查询中以使我的表膨胀,我将一无所获。现在,如果我将PHP
文件更改为不依赖 $_POST['aid'] 变量,它就可以工作。那么这里到底发生了什么?该PHP
文件在没有帖子的情况下工作,而帖子在没有表膨胀查询的情况下工作。
我真的希望有人能帮我解决这个问题,因为我完全迷路了
编辑
我改变了我的php文件进行测试。它现在看起来完全像这样。对不起墙
<?php
include "{$_SERVER['DOCUMENT_ROOT']}/TrakFlex/core/init.php";
if(isset($_POST['aid'])) {
$aid = $_POST['aid'];
echo $aid;
try {
$query_projectInfo = $db->prepare("
SELECT projects.account_id,
projects.project_name,
projects.pm,
projects.apm,
projects.est_start,
projects.est_end,
projects.contact,
projects.trips,
projects.tasks,
projects.perc_complete,
projects.bcwp,
projects.actuals,
projects.cpi,
projects.bcws,
projects.bac,
projects.comments,
projects.status,
projects.project_revenue,
projects.profit_margin,
projects.pm_perc,
projects.audited
FROM projects
WHERE account_id = ?
");
$query_projectInfo->bindValue(1, $aid, PDO::PARAM_STR);
$query_projectInfo->execute();
echo "<table class='contentTable'>";
echo "<th class='content_th'>" . "Job #" . "</th>";
echo "<th class='content_th'>" . "Project Name" . "</th>";
echo "<th class='content_th'>" . "PM" . "</th>";
echo "<th class='content_th'>" . "APM" . "</th>";
echo "<th class='content_th'>" . "Est. Start" . "</th>";
echo "<th class='content_th'>" . "Est. End" . "</th>";
echo "<th class='content_th'>" . "Contact" . "</th>";
echo "<th class='content_th'>" . "Trips" . "</th>";
echo "<th class='content_th'>" . "Tasks" . "</th>";
echo "<th class='content_th'>" . "% Complete" . "</th>";
echo "<th class='content_th'>" . "BCWP" . "</th>";
echo "<th class='content_th'>" . "Actuals" . "</th>";
echo "<th class='content_th'>" . "CPI" . "</th>";
echo "<th class='content_th'>" . "BCWS" . "</th>";
echo "<th class='content_th'>" . "BAC" . "</th>";
echo "<th class='content_th'>" . "Comments" . "</th>";
echo "<th class='content_th'>" . "Status" . "</th>";
echo "<th class='content_th'>" . "Project Revenue" . "</th>";
echo "<th class='content_th'>" . "Profit Margin" . "</th>";
echo "<th class='content_th'>" . "PM%" . "</th>";
echo "<th class='content_th'>" . "Audited" . "</th>";
while ($row = $query_projectInfo->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td class='content_td'>" . "<a href='#'>" . $row['account_id'] . "</a>" . "</td>";
echo "<td class='content_td'>" . $row['project_name'] . "</td>";
echo "<td class='content_td'>" . $row['pm'] . "</td>";
echo "<td class='content_td'>" . $row['apm'] . "</td>";
echo "<td class='content_td'>" . $row['est_start'] . "</td>";
echo "<td class='content_td'>" . $row['est_end'] . "</td>";
echo "<td class='content_td'>" . $row['contact'] . "</td>";
echo "<td class='content_td'>" . $row['trips'] . "</td>";
echo "<td class='content_td'>" . $row['tasks'] . "</td>";
echo "<td class='content_td'>" . $row['perc_complete'] . "</td>";
echo "<td class='content_td'>" . $row['bcwp'] . "</td>";
echo "<td class='content_td'>" . $row['actuals'] . "</td>";
echo "<td class='content_td'>" . $row['cpi'] . "</td>";
echo "<td class='content_td'>" . $row['bcws'] . "</td>";
echo "<td class='content_td'>" . $row['bac'] . "</td>";
echo "<td class='content_td'>" . $row['comments'] . "</td>";
echo "<td class='content_td'>" . $row['status'] . "</td>";
echo "<td class='content_td'>" . $row['project_revenue'] . "</td>";
echo "<td class='content_td'>" . $row['profit_margin'] . "</td>";
echo "<td class='content_td'>" . $row['pm_perc'] . "</td>";
echo "<td class='content_td'>" . $row['audited'] . "</td>";
echo "</tr>";
}
echo "</table>";
} catch(PDOException $e) {
die($e->getMessage());
}
} else {
echo 'could not load projects table';
}
?>
我在浏览器中得到的响应现在是这样的
6<table class='contentTable'><th class='content_th'>Job #</th><th class='content_th'>Project Name</th><th class='content_th'>PM</th><th class='content_th'>APM</th><th class='content_th'>Est. Start</th><th class='content_th'>Est. End</th><th class='content_th'>Contact</th><th class='content_th'>Trips</th><th class='content_th'>Tasks</th><th class='content_th'>% Complete</th><th class='content_th'>BCWP</th><th class='content_th'>Actuals</th><th class='content_th'>CPI</th><th class='content_th'>BCWS</th><th class='content_th'>BAC</th><th class='content_th'>Comments</th><th class='content_th'>Status</th><th class='content_th'>Project Revenue</th><th class='content_th'>Profit Margin</th><th class='content_th'>PM%</th><th class='content_th'>Audited</th></table>
这意味着两件事。正在传递和设置辅助变量。但是,当我在查询中运行它时,它会失败。但是如果我把 $aid = '6'; 然后将辅助绑定为它起作用的值。
为什么?
第二次编辑
因此,通过其他人的帮助,我们发现这是一个js
错误,而不是一个错误php
。该变量被设置为 6,js
但它有一些隐藏的字符,我想我找到了它们。当我查看萤火虫时,我将其视为来源
aid=%0D%0A6
这应该是一个换行符吧?我认为这就是导致错误的原因。
这就是我获得援助的方式
var title;
var aid;
$(".sa").click(function (e) {
title = $(this).text();
$.post('core/functions/getAccountId.php', {
title: title
})
.done(function(data) {
aid = data;
$("#acc_title").html(title);
$('#accountsSelectDiv').hide();
$('#acc_home').fadeIn(1000);
$('#home_div').show();
})
.fail(function(jqXHR, status, error) {
alert(error);
});
});
这可能是因为我如何得到它,我不知道。但是,如何删除源中的多余字符?这就是我认为的问题所在