表单中的值未在表单提交时更新

时间:2017-01-03 15:42:51

标签: php forms class updates

我目前正在为PHP的第一年学校项目编写CMS,我正在编写更新函数,当更新发生时,更新应该更改表单中的数据,因为我写了一个名为'updateValues的函数( )''会更新类的值,并以这种方式回显新数据,但它不会这样做?提交完成后我仍然需要重新刷新页面,我不知道为什么。

<?php

/*

  Author: Lucas Ouwens
  Een class voor het aanmaken, bewerken en het verwijderen van shop items.
  Dit is nog niet af, er moeten dingen veranderd worden omdat het gekopieerd is    van partner.php

*/

class shopItem {

private $id;
private $itemName;
private $desc;
private $image;
private $full_image;
private $cost;

function __construct($id, $itemName, $desc, $image, $full_image, $cost) {
  $this->id = $id;
  $this->itemName = $itemName;
  $this->desc = $desc;
  $this->image = $image;
  $this->full_image = $full_image;
  $this->cost = $cost;

}


public static function loadFromDB() {
  $db = databaseHandler::getInstance()->getConnection();

  $stmnt = $db->prepare("SELECT * FROM " . SHOPITEMS);
    $succes = $stmnt->execute();

    if($succes) {
      return $stmnt->fetchAll();
    }
}

public function delete() {
  $db = databaseHandler::getInstance()->getConnection();

  $stmnt = $db->prepare("SELECT * FROM " . SHOPITEMS . " WHERE id = :id");
    $stmnt->bindParam(":id", $this->id);
    $succes = $stmnt->execute();


    echo $stmnt->rowCount();
    if($succes) {
      if(!empty($stmnt->fetchAll())) {
      $stmnt = $db->prepare("DELETE FROM " . SHOPITEMS . " WHERE id = :id");
        $stmnt->bindParam(":id", $this->id);
        $succes = $stmnt->execute();
        if($succes) {
          echo "<p>Sucessfully removed the shop item from the database</p>";
          header("refresh:0;");
        }
      } else {
        echo '<p>Your request has already been processed</p>';
      }
    }
}

public static function getById($id) {
  $db = databaseHandler::getInstance()->getConnection();

  $stmnt = $db->prepare("SELECT * FROM " . SHOPITEMS . " WHERE id = :id");
      $stmnt->bindParam(":id", $id);
      $succes = $stmnt->execute();

      if($succes) {
        $data =  $stmnt->fetchAll();

        return new shopItem($id, $data[0]["itemname"], $data[0]["description"], $data[0]["image"], $data[0]["full_image"], $data[0]["cost"]);
      }

      return null;
}

/*
Het moment wanneer je er achter komt dat in PHP de ternary operators erg irritant zijn, zijn ifs je held.
                                      (Java masterrace)

*/
public function update($text, $image, $cost) {
  $id = $this->id;
    $textDiff = false;
    $imageDiff = false;
    $costDiff = false;

    $rel_path = "img\\shopitems\\$image";

    $rel_path = substr($rel_path, -1) == '/' || substr($rel_path, -1) == '\\' ? "" : "img\\shopitems\\$image";

    $target = realpath(__DIR__ . DIRECTORY_SEPARATOR . '..');
        $full_image = $target . "\\img\\shopitems\\$image";

    $db = databaseHandler::getInstance()->getConnection();

    $stmnt = $db->prepare("SELECT * FROM " . SHOPITEMS . " WHERE id = :id");
      $stmnt->bindParam(":id",  $id);

      $succes = $stmnt->execute();

      if($succes) {
          $data = $stmnt->fetchAll();

          if($data[0]["description"] != $text && $text != "")  $textDiff = true;
          if($data[0]["image"] != $rel_path && $rel_path != "") $imageDiff = true;
          if($data[0]["cost"] != $cost) $costDiff = true;

          echo $textDiff . $imageDiff . $costDiff;

              if($textDiff && $imageDiff && $costDiff) {
                $stmnt = $db->prepare("UPDATE " . SHOPITEMS . " SET image = :image, full_image = :fimage, description = :text, cost = :cost WHERE id = :id");
              } elseif($textDiff && $imageDiff) {
                $stmnt = $db->prepare("UPDATE " . SHOPITEMS . " SET image = :image, full_image = :fimage, description = :text WHERE id = :id");
              } elseif($textDiff && $costDiff) {
                $stmnt = $db->prepare("UPDATE " . SHOPITEMS . " SET description = :text, cost = :cost WHERE id = :id");
              } elseif($costDiff && $imageDiff) {
                $stmnt = $db->prepare("UPDATE " . SHOPITEMS . " SET image = :image, full_image = :fimage, cost = :cost WHERE id = :id");
              } elseif($textDiff) {
                $stmnt = $db->prepare("UPDATE " . SHOPITEMS . " SET description = :text WHERE id = :id");
              } elseif($costDiff) {
                $stmnt = $db->prepare("UPDATE " . SHOPITEMS . " SET cost = :cost WHERE id = :id");
              } elseif($imageDiff) {
                $stmnt = $db->prepare("UPDATE " . SHOPITEMS . " SET image = :image, full_image = :fimage WHERE id = :id");
              }

            $stmnt->bindParam(":id", $this->id);

            if($imageDiff) {
              $stmnt->bindParam(":image", $rel_path);
              $stmnt->bindParam(":fimage", $full_image);
            }

            if($costDiff) {
              $stmnt->bindParam(":cost", $cost);
            }

            if($textDiff) {
              $stmnt->bindParam(":text", $text);
            }
            $succes = $stmnt->execute();

            if($succes) {

              if($imageDiff === true) {
                unlink($data[0]["full_image"]);
                uploadToDirectory("img", "shopitems");
              }

              echo "<p>You sucessfully updated the shop item.</p>";

            }
      }
}

public static function create($text, $itemName, $image, $cost) {
    $db = databaseHandler::getInstance()->getConnection();

    $target = realpath(__DIR__ . DIRECTORY_SEPARATOR . '..');
    $dest = $target . "\\img\\shopitems\\$image";

    $imageLocation = "img/shopitems/$image";

    $stmnt = $db->prepare("INSERT INTO " . SHOPITEMS . " (description, itemname, full_image, image, cost) VALUES (:text, :itemname, :fimage, :image, :cost) ");
        $stmnt->bindParam(":text", $text);
  $stmnt->bindParam(":itemname", $itemName);
        $stmnt->bindParam(":fimage", $dest);
        $stmnt->bindParam(":image", $imageLocation);
  $stmnt->bindParam(":cost", $cost);

        $succes = $stmnt->execute();
            if($succes) {
                echo '<p>Sucessfully added the partner to the database!</p>';
            }
}

  /*

  Een functie voor het updaten van de waarden die boven aan de class staan.

  */
  public function updateValues() {
    // een verbinding leggen met de database te maken, door het te referencen naar de verbinding
    $db = databaseHandler::getInstance()->getConnection();

// de prepared statement maken met het gebruik maken van de shopitems constant en het ID van het object
  $stmnt = $db->prepare("SELECT * FROM " . SHOPITEMS . " WHERE id = :id");
  // de id parameter binden aan het object id
        $stmnt->bindParam(":id", $this->id);

        // executen van de prepared statement
    $succes = $stmnt->execute();

    // checken of het executen van de statement succesvol was
    if($succes) {
      // alle data ophalen
        $data = $stmnt->fetchAll();

        // de nieuwe values van het object ophalen uit de database
        $this->desc = $data[0]["description"];
        $this->image = $data[0]["image"];
        $this->full_image = $data[0]["full_image"];
        $this->cost = $data[0]["cost"];
        $this->itemName = $data[0]["itemname"];
    }

  }

public function getId() {
  return $this->id;
}

public function getName() {
  return $this->itemName;
}

public function getImage() {
  return $this->image;
}

public function getFullImage() {
  return $this->full_image;
}

public function getDesc() {
  return $this->desc;
}

 public function getCost() {
  return $this->cost;
}

}

我们需要在这里查看'update'和'updateValues'函数,updateValues函数应该更新类的值,然后在这里更新表单数据:

if(isset($_POST['shopitemData'])) {
  $shopitem = shopItem::getById($_POST['shopitemData']);
  $shopitem->updateValues();
  echo '
   <p>Description of shop item</p>
   <textarea name="desc">' . $shopitem->getDesc() . '</textarea>
   <p>Image of shop item (Leave empty if you do not wish to change this)</p>
   <input type="file" name="img">
   <p>price of shop item</p>
   <input type="number" name="cost" value="' . $shopitem->getCost() . '"> 
   <input type="submit" name="save" value="Save">';

     if(isset($_POST['desc']) && isset($_POST['cost'])) {
       $shopitem->update(isset($_POST['desc']) ? $_POST['desc'] : "",      isset($_FILES['img']) ? $_FILES['img']["name"] : "", isset($_POST['cost']) ? $_POST['cost'] : "");
    }
}

但是,我仍然需要刷新表单重新提交以使其更新,有没有办法更新它而无需重新提交它而不使用像AJAX这样的外部库?

0 个答案:

没有答案