请帮助解决这个小问题,提供一个大错误。我查询数据库以按字数输出内容限制。有效。但是我发现很难在另一页上显示全部内容。
以下是有效的数据库查询。
<?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
很抱歉,我不知道如何格式化此页面上的编码,因为我尽力使它变得混乱。请点我点。
最佳答案
在代码中添加一些验证。
$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>