【发布时间】:2019-06-17 12:23:59
【问题描述】:
我一直在为我拥有的一家企业开发一个网站,但我对我的 PHP 和 SQL 有点生疏。我能够在 PHP 中创建我想要的模型商店/库存数组,但我一直在尝试将 MySQL 数据库中的表数据插入到我的 PHP 数组中。我想要做的是复制我的模型,但是,我不想从我的inventoryArray.php 中定义的数组中提取数据,而是从我在 MySQL 中创建的表中提取数据。
样机如下:
shop.php(当前):
<?php foreach ($inventory as $handgun) {?>
<div class="column">
<h5><?php echo "$handgun[model]"?></h5>
<img class="thumbnail"
src="assets/style/images/inventory/pistols/<?php echo "$handgun[img]" ?>.png">
<table class="shopTables">
<tr>
<th>MPN:</th>
<td><?php echo "$handgun[mpn]"?></td>
</tr>
<tr>
<th>UPC:</th>
<td><?php echo "$handgun[upc]"?></td>
</tr>
<tr>
<th>Accessories:</th>
<td><?php echo "$handgun[accessories]"?></td>
</tr>
<tr>
<th>Description:</th>
<td><?php echo "$handgun[description]"?></td>
</tr>
</table>
<a href="product-page.php" class="button expanded">View</a>
</div>
<?php } ?>
inventoryArray.php(之前):
<?php
$inventory = array(
array(
'action' => "Striker-fired",
'category' => "Pistols",
'cal_ga' => "9mm",
'manufacturer' => "FN Herstal",
'model' => "FN 509 Midsize",
'UPC' => "845737010010",
'img' => "FN509M",
'price' => "$649"
),
array(
'action' => "SA/DA",
'category' => "Pistols",
'cal_ga' => "9mm",
'manufacturer' => "CZ USA",
'model' => "CZ P01",
'UPC' => "806703911991",
'img' => "CZP01",
'price' => "$627"
)
);
?>
我没有更改 shop.php 的任何内容,以下是我尝试在我的 inventoryArray.php 文件中使用我的 MySQL 表中的数据:
inventoryArray.php(之后):
require_once ('../mysqli_connect.php');
$query = "SELECT * FROM firearms";
$response = @mysqli_query($dbc, $query);
while ($row= mysqli_fetch_array($response)) {
$inventory = array(
array(
'model' => $row['model'],
'img' => $row['img'],
'mpn' => $row['mpn'],
'upc' => $row['upc'],
'accessories' => $row['accessories'],
'description' => $row['description']
)
);
}
mysqli_close($dbc);
虽然这会将数据从我的表中插入到我的数组中,但将新库存插入到我的 SQL 表中不会创建类似于我的模型的新嵌套数组。相反,它似乎覆盖了原始数组。
【问题讨论】:
-
$inventory = array(每次都会覆盖$inventory。
标签: php mysql arrays database mysqli