我在我的javascript中面临某种问题,我认为它正在努力打开一个小窗口,但我不知道它现在是如何工作的。这里是我的javascript代码下面我的php和html代码
<?php
require_once '../core/init.php';
$id = $_POST['id'];
if(is_numeric($id) && $id > 0 && $id == round($id, 0)){
$id = $_POST['id'];
} else {
exit;
}
$id = (int)$id;
$sql = "SELECT * FROM products WHERE id = '$id'";
$result = $db->query($sql);
$product = mysqli_fetch_assoc($result);
$brand_id = $product['brand'];
$sql = "SELECT brand FROM brand WHERE id = '$brand_id'";
$brand_query = $db->query($sql);
$brand = mysqli_fetch_assoc($brand_query);
$sizestring = $product['sizes'];
$size_array = explode(',', $sizestring);
?>
<!-- Details Modal -->
<?php ob_start(); ?>
<div class="modal fade details-1" id="details-modal" tabindex="-1" role="dialog" aria-labelledby="details-1" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button class="close" type="button" onclick="closemodal()" aria-label="close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title text-center"><?php echo $product['title']; ?></h4>
</div>
<div class="modal-body">
<div class="container-fluid">
<div class="row">
<div class="col-sm-6">
<div class="center-block">
<img src="<?php echo $product['image']; ?>" alt="<?php echo $product['title']; ?>" class="details img-responsive">
</div>
</div>
<div class="col-sm-6">
<h4>Details</h4>
<p><?php echo $product['description']; ?></p>
<hr>
<p>Price: $<?php echo $product['price']; ?></p>
<p>Brand: <?php echo $brand['brand']; ?></p>
<form action="add_cart.php" method="post">
<div class="form-group">
<div clss="col-xs-3">
<label for="quantity">Quantity:</label>
<input type="text" class="form-control" id="quantity" name="quantity">
</div>
</div>
<div class="form-group">
<label for="size">Size:</label>
<select name="size" id="size" class="form-control">
<option value=""></option>
<?php foreach($size_array as $string) {
$string_array = explode(':', $string);
$size = $string_array[0];
$quantity = $string_array[1];
echo '<option value="'.$size.'">'.$size.' ('.$quantity.' Available)</option>';
} ?>
</select>
</div>
</form>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button class="btn btn-default" onclick="closemodal()">Close</button>
<button class="btn btn-warning" type="submit"><span class="glyphicon glyphicon-shopping-cart"></span>Add To Cart</button>
</div>
</div>
</div>
</div>
<script>
function closemodal(){
jQuery ('#details-modal').modal('hide');
setTimeout(function(){
jQuery('#details-modal').remove();
jQuery('.modal-backdrop').remove();
},500);
}
</script>
<?php echo ob_get_clean(); ?>
答案 0 :(得分:0)
查看var_dump($product);
结果后:
array(10) {
["id"]=> string(1) "1"
["title"]=> string(12) "Levi's Jeans"
["price"]=> string(5) "29.99"
["list_price"]=> string(5) "39.99"
["band"]=> string(1) "1"
["categories"]=> string(1) "6"
["image"]=> string(24) "images/products/men4.png"
["description"]=> string(16) "Description here"
["featured"]=> string(1) "1"
["sizes"]=> string(14) "28:3,32:2,36:5"
}
很明显,您没有名为&#39;品牌in your
products`表的字段。
brand
的最近字段名称是band
,我认为这是一个错字。因此,如果您更改代码的这一行:
`$brand_id = $product['brand'];`
到此
`$brand_id = $product['band'];`
然后你将解决错误
注意:未定义的索引:品牌
此查询$sql = "SELECT * FROM products WHERE id = '$id'";
由于$id
为integer
,因此无需将其括在单引号中,您可以像这样查询:
$sql = "SELECT * FROM products WHERE id = $id";
同时查看您的查询,您有两个查询,您可以使用简单的join
你有'&#34; SELECT * FROM产品WHERE id =&#39; $ id&#39;&#34;和&#34; SELECT品牌FROM品牌WHERE id =&#39; $ brand_id&#39;&#34;
在表格products
中,字段band
是表brand
字段id
的外键所以您只需编写此查询:
$sql = "SELECT products.*, brand.brand FROM products left join brand on products.band = brand.id WHERE products.id = $id";
因此,php
文件顶部的detailsmodal.php
部分将如下所示:
<?php
require_once '../core/init.php';
$id = $_POST['id'];
if(is_numeric($id) && $id > 0 && $id == round($id, 0)){
$id = $_POST['id'];
} else {
exit;
}
$id = (int)$id;
$sql = "SELECT products.*, brand.brand FROM products left join brand on products.band = brand.id WHERE products.id = $id";
$result = $db->query($sql);
$product = mysqli_fetch_assoc($result);
$sizestring = $product['sizes'];
$size_array = explode(',', $sizestring);
?>
然后只在html
的这一行:
<p>Brand: <?php echo $brand['brand']; ?></p>
您必须将其更改为
<p>Brand: <?php echo $product['brand']; ?></p>
注意:由于您使用的是mysqli
,因此强烈建议您使用prepared statements而不是直接将变量放入查询中