动态Html页面从Mysql与PHP [英] Dynamic Html pages from Mysql with PHP

查看:125
本文介绍了动态Html页面从Mysql与PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用php和mysql在网站上工作,并且在数据库行中生成网页网址时遇到了一些问题。



我只有3个页面connection.php (mysql连接)index.php(其中显示带有产品详细信息URL的按钮的产品/内容缩略图)和details.php,我希望显示单个产品的信息。



从index.php我添加一个链接重定向到details.php页面与此:



< a href =details.php?id =<?php echo $ row ['ID'];?>



这是可行的,但大问题在details.php因为脚本不显示单个产品的详细信息,而是显示所有产品,请有人可以帮助我?谢谢

index.php 代码

  ......其他html代码...... 
< div class =row>
<?php

require_once'connection.php';

$ query =SELECT * FROM campi_name;
$ stmt = $ DBcon-> prepare($ query);
$ stmt-> execute();
while($ row = $ stmt-> fetch(PDO :: FETCH_ASSOC)){
?>


< div class =col-sm-4 col-md-3>
< div class =thumbnail>
< img src =<?php echo $ row ['Thumbnail'];?> alt =<?php echo $ row ['Title'];?>>
< div class =caption>
< h4><?php echo substr($ row ['Title'],0,30); ?>< / H4>
< p><?php echo $ row ['Brand']; ?>< / p为H.
<?php echo $ row ['ID']; ?>
< p>< a href =#class =btn btn-primary btn-lgrole =button> Cofronta< / a> < a href =dettagli.php?id =<?php echo $ row ['ID'];?> class =btn btn-default btn-lgrole =button> Dettagli< / a>< / p>
< / div>
< / div>
< / div>


<?php
}
?>
......其他html代码......

connection.php code

  $ DBhost =localhost; 
$ DBuser =root;
$ DBpass =;
$ DBname =prodotti;


$ b $尝试{
$ DBcon =新PDO(mysql:host = $ DBhost; dbname = $ DBname,$ DBuser,$ DBpass);
$ DBcon-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION);
} catch(PDOException $ ex){
die($ ex-> getMessage());
}

?>



detail.php 代码

  ......其他html代码...... 
< div class =container>


< div class =row>

<?php

require_once'connection.php';

$ query =SELECT * FROM campi_name;
$ stmt = $ DBcon-> prepare($ query);
$ stmt-> execute();
while($ row = $ stmt-> fetch(PDO :: FETCH_ASSOC)){
?>


< div class =col-sm-4 stylerow>
< a href =<?php echo $ row ['AffiliateLink'];?>类= 缩略图 >
< img src =<?php echo $ row ['Thumbnail'];?> alt =<?php echo $ row ['Title'];?>>
< / a>

< / div>


< div class =col-sm-8 stylerow>
< h2><?php echo $ row ['Title']; ?>< / H2>
< p><?php echo $ row ['Brand']; ?>< / p为H.
< button type =buttonclass =btn btn-primary btn-lg>亚马逊< / button>
< / div>

< / div>

< / div><! - /.container - >
......其他html代码......


解决方案

添加
$ id = $ _ GET ['id'];



编辑代码中的以下行

  $ query =SELECT * FROM campi_name; 

  $ query =SELECT * FROM campi_name where id ='$ id'; 


I'm working on a website with php and mysql and have some problems to generate web pages URL from database rows.

I have only 3 page connection.php (mysql connection) index.php (where show al products/contents thumbnails with button with product details URL) and details.php where i want show info for single product.

from index.php i add a link to redirect to details.php page with this:

<a href="details.php?id=<?php echo $row['ID']; ?>"

it's work but Big problem is in details.php because the script don't show a single products details, but show all products, please someone can help me? Thank you

index.php code

......other html code......
<div class="row">
<?php

require_once 'connection.php';

$query = "SELECT * FROM campi_name";
$stmt = $DBcon->prepare( $query );
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
?>


<div class="col-sm-4 col-md-3">
<div class="thumbnail">
<img src="<?php echo $row['Thumbnail']; ?>" alt="<?php echo $row['Title']; ?    >">
 <div class="caption">
 <h4><?php echo substr($row['Title'], 0, 30); ?></h4>
 <p><?php echo $row['Brand']; ?></p>
 <?php echo $row['ID']; ?>
 <p><a href="#" class="btn btn-primary btn-lg" role="button">Cofronta</a> <a href="dettagli.php?id=<?php echo $row['ID']; ?>" class="btn btn-default btn-lg" role="button">Dettagli</a></p>
 </div>
 </div>
 </div>


 <?php
 }
 ?>
 ......other html code......

connection.php code

$DBhost = "localhost";
$DBuser = "root";
$DBpass = "";
$DBname = "prodotti";



try {
$DBcon = new PDO("mysql:host=$DBhost;dbname=$DBname",$DBuser,$DBpass);
$DBcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $ex){
die($ex->getMessage());
}

?>

details.php code

......other html code......
<div class="container">


<div class="row">

<?php

require_once 'connection.php';

$query = "SELECT * FROM campi_name";
$stmt = $DBcon->prepare( $query );
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
?>


<div class="col-sm-4 stylerow">            
<a href="<?php echo $row['AffiliateLink']; ?>" class="thumbnail">
<img src="<?php echo $row['Thumbnail']; ?>" alt="<?php echo $row['Title']; ?  >">
</a>

</div>


<div class="col-sm-8 stylerow">
<h2><?php echo $row['Title']; ?></h2>
<p><?php echo $row['Brand']; ?></p>
<button type="button" class="btn btn-primary btn-lg">Amazon</button>
</div>

</div>

</div><!-- /.container -->
......other html code......

解决方案

Add $id=$_GET['id'];

edit following line in your code

$query = "SELECT * FROM campi_name";

To

$query = "SELECT * FROM campi_name where id='$id' ";

这篇关于动态Html页面从Mysql与PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆