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

时间:2019-09-19 23:36:25

标签: php mysql sql

我对sql的工作方式并不十分了解,但是我正在尝试检查数据库是否包含一个值,如果是,那么它将查找所有值到id,如果不是,则它仅将一个特定值绑定到该想法。我在错误日志中收到了<?php foreach ($edited as $key => $value) : ?>

行的错误

由于遇到障碍,我没有做太多的努力来解决它,但是我看到了一些关于绑定限制的信息,我不确定我的第一个查询是否超过了该限制,或者我只是过于复杂了,实际上有一种做我尝试的方法更短。

//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>

我的预期结果是,如果数据库包含“已请求”,则主管可以查看和编辑所有字段,但是如果未要求,则主管只能查看和编辑“批准”字段。

1 个答案:

答案 0 :(得分:0)

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;
}