使用 unset() 从购物车中删除项目会导致 mysql 错误

发布于 2024-12-14 09:53:28 字数 1467 浏览 1 评论 0原文

大家好,这是我在 stack Overflow.com 上的第一篇文章 我正在尝试创建购物车。值存储在二维表中的会话中。第一个值代表数据库中的商品 ID,第二个值代表商品的数量。

foreach($cart_array as $row){
    $sql = "SELECT * FROM prod_table WHERE id=".$row['0']."";
    $cart_result = mysql_query($sql, $conn);
    while ($array = mysql_fetch_array($cart_result)) {
        echo "<tr><td>". $array[manufacturer] . "</td>";
        echo "<td>". $array[model]."</td>";
        echo "<td>".$row['1']."</td>";
        $cart_value = $array[price]*$row['1'];
        //sum and store value of all products
        $total_cart_value += $cart_value;
        echo "<td>&pound;" .$cart_value. "</td>";
        echo "<td><a href='search.php?kick_id=".$row['0']."'>Remove</a></td></tr>";

好的,要删除购物车中的项目,用户单击“删除”,然后他将被发送回同一页面(由于某种原因,我很难将 $_SERVER['PHP_SELF'} 与 GET 方法一起使用...) 然后触发代码的不同部分,这将使用 unset 函数从 $cart_array 中删除一对值。

if(isset($_GET['kick_id'])) {
    $t = count($cart_array);
    for( $u = 0; $u < $t; $u++){
        if ($cart_array[$u]['0'] == $_GET['kick_id']){
            unset($cart_array[$u]['0']);
            unset($cart_array[$u]['1']);
            echo " Item has been removed from your cart";
            $cart_array = array_values($cart_array); 

它实际上删除了一对值,但是现在它不再顺利运行,而是显示错误消息: 警告:mysql_fetch_array():提供的参数不是有效的 MySQL 结果资源... 我假设会话数组中一定有一些已删除值的“痕迹”,这导致了错误。 如何修复代码? 还有其他方法/途径来删除值吗?

Hello everyone this is my first post on stack overflow.com
I am trying to create shopping cart. Values are being stored in a session in two dimension table. First value represents item id in database, second - quantity of item.

foreach($cart_array as $row){
    $sql = "SELECT * FROM prod_table WHERE id=".$row['0']."";
    $cart_result = mysql_query($sql, $conn);
    while ($array = mysql_fetch_array($cart_result)) {
        echo "<tr><td>". $array[manufacturer] . "</td>";
        echo "<td>". $array[model]."</td>";
        echo "<td>".$row['1']."</td>";
        $cart_value = $array[price]*$row['1'];
        //sum and store value of all products
        $total_cart_value += $cart_value;
        echo "<td>£" .$cart_value. "</td>";
        echo "<td><a href='search.php?kick_id=".$row['0']."'>Remove</a></td></tr>";

OK, to remove item form cart user clicks remove and then he is being send back to the same page ( for some reason I had difficulty to use $_SERVER['PHP_SELF'} with GET method... )
and then different part of code is being triggered which is going to remove pair of values from $cart_array, using unset function.

if(isset($_GET['kick_id'])) {
    $t = count($cart_array);
    for( $u = 0; $u < $t; $u++){
        if ($cart_array[$u]['0'] == $_GET['kick_id']){
            unset($cart_array[$u]['0']);
            unset($cart_array[$u]['1']);
            echo " Item has been removed from your cart";
            $cart_array = array_values($cart_array); 

And it actually removes pair of values but, now instead of running smoothly it displays error message :
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource ...
I assume that there must be some "trace" of deleted values in session array which is causing error.
How to fix code ?
Is there any other method/approach to delete values ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风月客 2024-12-21 09:53:28

您似乎没有 unset($cart_array[$u]),只有它包含的两个键。不必费心取消设置 $cart_array[$u]['0']'1',只需取消设置 $cart_array[$u]

You don't appear to have unset($cart_array[$u]), just the two keys it contains. Don't bother unsetting $cart_array[$u]['0'] and '1', just unset $cart_array[$u].

囍孤女 2024-12-21 09:53:28

我认为...

这就是你做错的事情。
程序员永远不应该“假设”。程序员可以(并且应该)确定
假设会让你误入歧途并浪费你的时间(以及当你提出问题时其他人的时间)。

因此,您必须进行一些调试 - 进行调查以找到问题的实际原因
提供的参数不是有效的 MySQL 结果资源错误表示查询中存在错误。所以,你必须看看它是什么错误。因此,以可让您看到发生的任何错误的方式运行您的所有查询。最简单的方法是

$cart_result = mysql_query($sql, $conn) or trigger_error(mysql_error()." in ".$sql);

再次运行您的代码。
您会在通常的位置(屏幕或日志文件)中发现 sql 错误。

如果您无法理解错误的含义并自行更正代码 - 是时候提出问题了。一个明确而直接的问题,而不是出于某些假设的模糊问题。明白了吗?

I assume that...

that is what you are doing wrong.
A programmer should never "assume". A programmer can (and should) be certain.
Assumptions will lead you astray and waste your time (as well as other people's time when you ask a question).

So, you have to do some debugging - an investigation to find the actual cause of the problem.
supplied argument is not a valid MySQL result resource error means there was an error in the query. So, you have to see what error it was. So, run all yoiur queries in the manner which let you to see any error occurred. A simplest way would be

$cart_result = mysql_query($sql, $conn) or trigger_error(mysql_error()." in ".$sql);

and run your code again.
you will find an sql error in their usual place - a screen or a log file.

If you'll be unable to get the meaning of the error and correct your code yourself - it's time to ask a question. A certain and direct question, not vague question out of some assumptions. Gotcha?

情愿 2024-12-21 09:53:28

有两种方法可以解决此问题:

1)在第一部分:

foreach($cart_array as $row){
  if(empty($row[0])
    continue;
  $sql = "SELECT * FROM prod_table WHERE id=".$row['0']."";
  [...]

2)当您删除该项目时,而不是 unset($cart_array[$u]['0']);你为什么不取消设置($cart_array[$u]); ?

There are two ways to solve this:

1) on your first part:

foreach($cart_array as $row){
  if(empty($row[0])
    continue;
  $sql = "SELECT * FROM prod_table WHERE id=".$row['0']."";
  [...]

2) when you remove the item, instead of unset($cart_array[$u]['0']); why don't you do unset($cart_array[$u]); ?

五里雾 2024-12-21 09:53:28

可能这

while ($array = mysql_fetch_array($cart_result)) {

就是导致问题的原因。基于您看到的错误。在尝试获取结果之前检查是否有任何可用结果

if(mysql_num_rows($cart_result)>0){
  while ($array = mysql_fetch_array($cart_result)) {
//rest code here
}

Probably this

while ($array = mysql_fetch_array($cart_result)) {

is causing the problem. based on error you see. before you try fetch result check if there is any results available by

if(mysql_num_rows($cart_result)>0){
  while ($array = mysql_fetch_array($cart_result)) {
//rest code here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文