本文介绍了在会话中保存数据库行 ID 以备后用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在 php 会话中保存行 $row['Id']
以便我可以在下一页使用这个变量来填充 $product_id
动态变量.
到目前为止,这是我所拥有的:
<?php $sql = "SELECT * FROM assortiment WHERE Categorie = '$productid' ORDER BY Id DESC ";$result = $conn->query($sql);如果($result->num_rows>0){//输出每一行的数据while($row = $result->fetch_assoc()) {SESSION_START();$identi = $row['Id'];$_SESSION["product_id"] = $identi;echo "<a href='/dbtest/dinner/amsterdam/".$row["Slug"]./".$row["Id"] ."'><div class='products'><div class='col-sm-3'><div class='product-img'>";echo "<img src='http://www.example.nl/Uploaded_files/Thumbs/" .$row['Fotomateriaal'].".jpg'>";echo "</div></div><div class='col-sm-6'>";echo "<div class='h2-container'><h2>".$row["产品"].</h2></div>".$row["Samenvatting"].".$_SESSION["product_id"] ."";echo "</div><div class='col-sm-3'><div class='col-sm-12 text-right'><p style='margin-bottom:20px;width:30px;'><i class='fa fa-heart' aria-hidden='true'></i></p>
";echo "
<tr><td><i class='fa fa-users' aria-hidden='true'></i></td><td><p>vanaf 10人</p></td></tr><tr><td><i class='fa fa-clock-o' aria-hidden='true'></i></td><td>" . $row["Tijdsduur"] ." uur</td></tr><tr><td><i class='fa fa-euro' aria-hidden='true'></i></td><td>vanaf " . $row["VerkoopPP40"] ."pp<small>excl btw</small></td></tr></table></div></div></a>";}} 别的 {echo "0 结果";}?>如您所见,我正在尝试保存 $row['Id']
.首先,我在 $identi 变量中创建此 ID,以便我可以回显它.然后我将 ID 保存在 $_session["product_id"]
中,稍后在 HTML 中回显它.这有效.但是当我点击链接进入下一页时,会话似乎没有保存.
我无法使用以下方法在下一页回显 product_id
:
我认为它没有被存储,或者(可能)我做错了什么?我在堆栈溢出上发现了一些在数据库查询的另一部分内启动会话的内容:
0) {while($row = mysqli_fetch_assoc($result)) {$fotos = $row["Fotomateriaal"];$productnaam = $row["Product"];$contenttext = $row["WebTekst"];$tijdsduur = $row["Tijdsduur"];$prijs = $row["VerkoopPP40"];$sub = $row["子类别"];$cate = $row["类别"];$seo = $row["SEO_Pagetitle"];$youtube = $row["Actief_Avontuur"];$_SESSION["product_id"] = $row["Id"];}} 别的 {$value = "Geen 结果";?>
但这也没有给我什么.有谁知道为什么这不起作用.通常,如果我想像往常一样在会话中存储变量(仅使用 PHP 和 Wordpress),则一切正常,但这只是行不通.任何帮助将不胜感激.
解决方案
其实你写的session_start();
写错了(SESSION_START();
)和错误的地方(在 while()
内).
在每个 php 页面上,在启动
之后,在每个页面的顶部添加 session_start();
注意:- session_start();
需要获取+操作SESSION
数据
I'm trying to save the row $row['Id']
in a php session so I can use this variable on the next page to have it fill the $product_id
variable dynamically.
So far this is what I've got:
<?php $sql = "SELECT * FROM assortiment WHERE Categorie = '$productid' ORDER BY Id DESC ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
SESSION_START();
$identi = $row['Id'];
$_SESSION["product_id"] = $identi;
echo "<a href='/dbtest/dinner/amsterdam/".$row["Slug"]. "/" . $row["Id"] . "'><div class='products'><div class='col-sm-3'><div class='product-img'>";
echo "<img src='http://www.example.nl/Uploaded_files/Thumbs/" .$row['Fotomateriaal']. ".jpg'>";
echo "</div></div><div class='col-sm-6'>";
echo "<div class='h2-container'><h2>" . $row["Product"]. "</h2></div>" . $row["Samenvatting"]. "" . $_SESSION["product_id"] . "";
echo "</div><div class='col-sm-3'><div class='col-sm-12 text-right'>
<div class='border'>
<p style='margin-bottom:20px;width:30px;'><i class='fa fa-heart' aria-hidden='true'></i></p>
</div>
</div>";
echo "<table>
<tr>
<td><i class='fa fa-users' aria-hidden='true'></i></td>
<td><p>vanaf 10 personen</p></td>
</tr>
<tr>
<td><i class='fa fa-clock-o' aria-hidden='true'></i></td>
<td>" . $row["Tijdsduur"] . " uur</td>
</tr>
<tr>
<td><i class='fa fa-euro' aria-hidden='true'></i></td>
<td>vanaf " . $row["VerkoopPP40"] ." p.p. <small>excl btw</small></td>
</tr>
</table></div></div></a>";
}
} else {
echo "0 results";
}?>
As you can see I'm trying to save the $row['Id']
. First I make this ID in a $identi variable so that I can echo it. Then I save the ID in a $_session["product_id"]
and echo it later on in the HTML. This works. But when I click on the link to go to the next page it looks like the session isn't saved.
I cannot echo the product_id
on the next page using:
<?php
if(isset($_SESSION['product_id'])){
echo $_SESSION['product_id'];
};
?>
I think it's not being stored, or (probably) I'm doing this all wrong? I found something on stack-overflow that started the session inside another part of the database query:
<?php
$sql = "SELECT * FROM assortiment WHERE Subcategorie = '$product_id'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$fotos = $row["Fotomateriaal"];
$productnaam = $row["Product"];
$contenttext = $row["WebTekst"];
$tijdsduur = $row["Tijdsduur"];
$prijs = $row["VerkoopPP40"];
$sub = $row["Subcategorie"];
$cate = $row["Categorie"];
$seo = $row["SEO_Pagetitle"];
$youtube = $row["Actief_Avontuur"];
$_SESSION["product_id"] = $row["Id"];
}
} else {
$value = "Geen resultaten";
} ?>
But this give me nothing also. Does anyone know why this isn't working. Normally everything works fine if I want to store a variable in a session like normal (just using PHP and Wordpress) but this just wont work. Any help would be much appreciated.
解决方案
Actually you write session_start();
in wrong way( SESSION_START();
) and wrong place (Inside while()
).
On each php page add session_start();
on top of each page just after starting <?php
Note:- session_start();
needed to fetch+manipulate SESSION
data
这篇关于在会话中保存数据库行 ID 以备后用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文