【发布时间】:2020-02-19 13:11:09
【问题描述】:
我有两个表:类别和模型。目前,类别表很重要。
| id | nazwa | desc_s1 | photo | active | id_user |
| 1 | Audi | test | url | 1 | 2 |
| 2 | BMW | test | url | 1 | 2 |
| 3 | Ford | test | url | 1 | 2 |
| 4 | BMW | test | url | 1 | 2 |
我有 php:
$query_categories = "SELECT * FROM `categories` WHERE nazwa='$selected_category' AND active=1 AND id_user=".$_SESSION['id_user'].";";
$query_model = "SELECT * FROM models WHERE active=1";
$result_categories = mysqli_query($con, $query_categories);
$result_model = mysqli_query($con, $query_model);
if (mysqli_num_rows($result_categories) > 0 && mysqli_num_rows($result_model) > 0) {
$row_categories = mysqli_fetch_assoc($result_categories);
$selected_category = $row_categories['nazwa'];
$_SESSION['selected_category'] = $selected_category;
$_SESSION['title_content'] = $title_content;
$_SESSION['price'] = $price;
$category = $row_categories['nazwa'];
$desc_s1 = $row_categories['desc_s1'];
$photo = $row_categories['photo'];
$row_model = mysqli_fetch_assoc($result_model);
$selected_model = $row_model['model'];
$stan = $row_model['stan'];
$przeznaczenie = $row_model['przeznaczenie'];
$kolor = $row_model['kolor'];
$typ = $row_model['typ'];
$table = array();
do
{
array_push($table, '
<tr>
<td class="text-left">353</td>
<td class="text-left">Stan|' . $row_model['stan'] . '<br />Przeznaczenie|' . $row_model['przeznaczenie'] . '<br />Kolor|' . $row_model['kolor'] . '<br />Typ|' . $row_model['typ'] . '<br /></td>
<td class="text-left">' . str_replace(' ', '_', $row_model['model']) . '<br /></td>
<td class="text-left">' . $title_content . ' ' . $selected_category . ' ' . $row_model['model'] . '</td>
<td class="text-left">' . $price . '</td>
<td class="text-left">' . nl2br($row_categories['photo']) . '</td>
<td class="text-left">' . nl2br(htmlspecialchars($row_categories['desc_s1'])) . '</td>
</tr>');
} while ($row_model = mysqli_fetch_array($result_model));
} else {
array_push($errors, '<span class="badge badge-warning">Error</span>');
}
它的工作但是,输出表给了我 3 个结果,缺少的是最后一个。我知道原因是与 row id=2 一样的名称,但是如何获取结果中的所有行?我尝试了 while inside while,但它并没有像我想象的那样工作。
【问题讨论】:
-
您的代码易受 SQL 注入攻击。您应该使用准备好的语句。
-
你为什么使用do/while?只需使用 foreach。