如何进行 php 动态查询

im尝试根据用户选择的内容更改查询。在我的情况下,显示了两张带有从数据库中提取的标签的照片。根据单击的图像,我希望在查询中使用该标签,谁将显示数据库中与该标签关联的项目。我尝试使用全局变量,但它不适合我。我不知道如何在用户单击某些内容后将数据插入查询中


有一个代码片段,我从数据库中提取带有标签的照片,并根据我选择哪个,我希望“$modeliai_结果['Modelis']”(车辆模型标签)发送并在下面的另一个查询中使用


<div class="eile">

    <?php

    echo '

    <a href="standartiniskatalogas.php?page=kategorijos"/>

    <img src="data:image/jpeg;base64,'.base64_encode( $modeliai_results['Nuotrauka'] ).'"/>

    <div class="description">'.$modeliai_results['Marke'].' '.$modeliai_results['Modelis'].'

    </div></a>';

?>

来自第二个图像的页面,应根据从上一页中选择的内容执行查询:


<?php

    $kategorijos_sql="SELECT * FROM kategorijos (HERE SHOULD BE ADDED: "WHERE Modelis='MODEL NAME WHICH WAS SELECTED WHEN PRESSED ON IMAGE'";

    $kategorijos_query=mysqli_query($dbconnect, $kategorijos_sql);

    $kategorijos_results=mysqli_fetch_assoc($kategorijos_query);


do{

    echo '

    <div class="eile2">

    <a href="isore/isore.html">

    <img src="data:image/jpeg;base64,'.base64_encode( $kategorijos_results['Nuotrauka'] ).'"/>

    <div class="description">'.$kategorijos_results['Kategorija'].'</div>

    </a></div>'; /* uzdaro echo*/

    }

    while ($kategorijos_results=mysqli_fetch_assoc($kategorijos_query))

     ?>

我没有使用任何额外的工具或库。

选择对象 显示与对象关联的数据


慕尼黑5688855
浏览 120回答 1
1回答

守着一只汪

我的错误是我专注于“onclick”事件,该事件将设置全局变量,并且该全局变量将用于查询。问题是,php与java脚本不是那么大的朋友,并且会创建无法链接到相同内容的URL。解决方案是使用简单的东西“$_GET”。将 $_GET 分配给某个 php 变量,然后在“href”中使用它。这样,当您单击带有“href=”.$variable“的任何类型的对象时。> bla bla /a>“,您将被重定向到带有URL尾部的页面,您可以从中访问以在其他查询中使用&nbsp;<?php&nbsp; &nbsp; &nbsp; &nbsp; $model= $_GET['model']; //variable from URL tail&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $category= $_GET['category'];&nbsp; &nbsp; &nbsp; &nbsp; $category_sql="SELECT * FROM service WHERE Model='$model' AND Category='$category'"; //my SQL where i use variables selected from URL&nbsp; &nbsp; &nbsp; &nbsp; $category_query=mysqli_query($dbconnect, $category_sql);&nbsp; &nbsp; &nbsp; &nbsp; $category_results=mysqli_fetch_assoc($category_query);&nbsp; &nbsp; &nbsp; &nbsp; do{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo ' //print html together with php code&nbsp; &nbsp; &nbsp; &nbsp; <tr class="data">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="catalogue.php? page=service&model='.$model.'&category='.$category.'&service='.$category_results['Name'].'"> //here i make href which when clicked will pass values named with $ sign<img src="data:image/jpeg;base64,'.base64_encode( $category_results['Image'] ).'"/>//Here i take image from data base<br><button>'.$category_results['Name'].'</button></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> <p>'.$category_results['Description'].'</p> //I take values from data base&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>'.$category_results['Price'].' &euro;</td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>';&nbsp; &nbsp; }&nbsp; &nbsp; while ($category_results=mysqli_fetch_assoc($category_query)) //loop which cycles through all data base items&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;?>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript