使用OOP PHP更改枚举

时间:2017-03-14 00:19:02

标签: php mysql enums

我是用PHP学习Web开发的新手,我有一个问题,我试图解决。我的数据库中有一个ENUM类型,它有2个值:" Y"或" N"。它在我的用户表(tbl_users)中称为userStatus。我点击按钮时尝试使用PHP更改该特定用户的ENUM值。然而,当我点击按钮时没有任何反应,我不确定它的按钮或我的PHP或两者的组合是否错误导致这不起作用?

PHP更改ENUM:

if (ytMatch && ytMatch[1].length === 11) {
  var youtubeId = ytMatch[1];
  $video = $('<iframe>')
    .attr('frameborder', 0)
    .attr('src', '//www.youtube.com/embed/' + youtubeId)
    .attr('width', '100%').attr('height', '360');

获取用户ID功能:

if(isset($_POST['btn-activate'])){

  if(isset($_GET['id']))
  {
   $id = $_GET['id'];
   extract($user_home->getID($userId));

   $statusY = "Y";
   $statusN = "N";

$stmt = $user->runQuery("SELECT userID,userStatus FROM tbl_users WHERE userID=:uID");
$stmt->execute(array(":userID"=>$userId));
$row=$stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->rowCount() > 0)
{
if($row['userStatus']==$statusN)
{
$stmt = $user->runQuery("UPDATE tbl_users SET userStatus=:status WHERE userId=:userID");
$stmt->bindparam(":status",$statusY);
$stmt->bindparam(":userID",$userId);
$stmt->execute();

$msg = "
          <div class='alert alert-success'>
    <button class='close' data-dismiss='alert'>&times;</button>
    <strong>WoW !</strong>  Your Account is Now Activated : <a href='manage_users.php'></a>
       </div>
       ";
}
else
{
$msg = "
          <div class='alert alert-error'>
    <button class='close' data-dismiss='alert'>&times;</button>
    <strong>sorry !</strong>  Your Account is allready Activated : <a href='manage_users.php'></a>
       </div>
       ";
}
}
else
{
$msg = "
      <div class='alert alert-error'>
   <button class='close' data-dismiss='alert'>&times;</button>
   <strong>sorry !</strong>  No Account Found : <a href='manage_users.php'></a>
   </div>
   ";
}
}
}

表显示用户:

 public function getID($userId)
 {
  $stmt = $this->db->prepare("SELECT * FROM tbl_users WHERE userId=:id");
  $stmt->execute(array(":id"=>$userId));
  $editRow=$stmt->fetch(PDO::FETCH_ASSOC);
  return $editRow;
 }

表格末尾的我的按钮,当点击时,应该在以下位置运行PHP:

  $database = new Database();
  $db = $database->dbConnection();
  $conn = $db;


          $query = "SELECT * FROM tbl_users";
          $stmt = $conn->prepare($query);
          $stmt->execute();
          while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
          ?>
          <tr>

            <td><?php echo $row['userID']?></td>
            <td><?php echo $row['userName']?></td>
            <td><?php echo $row['userFirstName']."&nbsp;".$row['userSurname']; ?></td>
            <td><?php echo $row['userEmail']?></td>
            <td><?php echo $row['userRole']?></td>
            <td><?php echo $row['userStatus']?></td>

          <td>

提前致谢。

2 个答案:

答案 0 :(得分:0)

name属性放在按钮上,而不是放在<i>元素中。

<?php

    if($row['userStatus'] == ('N')){

        echo '  <button type="submit" class="btn btn-info" name="btn-activate"><i class="glyphicon glyphicon-ok"></i> Activate</button>';

    } else {

        echo '  <button class="btn btn-default"><i class="glyphicon glyphicon-eye-close"></i> Archive</button>';

    }

?>

答案 1 :(得分:0)

如果前两个if条件中的任何一个未评估为TRUE,则没有代码(没有任何反应)。

考虑添加一些调试输出。至少为每个ifs和else一些调试输出添加echo

非常奇怪,代码正在检查$_POST$_GET。我怀疑id正在表单提交中传递,就像btn-activate一样,而不是作为uri中的参数。 (即你的意思是$_POST['id']吗?只是在这里猜测。

我们在此行中看到对$userid的引用:

extract($user_home->getID($userId));

但是我们看不到为$userId分配了什么值。上一行尝试设置名为$id的变量。但是我们没有看到$id被其他地方使用。

就个人而言,我不会在没有特别需要的地方使用extract功能。 (我不希望我的代码容易出错,在本例中,当有人向tbl_users添加一列时。)

http://ericlippert.com/2014/03/05/how-to-debug-small-programs/