<?php
$result = mysqli_query($conn,"SELECT * FROM news ORDER BY date DESC
LIMIT 5")or die(mysql_error());
if (!$result) {
printf("Error: %s\n", mysqli_error($conn));
exit();
}
echo "<table align='left' CELLPADDING='50' >";
while($row = mysqli_fetch_array($result))
{
if ($row['photoid'] == NULL){
$date_string = $row['date'];
$date = strtotime($date_string);
$date = date('m/d/y', $date);
echo "<tr>";
echo "<td style='width: 750px'>" .$row['title'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width: 700px'>" . $row['news'] . "</td>";
echo "<td style='width: 100px'>" . $date . "</td>";
echo "</tr>";
}
else
{
$result = mysqli_query($conn,"SELECT news.title,news.date,news.news,photo.photo FROM news, photo WHERE news.photoid = photo.photoid ORDER BY date DESC") or die(mysql_error());
$row = mysqli_fetch_array($result);
$date_string = $row['date'];
$date = strtotime($date_string);
$date = date('m/d/y', $date);
echo "<tr>";
echo "<td style='width: 750px'>" . $row['title'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width: 700px'>" . $row['news'] . "</td>";
echo "<td style='width: 100px'>" . $date . "</td>";
echo "</tr>";
echo "<td style='width: 800px'>" . '<img height="300" width="300" src="data:image/jpeg;base64,'.base64_encode( $row["photo"] ).'" >' . "</td>";
}
}
echo "</table>";
mysqli_close($conn);
?>
所以网站显示一个新闻栏,代码从数据库中获取新闻数据,如果有一个photoid(如果新闻有照片),那么它会将新闻添加到带有照片的表格中。如果没有照片,则在没有照片的情况下将新闻添加到桌面。简单。在测试的那一刻,我有两篇带有照片的新闻文章,第一篇完美地工作,然后是第二次错误
Undefined index: photoid in
对于行if ($row['photoid'] == NULL){
。有一个photoid。
答案 0 :(得分:0)
在检查值之前,您需要检查是否设置了变量/键。试试这个:
if (isset($row['photoid']) && $row['photoid'] == NULL){