简体   繁体   English

更新表格错误? (SQLSTATE [HY000]:一般错误)

[英]Updating table error? (SQLSTATE[HY000]: General error)

I'm currently getting this error 我目前收到此错误

[31-Aug-2018 15:50:46 America/New_York] PHP Fatal error:  Uncaught 

exception 'PDOException' with message 'SQLSTATE[HY000]: General error' 
in /home/crysuicn/public_html/products.php:22
Stack trace:
#0 /home/crysuicn/public_html/products.php(22): PDOStatement->fetch(2)
#1 {main}
  thrown in /home/crysuicn/public_html/products.php on line 22

Line 22 is: 第22行是:

while($row = $query->fetch(PDO::FETCH_ASSOC)){

Therefore the error would be somewhere in here, but I do not see what the error could be. 因此,错误可能在此处,但我看不出错误可能是什么。

if(isset($_SESSION["discount"])){
        if($_SESSION["discount"] != "na"){
            $sql = "SELECT users FROM discounts WHERE name=:promo";
            $query = $st->prepare($sql);
            $query->execute(array(
                'promo' => $name
            ));
            if($query->rowCount()>0){
                while($row = $query->fetch(PDO::FETCH_ASSOC)){
                    if(!empty($row["users"])){
                        $usedips = $row["users"];
                        $usedips = $usedips.", ".$_SERVER["REMOTE_ADDR"];
                    }else{
                        $usedips = $_SERVER["REMOTE_ADDR"];
                    }
                    $sql = "UPDATE discounts SET users=:updatedips WHERE name=:name";
                    $query = $st->prepare($sql);
                    $query->execute(array(
                        'updatedips' => $usedips,
                        'name' => $name
                    ));
                }
            }
        }
    }
    $_SESSION["discount"] = "na";

Any help is much appreciated. 任何帮助深表感谢。 I haven't had this error before so I am not sure where to go from here. 我之前没有遇到此错误,所以我不确定从这里去哪里。 It does go through and updates the table 它确实通过并更新了表

You are destroying your $query handle by reusing it on the UPDATE query thus the while loop gets broken. 您通过在UPDATE查询上重用$query句柄来破坏它,从而使while循环中断。

So use another variable for the statement handle used in the UPDATE. 因此,请对UPDATE中使用的语句句柄使用另一个变量。

Its also better to prepare a query once and reuse it many times, just replacing the variables. 最好只准备一次查询并重复使用多次,只需替换变量即可。 This allows the database to compile and optimise the query only once, but run it many times. 这使数据库只能一次编译和优化查询,但可以多次运行。 It's quicker for your script and places less unnecessary load on the database. 它可以更快地执行脚本,并减少不必要的数据库负载。

if(isset($_SESSION["discount"])){
    if($_SESSION["discount"] != "na"){
        $sql = "SELECT users FROM discounts WHERE name=:promo";
        $query = $st->prepare($sql);
        $query->execute(array('promo' => $name));

        if($query->rowCount()>0){

            // prepare once use many times
            $sql = "UPDATE discounts 
                    SET users=:updatedips WHERE name=:name";   
            $stmt= $st->prepare($sql);

            while($row = $query->fetch(PDO::FETCH_ASSOC)){
                if(!empty($row["users"])){
                    $usedips = $row["users"];
                    $usedips = $usedips.", ".$_SERVER["REMOTE_ADDR"];
                }else{
                    $usedips = $_SERVER["REMOTE_ADDR"];
                }

                $stmt->execute(array('updatedips' => $usedips,'name' => $name));
            }
        }
    }
}
$_SESSION["discount"] = "na";

You're recycling variable names and sabotaging your loop. 您正在回收变量名并破坏循环。 Rename the inner "query", or while you're at it, structure it differently: 重命名内部“查询”,或者在使用它时以不同的方式进行结构化:

        $select = $st->prepare("SELECT users FROM discounts WHERE name=:promo");
        $select->execute(array(
            'promo' => $name
        ));

        $update = $st->prepare("UPDATE discounts SET users=:updatedips WHERE name=:name");

        if ($select->rowCount() > 0) {
            while ($row = $select->fetch(PDO::FETCH_ASSOC)) {
                if(!empty($row["users"])){
                  $usedips = $row["users"];
                  $usedips = $usedips.", ".$_SERVER["REMOTE_ADDR"];
                } else {
                  $usedips = $_SERVER["REMOTE_ADDR"];
                }

                $update->execute(array(
                  'updatedips' => $usedips,
                  'name' => $name
                ));
            }
        }

Whenever possible give your variables meaningful, contextual names. 只要有可能,请为变量提供有意义的上下文名称。 $query is often too vague. $query通常太含糊。

Plus use prepared statements effectively: Prepare once and execute many times if necessary. 加上有效使用准备好的语句:准备一次,并在必要时执行多次。

It also looks like you're assembling a comma-separated value in a column which is a big problem for relational databases. 看起来您正在用逗号分隔值的列中,这对于关系数据库是一个大问题。 Instead create a proper one-to-many relationship here. 而是在此处创建适当的一对多关系。 Try to avoid violating the Zero, One or Infinity Rule by understanding the idea behind proper database normalization . 通过理解适当的数据库规范化背后的想法尝试避免违反零,一或无穷规则

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM