简体   繁体   English

出现错误 SQLSTATE[HY000]:一般错误:尝试执行 if 语句时出现 2031

[英]Got error SQLSTATE[HY000]: General error: 2031 when trying to execute if statement

I'm not very experienced with the way sql works really but I am trying to check if the database contains a value and if so then it finds all the values to id and if not it only binds one particular value to the idea.我对 sql 的实际工作方式不是很有经验,但我正在尝试检查数据库是否包含一个值,如果是,那么它会找到 id 的所有值,如果不是,它只会将一个特定值绑定到这个想法。 I get the error in my error log for the line <?php foreach ($edited as $key => $value): ?>我的错误日志中出现<?php foreach ($edited as $key => $value): ?>

I haven't tried much to solve it because I have hit a roadblock, but I saw something talking about a binding limit and I'm not sure if I am over that limit with my first query or if I'm just overcomplicating it and there's actually a shorter way to do what I'm trying.我没有尝试太多来解决它,因为我遇到了障碍,但是我看到了一些关于绑定限制的内容,我不确定我的第一个查询是否超过了该限制,或者我是否只是过于复杂了它和实际上有一种更短的方法来做我正在尝试的事情。

//In user.php
public function isChangeRequested($requested){
        if ($requested != "Requested"){ 
            return true;
        }
        return false;
    }
///

<?php
if (isset($_GET['id'])) {
  try {
    $id = $_GET['id'];
    $requestedstatus = "SELECT  change_request FROM timesheet WHERE id = :id";
    $results = $db->query($requestedstatus);
    if($user->isChangeRequested($results)){
      $isrequested = "SELECT * FROM timesheet WHERE id = :id";
      $statement = $db->prepare($isrequested);
      $statement->bindValue(':id', $id);
      $statement->execute();
      $edited = $statement->fetch(PDO::FETCH_ASSOC);
    } 
    else {
      $notrequested = "SELECT approve_status FROM timesheet WHERE id = :id";
      $statement = $db->prepare($notrequested);//submit_day, time_in, time_out, approve_status
      $statement->bindValue(':id', $id);
      $statement->execute();
      $edited = $statement->fetch(PDO::FETCH_ASSOC);
    }

  } catch(PDOException $error) {
      echo $sql . "<br>" . $error->getMessage();
  }
} else {
    echo "Something went wrong!";
    exit;
}
?>

<form method="post" name="editform">
<?php foreach ($edited as $key => $value) : ?>
<label style="padding-top: 10px;" for="<?php echo $key; ?>"><?php echo ucwords(str_replace("_"," ", $key)); ?></label>
<input style="padding-top: : 10px;"  type="text" class="form-control" name="<?php echo $key; ?>" id="<?php echo $key; ?>" value="<?php echo $value; ?>" <?php echo ($key === 'id' ? 'readonly' : null);?> <?php echo ($key === 'name' ? 'readonly' : null);?> <?php echo ($key === 'submit_status' ? 'readonly' : null);?> <?php echo ($key === 'user_id' ? 'readonly' : null);?>>
<?php endforeach; ?>
<input style="margin-top:20px;" class="btn btn-primary" type="submit" name="submit" value="Update Entry">
</form>

My expected results are if the database contains Requested than the supervisor is able to see and edit all fields, but if it is not requested then the supervisor can only see and edit the Approved field.我的预期结果是,如果数据库包含 Requested,主管可以查看和编辑所有字段,但如果没有请求,那么主管只能查看和编辑 Approved 字段。

if (isset($_GET['id'])) {
  try {
    $id = $_GET['id'];
    $requestedstatus = "SELECT  change_request FROM timesheet WHERE id = :id";
    $results = $db->prepare($requestedstatus);
    $results->bindValue(':id', $id);
    $results->execute();
    $requestedresult = $results->fetch(PDO::FETCH_ASSOC);
    foreach ($requestedresult as $thatshitsrequested)
    echo $thatshitsrequested;
    if(!$user->isChangeRequested($thatshitsrequested)){
      $isrequested = "SELECT * FROM timesheet WHERE id = :id";
      $statement = $db->prepare($isrequested);
      $statement->bindValue(':id', $id);
      $statement->execute();
      $edited = $statement->fetch(PDO::FETCH_ASSOC);
    } 
    else {
      $notrequested = "SELECT approve_status FROM timesheet WHERE id = :id";
      $statement = $db->prepare($notrequested);//submit_day, time_in, time_out, approve_status
      $statement->bindValue(':id', $id);
      $statement->execute();
      $edited = $statement->fetch(PDO::FETCH_ASSOC);
    }

  } catch(PDOException $error) {
      echo $sql . "<br>" . $error->getMessage();
  }
} else {
    echo "Something went wrong!";
    exit;
}

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

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