为什么链接查询数据库不起作用?

时间:2013-03-27 09:33:33

标签: php mysql

我有一个显示链接的页面,所以当我点击该链接时,它应该带我到另一个页面并查询数据库,以便我只能看到该特定链接的信息。但现在它给我一个问题,它与我在其他程序中使用的代码相同。我一直在努力解决这个问题近两个小时。请在下面提供代码,欢迎任何可以帮助您的人。

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

2 个答案:

答案 0 :(得分:2)

您在第一次$job_id = $row['job_id'];

时忘记添加while的可能性

答案 1 :(得分:1)

$ job_id似乎没有定义,并且使用echo-statement创建的链接不是您想要的(我希望...)Option1不会为job_id分配实际值(它指定字符串'$ job_id “)。

Option2执行操作并将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>';

逗号已被句号所取代,因为。用于连接字符串。