单击数据库echo并从相同的ID获取信息

时间:2014-03-18 10:14:09

标签: php html css sql

第二次来这里寻求帮助。我必须将PHP用于学校项目,并且在有限的时间内很难了解您的需求。无论如何,我试图用产品图像和同一产品的名称制作一个表,但是我已经成功完成了。我想点击其中一个产品的名称,并从页面上其他地方的唯一ID上的相同数据库中获取信息,也许这是不可能的,或者我的代码只是没有做到这样做,但我认真的不知道目前该做什么,任何帮助将不胜感激。

<?php
ini_set('display_errors', 'On');
require_once 'dbconfig.php';

$result = mysqli_query($con,"SELECT * FROM produkt JOIN bild ON bild.idBild = produkt.idProdukt JOIN ingrediens ON ingrediens.idIngrediens = produkt.idProdukt");
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> 
<html>
<head>
<title>Idun af Varla goes</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="stil.css" media="screen">
</head>
<body>
<center>

<div id="container-banner">
<div id="banner">
    <p>
        <img src="banner.png">
    </p>
</div>
</div>

<div id="container-smallstandard">
<div id='cssmenu'>
<ul>   
<li>
    <a href='#'>
    <span>Nyheter</span>
    </a>
</li>

<li class='active'>
    <a href='#'>
    <span>Produkter</span>
    </a>
</li>

<li>
    <a href='#'>
    <span>Återförsäljare</span>
    </a>
</li>

<li class='last'>
    <a href='#'>
    <span>Kontakta oss</span>
    </a>
</li>
</ul>
</div>
</div>

<div id="container-bigstandard">
<p>

</p>
</div>

<div id="container-bottomsmall">
<p>Mail.</p>
</div>

这是打印出我的图像和产品名称的php,我点击了一个名称,我希望该唯一条目的ID在页面的另一部分打印出具有相同ID的其余行。

<div id="container-bigstandardphp" align="left">
<p>

<?php
while($row = mysqli_fetch_array($result))
{
    $imgData = base64_encode($row['Bild']);
    ?>
    <table class = "blubb">
    <tr>
        <td>
            <img src="data:image/jpeg;base64,<?php echo $imgData ?>" />     </td>
    </tr>
    <tr>
        <td>
            <?php echo $row['Produkt_Namn'] ?>
        </td>
    </tr>
    </table>
<?php } ?>
</p>
</div>
</center>

<?php
mysqli_close($con);
?>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

您需要使用Ajax

$("#unique_id").on("click", function(e) {
    $.ajax({ 
        url: "your_endpoint",
        data: $(this).attr("id").serialize();
        success: function(data) { 
           //append data
        },
        error: function(data) {
           //append data
        }
    });
});

答案 1 :(得分:0)

我猜你会得到类似Produkt_IdProdukt_Namn的内容。 将<td><?php echo $row['Produkt_Namn'] ?></td>替换为:

<a href="edit.php?id="<?php echo $row['Produkt_Id'] ?> >
 <td><?php echo $row['Produkt_Namn'] ?></td>
</a>

制作新页面edit.php并输入以下代码:

<?php
  $id = $_GET["id"];
  // You'll get the product id in $id.
  // Run the `SELECT` query for what ever fields you want of that id.
  // See example query:

  $sql = "SELECT * FROM produkt WHERE Produkt_Id=".$id; 
  $result = mysqli_query($sql) or die($sql);
  // I suppose 'Produkt_Id' is the field which contains id of 
  // the product in the database.

  //You'll be getting the product info with $result.
  //Hope you can then display the info as per your need may be within a table or anything.
?>

此信息将显示在新页面上,即edit.php。如果您想在上一页显示信息,那么只需在上一页上使用iframe。

希望这会有所帮助。