为什么链接到查询数据库不起作用?
我有一个显示链接的页面,所以当我点击该链接时,它应该带我到另一个页面并查询数据库,以便我只能看到特定链接的信息。但现在它给了我一个问题,它是我在其他程序中使用的相同代码。我一直试图弄清楚这个问题差不多2个小时。请在下面列出任何可以帮助你的人的代码。为什么链接到查询数据库不起作用?
First piece of code or page
<?php
$selector1 =("SELECT job_id, job_title, job_description,
job_category, wantORoffering, address, cell, email, date_registered,
user_id from h2s_job ORDER BY date_registered DESC LIMIT $offset, $rowsperpage");
$result_selector=$con->prepare($selector1);
$result_selector->execute();
while($row = $result_selector->fetch(PDO::FETCH_ASSOC)){
#substring
$job_description = $row['job_description'];
$job_description = wordwrap($job_description , 200);
$job_description = explode("\n", $job_description);
$job_description = $job_description [0] . '...';
#substring
echo '<a href="jobdetails.php?job_id=$job_id"><div id="record" id ="record-">
<strong class="pic"><img src="images/nopic.png" ></strong>
<strong class="job_title"><h4>',$row['job_title'],'</h4></strong></br>
<strong class ="desc"><p>',$job_description,'</p></strong></br>
<strong class="contact"><p>',$row['cell'],'</strong></br>
<strong class="time"><p>',$row['date_registered'],'</strong></br>
</div></a>';
}?>
Second page
<?php
$selector =("SELECT job_title, job_description,
job_category, wantORoffering, address, cell, email, date_registered,
user_id FROM h2s_job WHERE job_id =".$_GET['job_id']);
$result_selector=$con->prepare($selector);
$result_selector->execute();
while($row = $result_selector->fetch(PDO::FETCH_ASSOC)){
#substring
$job_description = $row['job_description'];
echo '<div id="record" id ="record-">
<strong class="pic"><img src="images/nopic.png" ></strong>
<strong class="job_title"><h4>',$row['job_title'],'</h4></strong></br>
<strong class ="desc"><p>',$job_description,'</p></strong></br>
<strong class="contact"><p>',$row['cell'],'</strong></br>
<strong class="time"><p>',$row['date_registered'],'</strong></br>
</div></a>';
}
?>
我得到
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column '$job_id' in 'where clause'' in C:\wamp\www\houseCurrent\jobdetails.php:98 Stack trace: #0 C:\wamp\www\houseCurrent\jobdetails.php(98): PDOStatement->execute() #1 {main} thrown in C:\wamp\www\houseCurrent\jobdetails.php on line 98
$ JOB_ID添加$job_id = $row['job_id'];
似乎并没有被定义的错误,并与回声声明ISN创建的链接'你想要什么(我希望...)Option1没有分配job_id的实际值(它分配字符串'$ job_id')。
选项2的伎俩,并分配到的job_id值5
<?php
$job_id = 5; //example
//OPTION 1 This would output to html => jobdetails.php?job_id=$job_id
echo '<a href="jobdetails.php?job_id=$job_id">test</a>';
//OPTION 2 This would output to html => jobdetails.php?job_id=5
echo '<a href="jobdetails.php?job_id=' . $job_id . '">test</a>';
?>
你可以改变...(选项1)
echo '<a href="jobdetails.php?job_id=$job_id"><div id="record" id ="record-">
<strong class="pic"><img src="images/nopic.png" ></strong>
<strong class="job_title"><h4>',$row['job_title'],'</h4></strong></br>
<strong class ="desc"><p>',$job_description,'</p></strong></br>
<strong class="contact"><p>',$row['cell'],'</strong></br>
<strong class="time"><p>',$row['date_registered'],'</strong></br>
</div></a>';
到(选项2)
echo '<a href="jobdetails.php?job_id=' . $job_id . '"><div id="record" id ="record-">
<strong class="pic"><img src="images/nopic.png" ></strong>
<strong class="job_title"><h4>' . $row['job_title'] . '</h4></strong></br>
<strong class ="desc"><p>' . $job_description . '</p></strong></br>
<strong class="contact"><p>' . $row['cell'] . '</strong></br>
<strong class="time"><p>' . $row['date_registered'] . '</strong></br>
</div></a>';
逗号已被句点替换,因为。用于串联字符串。
逗号不是句号? – Waygood 2013-03-27 10:01:13
'。 $ job_id。 '而不是只有$ job_id。 – bestprogrammerintheworld 2013-03-27 10:03:52
啊,现在我明白你的意思了。我只是粘贴了原始代码。我将编辑。感谢您的观察! – bestprogrammerintheworld 2013-03-27 10:04:48
查看为变量使用占位符。然后将$ job_id变量绑定到:job_id占位符http://php.net/manual/en/pdo.prepare.php – Waygood 2013-03-27 09:35:40
' hjpotter92 2013-03-27 09:38:01
是否在URL中传递了正确的ID? – 2013-03-27 09:38:07