Shopping Cart $_GET问题无法与ID一起正常工作。

huangapple go评论58阅读模式
英文:

Shopping Cart $_GET problem can't work with the id

问题

我有一个关于 $_GET 的问题。

在我的 index.php 文件上,我有一个小型购物网站,可以通过点击“购买”按钮购买各种随机物品。点击“购买”按钮后,我尝试获取所点击产品的 id,如下所示:

//连接、SQL等操作...

$response = "";

while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
    //其他操作...
    $response .= "<a href='addtocart.php?id=" . $row['ProduktID'] . "'><button class='inCart'>Buy</button></a>";
}

然后,我有一个名为 addtocart.php 的新文件。我可以看到这些 id:

图片1

图片2

addtocart.php:

<?php
session_start();

if (isset($_GET['ProduktID']) && !empty($_GET['ProduktID'])) {
    //进行相关操作
} else {
    echo "错误,无法添加该物品";
}

我始终收到错误消息。

英文:

I have a problem with $_GET.

On my index.php file I have a little shopping site where I can buy random stuff by clicking the "buy" button. After clicking the "buy" button I am trying to get the id of the clicked product like this:

//Stuff like connection, sql, etc..

$response=&quot;&quot;;

    while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
        //Other Stuff...
        $response .= &quot;&lt;a href=&#39;addtocart.php?id=&quot;. $row[&#39;ProduktID&#39;] . &quot;&#39;&gt;&lt;button class=&#39;inCart&#39;&gt;Buy&lt;/button&gt;&lt;/a&gt;&quot;;
    }

And then I have a new file called addtocart.php. I can see the id's:

Image

Image

addtocart.php:

&lt;?php
  session_start();

  if(isset($_GET[&#39;ProduktID&#39;]) &amp;&amp; !empty($_GET[&#39;ProduktID&#39;])){
     //Do Stuff
  }
  else{
     echo &quot;Error, can&#39;t add the item&quot;;
  }

I am always getting the Error message..

答案1

得分: 1

在这里,addtocart.php?id 中的 id 已经被使用,因此在URL中它将作为参数传递 id

$response .= "<a href='addtocart.php?id=" . $row['ProduktID'] . "'><button class='inCart'>购买</button></a>";

addtocart.php:

所以在PHP中,您应该这样访问 $_GET['id']

<?php
  session_start();

  if(isset($_GET['id']) && !empty($_GET['id'])){
     //执行相关操作
  }
  else{
     echo "错误,无法添加物品";
  }
?>
英文:

Here addtocart.php?id you have used id, so in URL it will passed param as id

     $response .= &quot;&lt;a href=&#39;addtocart.php?id=&quot;. $row[&#39;ProduktID&#39;] . &quot;&#39;&gt;&lt;button class=&#39;inCart&#39;&gt;Buy&lt;/button&gt;&lt;/a&gt;&quot;;

addtocart.php:

so in php you should access as $_GET[&#39;id&#39;]

&lt;?php
  session_start();

  if(isset($_GET[&#39;id&#39;]) &amp;&amp; !empty($_GET[&#39;id&#39;])){
     //Do Stuff
  }
  else{
     echo &quot;Error, can&#39;t add the item&quot;;
  }

huangapple
  • 本文由 发表于 2020年1月3日 17:46:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576275.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定