简体   繁体   中英

What is a General error and when and why do we get it? {“error”:{“text”:SQLSTATE[HY000]: General error}}

I am trying to update my Users table after I insert data into.It worked fine when I was just returning all the data but now it is giving me this error msg i get when I run it in Postman.

171{"error":{"text":SQLSTATE[HY000]: General error}}

This is the code I am trying to execute using the slim frame work.

function add_User()
 {
           $response = array();
           $request = \Slim\Slim::getInstance()->request();

                  $name = $request->post('name');
                   $emailId = $request->post('emailId');
                   $mobileNumber = $request->post('mobileNumber');
                   $password = $request->post('password');
                   $otp = $request->post('otp');
                   $user_status = $request->post('user_status');
                   $referral_code = $request->post('referral_code');
                   $referred_by = $request->post('referred_by');

                  $addressId=NULL;

  $sql = "INSERT IGNORE INTO users(userId,name,emailId,mobileNumber,password,otp,user_status,referral_code,referred_by)VALUES
  (NULL,:name,:emailId,:mobileNumber,:password,:otp,:user_status,:referral_code,:referred_by);";
    try 
    {
        $db = getConnection();
        $stmt = $db->prepare($sql);
        $stmt->bindParam("name",  $name);
        $stmt->bindParam("emailId", $emailId);
        $stmt->bindParam("mobileNumber", $mobileNumber);
        $stmt->bindParam("password", $password);
        $stmt->bindParam("otp", $otp);
        $stmt->bindParam("user_status", $user_status);
        $stmt->bindParam("referral_code", $referral_code);
        $stmt->bindParam("referred_by", $referred_by);

            $stmt->execute();
           if ($stmt->rowCount())
        {
          $lastId = $db->lastInsertId();
        echo $lastId;
         $refCode= $referral_code.$lastId;

          $sql ="Update Users SET referral_code = :referral_code, referred_by = :referred_by WHERE  userId = :userId";
    try 
    {
        $db = getConnection();
        $stmt = $db->prepare($sql);
        $stmt->bindParam("userId", $lastId);
        $stmt->bindParam("referral_code", $refCode);
        $stmt->bindParam("referred_by", $referred_by);

            $stmt->execute();
            $res = $stmt->fetchAll(PDO::FETCH_OBJ);
            $row=count($res);
           $db = null;
          if ($row > 0)
        {
         $response["error"] = "SignUp SuccessFully";
          $response['userId'] = $res;
          echoResponse(201, $response);
        } else 
      {   $response['error'] = true;
           $response['message'] = "User Not Available";
           echoResponse(200, $response);
      }
    } catch(PDOException $e) 
    {

        echo '{"error":{"text":'. $e->getMessage() .'}}'; 
    }

       } else 
      {
           $response['error'] = true;
           $response['message'] = "Cant create  User";
           echoResponse(200, $response);
      }
    } catch(PDOException $e) 
    {

        echo '{"error":{"text":'. $e->getMessage() .'}}'; 
    }
 }

Any help or support is appreciated.Thank you.

Answer from: PDO error: " SQLSTATE[HY000]: General error " When updating database

You do not use fetchAll() as in

$res = $stmt->fetchAll(PDO::FETCH_OBJ);

with update or insert queries. Removing this statement should rectify the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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