1

尝试执行该过程时出现错误:“字段列表”中的“未知列”“数组”。我还是 php 新手,所以我想知道我的编码有什么问题。

这是首先编码。

<?php
include("dbconnect.php");


$q = mysql_query("SELECT bachok_qtt FROM bachok WHERE bachok_type = 'pants' ");
if(is_resource($q) and mysql_num_rows($q)>0)
{
    $r = mysql_fetch_array($q);
    $r["bachok_qtt"];
}

$w = mysql_query("SELECT bachok_qtt FROM bachok WHERE bachok_type = 'clothes' ");
if(is_resource($w) and mysql_num_rows($w)>0)
{
    $s = mysql_fetch_array($w);
    $s["bachok_qtt"];
}       

$e = mysql_query("SELECT stock_qtt FROM stock WHERE stock_type = 'pants' ");
if(is_resource($e) and mysql_num_rows($e)>0)
{
    $a = mysql_fetch_array($e);
    $a["stock_qtt"];
}    

$t = mysql_query("SELECT stock_qtt FROM stock WHERE stock_type = 'clothes' ");
if(is_resource($t) and mysql_num_rows($t)>0)
{
    $b = mysql_fetch_array($t);
    $b["stock_qtt"];
}    


if ($r < $a)
{
    if($s < $b)
    {
        $sql1 = "UPDATE stock SET stock_qtt = stock_qtt - $r  WHERE stock_type = 'pants' ";
        $sql2 = "UPDATE stock SET stock_qtt = stock_qtt - $s  WHERE stock_type = 'clothes' ";

        $result1 = mysql_query($sql1) or die ("Error: " . mysql_error());
        $result2 = mysql_query($sql2) or die ("Error: " . mysql_error());

        if($result1)
        {
            if($result2)
            {
            echo "Successful";
            echo "<br>";
            echo "<a href='admin2.php'>Back to main page</a>";
            }
        }
        else 
        {
            echo "ERROR";
        }
    }
}
else
{
    echo "Not enough stock";
    echo "<br>";
    echo "<a href='admin2.php'>Back to main page</a>";
}




?> 

那么,为什么它在执行时会抛出未知的列数组。我是否正确使用了 MySQL_fetch_array()?

4

4 回答 4

3

问题来了。。

$sql1 = "UPDATE stock SET stock_qtt = stock_qtt - $r  WHERE stock_type = 'pants' ";
$sql2 = "UPDATE stock SET stock_qtt = stock_qtt - $s  WHERE stock_type = 'clothes' ";

$r和都是$s数组。

我想你正在尝试这样做..

$r = mysql_fetch_array($q);
$r = $r["bachok_qtt"];
于 2013-04-23T09:41:39.707 回答
1

您的问题是您定义$r为一个数组,然后尝试在字符串中使用它。查看您的代码,我实际上怀疑您希望将值分配给$r["bachok_qtt"]变量,然后在查询中使用它。

例如下面的代码不做任何事情:

$r["bachok_qtt"];

您应该按如下方式更新您的代码:

$q = mysql_query("SELECT bachok_qtt FROM bachok WHERE bachok_type = 'pants' ");
if(is_resource($q) and mysql_num_rows($q)>0)
{
    $r = mysql_fetch_array($q);
    $bachok_qtt = $r["bachok_qtt"];
}

$w = mysql_query("SELECT bachok_qtt FROM bachok WHERE bachok_type = 'clothes' ");
if(is_resource($w) and mysql_num_rows($w)>0)
{
    $s = mysql_fetch_array($w);
    $bachok2 = $s["bachok_qtt"];
}    

然后你的SQL如下:

$sql1 = "UPDATE stock SET stock_qtt = stock_qtt - $bachok_qtt  WHERE stock_type = 'pants' ";
$sql2 = "UPDATE stock SET stock_qtt = stock_qtt - $bachok2  WHERE stock_type = 'clothes' ";

还应该通知您,该mysql_*函数系列现在正在弃用,并且很快就会失效。与其在新代码中使用它们,不如研究MySQLiPDO的使用。它们不仅带来了长寿的好处,而且还消除了使用mysql_*.

于 2013-04-23T09:44:25.087 回答
0

给你,我修正了你的错误。尝试这个

 <?php
 include("dbconnect.php");


 $q = mysql_query("SELECT bachok_qtt FROM bachok WHERE bachok_type = 'pants' ");
 if(is_resource($q) and mysql_num_rows($q)>0)
 {
  $r = mysql_fetch_array($q);

  }

 $w = mysql_query("SELECT bachok_qtt FROM bachok WHERE bachok_type = 'clothes' ");
 if(is_resource($w) and mysql_num_rows($w)>0)
 {
  $s = mysql_fetch_array($w);

 }       

 $e = mysql_query("SELECT stock_qtt FROM stock WHERE stock_type = 'pants' ");
 if(is_resource($e) and mysql_num_rows($e)>0)
 {
  $a = mysql_fetch_array($e);

 }    

 $t = mysql_query("SELECT stock_qtt FROM stock WHERE stock_type = 'clothes' ");
 if(is_resource($t) and mysql_num_rows($t)>0)
 {
 $b = mysql_fetch_array($t);

 }    


 if ($r["bachok_qtt"] < $a["stock_qtt"])
 {
 if($s["bachok_qtt"] < $b["stock_qtt"])
 {
    $sql1 = "UPDATE stock SET stock_qtt = stock_qtt - '".$r["bachok_qtt"]."'  WHERE stock_type = 'pants' ";
    $sql2 = "UPDATE stock SET stock_qtt = stock_qtt - '".$s["bachok_qtt"]."'  WHERE stock_type = 'clothes' ";

    $result1 = mysql_query($sql1) or die ("Error: " . mysql_error());
    $result2 = mysql_query($sql2) or die ("Error: " . mysql_error());

    if($result1)
    {
        if($result2)
        {
        echo "Successful";
        echo "<br>";
        echo "<a href='admin2.php'>Back to main page</a>";
        }
    }
    else 
    {
        echo "ERROR";
    }
 }
 }
 else
 {
 echo "Not enough stock";
 echo "<br>";
 echo "<a href='admin2.php'>Back to main page</a>";
 }

  ?> 
于 2013-04-23T09:57:42.800 回答
0

我想你可以看看$r["bachok_qtt"]; 也许你想说的话$r = $r["bachok_qtt"];

于 2013-04-23T09:53:24.750 回答