显示数据库查询的最终结果时出错

时间:2014-05-03 07:00:15

标签: php mysql

请帮助排序这个小问题,给出一个大错误。我查询数据库以通过多个单词输出内容限制。有效。但我发现很难在另一页上显示完整内容。  以下是有效的数据库查询。

<?php 
    require_once ('inc/mysqli_connect.php');// Connect to the db.
    $q = "select id,SUBSTRING_INDEX(description,' ',250) AS responsibity, SUBSTRING_INDEX(qualification,' ',250) AS qualification,(post) AS position FROM career ORDER BY id DESC LIMIT 15";
    $r = @mysqli_query ($dbc, $q); // Run the query.

    if ($r) { // If it ran OK, display the records.
        echo '<h2>Open Positions</h2>';

    // Fetch and print all the records:
              while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
              echo'<ul>';
              echo '<li><a href="http://localhost/sitename/career-details.php?id
='.htmlentities($row['id']).'">'.htmlentities($row['position']).'</a>.</li>';
              echo '</ul>';
        }
        mysqli_free_result ($r); // Free up the resources.  

    } else { // If it did not run OK.

        // Public message:
        echo '<p class="error">The are no latest job openings please. We apologize for any inconvenience.</p>';

        // Debugging message:
        echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>';

    } // End of if ($r) IF.
     ?>

我发现问题的另一页。

<?php
$page_title = 'View career details';
require_once ('inc/mysqli_connect.php');// Connect to the db.

$id= $_GET['id'];
$q = "SELECT * FROM career WHERE id= '$id' ";
$r = @mysqli_query ($dbc, $q); // Run the query.

if ($r) { // If it ran OK, display the records.
    echo '<h2>Career Details</h2>';

// Fetch and print all the records:
          while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
          echo $row['position'];
          echo '<br>'.$row[' responsibility'];
          echo '<br>'. $row['qualification'];
          echo '<a href="apply.php>Apply.</a>';
    }
    mysqli_free_result ($r); // Free up the resources.  

} else { // If it did not run OK.

    // Public message:
    echo '<p class="error">Could not bring you job description due to error. We apologize for any inconvenience.</p>';

    // Debugging message:
    echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>';

      } // End of if ($r) IF.


      ?>

因此给出错误。

An error occurred in script 'C:\wamp\www\sitname\career-details.php' on line 74: Undefined index: id

很抱歉,我不知道如何在此页面上格式化编码,因为我尝试了所有我无法使其变得混乱。请指点我。

2 个答案:

答案 0 :(得分:0)

在代码中添加一些验证。

$id = null;
if(isset($_GET['id'])){
   $id= intval($_GET['id']);
}
$q = "SELECT * FROM career WHERE id= '$id' ";
$r = @mysqli_query ($dbc, $q); // Run the query.
$rowcount = mysqli_num_rows($r);
if($rowcount > 0){
   // Fetch and print all the records:
}

此外,您还有一些类型错误,例如

echo '<br>'.$row[' responsibility']; // '<br>$row['responsibility']
echo '<a href="apply.php>Apply.</a>'; // <a href="apply.php">Apply.</a>

答案 1 :(得分:0)

您在第一个查询中使用列别名:

SUBSTRING_INDEX(description,' ',250) AS responsibity
(post) AS position

您在第二个查询中使用SELECT *。您只能使用原始列名称,而不是$row['responsibility']$row['position']