2016-07-21 274 views
1

我一直在使用我的网站 - 这是一个针对不同商品(这也是我的第一个网站)的商店。我刚刚完成了网站的HTML/CSS部分,但我希望使其更加动态和易于编辑,而无需一次更改多个页面。我认为我可以做到这一点,也使网站搜索的实现更容易,方法是将数据库中的项目的名称和价格与PHP代码一起检索名称和价格,并将其插入项目图像的下方。不幸的是,我完全不知道如何去做这件事。我试过寻找其他人有类似的问题,但没有一个人似乎特别是我在找什么。我将把我的PHP代码放在下面,然后我会放一些HTML代码来显示我想放置信息的位置。如何从MySQL数据库获取数据并将其插入到文本中

<?php 
$servername = "localhost"; 
$username = "root"; 
$password = "testpw"; 
$dbname = "testdb"; 

$conn = new mysqli($servername, $username, $password, $dbname); 
if($conn->connect_error) { 
} 
$sql = "SELECT id, name, price FROM items"; 
$result = $conn->query($sql); 

if ($result->num_rows > 0) { 
while($row = $result->fetch_assoc()) { 
echo " " .$row["id"]. " " .$row["name"]. " " .$row["price"]. "<br>"; 
} 
} else { 
echo "0 results"; 
} 
$conn->close(); 

?> 

这就是我用来获取每个项目信息的PHP代码。但问题是,它获取每个项目的信息并立即显示。我希望代码检查项目的ID,然后检索关于它的统计信息并显示它们。这意味着如果我想重新安排商店中的商品,我只需要更改ID,这使我更容易操作商店及其中的商品。以下是项目的HTML代码。

<div id="images"> 
<figure> 
    <img class="gunimage" id="(THIS IS THE ID USED TO GET THE INFO)" src="idk.png" width="225" height="190" alt=""> 
    <figcaption><h3>Name: (INSERT NAME HERE USING PHP) </h3></figcaption> 
    <figcaption><h5>Price: (INSERT OTHER PRICE HERE)/month</h5></figcaption> 
    <figcaption><div id="tfbutton5"><form action="buy.html"><input type="submit" value="Rent" class="tfbutton5"></form></figcaption> 
</div> 
</figure> 
</div> 

我标记,我想的信息去了,我希望有ID的事情,所以PHP代码知道它是什么项目。我知道两个代码都是分开工作的,因为我已经多次测试了这两个代码。很抱歉,如果我的问题很长,我想尽可能具体,因此很明显我的问题是什么。请原谅我的代码是否需要进行很多更改才能适用于我描述的情况,因为我只能弄清楚如何制作列出每个项目信息的内容。任何帮助,将不胜感激。谢谢。

+0

https://en.wikipedia.org/wiki/Where_(SQL) –

+0

我在哪里可以在我的代码中添加该商品以仅获取商品的信息与该集ID? – Nathan

+0

'$ sql =“SELECT ID,name,price FROM items WHERE ID ='the_item_id'”;'如果你也想要项目的统计信息,查询更复杂,因为它涉及至少一个JOIN,但这取决于关于你的数据库结构 – Gianluca

回答

0

如果你想要做更多的东西,你可以把一个名为
“connection.php”或更具描述性的文件

try { 
    $servername = 'localhost'; 
$username = 'root'; 
$password = 'testpw'; 
$dbname = 'testdb'; 
$db = new mysqli($servername, $username, $password, $dbname); 
} catch(Exception $e) { 
    echo $e->error_list; 
    exit; 
} 
function execute_query($con, $query, $variables) { 
$stmt = $con->prepare($query); 
$stmt->execute($variables); 
return $stmt; 
} 

,你可以在当一个connection.php拆分这些顶部如果您有多个函数,则在function.php文件中运行。

include ('connection.php'); 
$id_number = $_GET['id']; 
# !! NOTICE if grabbing | my understanding another page | 
// filter if grabbing from GLOBAL Variable $_GET 
$id = filter_var($id_number, FILTER_SANITIZE_NUMBER_INT); //take out 
if not grabbing from outside source. 
$con = $db; 
try { 
    $query = 'SELECT t.id as id , t.name as name, t.price as price 
    FROM items as t 
    WHERE t.id = :id'; 
$variables[':id'] = $id; 
    $items = execute_query($con, $query, $variables); 
}catch(mysqli_error_list $e) { 
    echo $e->getMessage(); 
} 
?> 
<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <meta http-equiv="X-UA-Compatible" content="ie=edge"> 
    <title>Document</title> 
</head> 
<body> 
<div id="images"><!-- i am guessing you want it within this div--> 
<?php if(isset($item)) { ?> 
<?php foreach ($items as $item) { ?> 
<figure> 
     <img class="gunimage" id="<?php echo $item['id']; ?>" 
src="idk.png" width="225" height="190" alt="<?php echo $item['name']; 
?>"> 
    <figcaption><h3>Name: <?php echo $item['name']; ?> </h3> 
</figcaption> 
    <figcaption><h5>Price: <?php echo $item['price']; ?> </h5> 
</figcaption> 
    <figcaption> 
    <!-- instead of a id tfbutton5 needs to be a class otherwise your 
    going to have an issue here when it loops, you will have an error--> 
    <div class="tfbutton5"><form action="buy.html"><input 
    type="submit" value="Rent" class="tfbutton5"></form> 
    <!--also, not sure why you have an Id and a class named exactly 
identical but it is bad practice. sorry do not do it. --> 
    </figcaption> 
    </div> 
    </figure> 
    <?php } ?> 
<?php } ?> 
</div> 
</body> 
</html> 

,如果你想要一个图像源,则需要输入到数据库中,你的项目表具有say..rows编号,图片,和项目的ID表连接到图像表。然后使用商品ID作为商品和图像之间的点

相关问题