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


Clicking on database echo and get info from the same ID

我第二次来这里求助了。我必须使用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>

您需要使用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
        }
    });
});

我猜你会得到这样的东西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。

希望这对你有帮助。