1 Replies - 125 Views - Last Post: 30 January 2019 - 07:17 PM Rate Topic: -----

#1 Exceedinglife   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 202
  • Joined: 01-July 12

php update page no errors prob something small

Posted 30 January 2019 - 06:56 PM

Hey all,
I have a CRUD php project I am working on. I have insert working reading and deleteing. The only one I have left is update. My page submits the form but when the btn is clicked my page refreshes and does not update in the db. All the controls are cleared. Im assuming Iím missing something simple or just have a small mistake that is running past me,
<?php
    error_reporting(E_ALL & ~E_NOTICE);
  // include config for Database
    require_once "php/config.php";

    // Declare the variables that will be used.
    $name = $language = $datenow = "";
    $nameerror = $langerror = $dateerror = "";

    // Process form data when its submitted
    if(isset($_POST["id"]) && !empty($_POST["id"])) {
      // Get hidden input value.
        $id = $_POST["id"];
          $input_name = trim($_POST["name"]);
          if(empty($input_name)) {
              $nameerror = "Name is required. ";
          } elseif (!filter_var($input_name, FILTER_VALIDATE_REGEXP,
              array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))) {
              $nameerror = "Please enter a valid name. ";
          } else {
              //$namesafe = mysqli_real_escape_string($connection, $input_name);
              $name = $input_name;
          }
          // Validate language entered
          $input_lang = trim($_POST["language"]);
          if(empty($input_lang)) {
              $langerror = "Please enter a language. ";
          } else {
              $language = $input_lang;
          }
          // Get current Date and Time
          $currentDate = date("Y-m-d H:i:s");
          // OR
          $datetimeobj = new DateTime();
          $datetimeobj->format("Y-m-d H:i:s");

          //if(empty($nameerror) && empty($langerror) && empty($dateerror)) {

            if($name != "" && $language != "") {
                $sql = "UPDATE users SET name=:name, language=:language, " .
                       "date=:date WHERE id=:id";
                    error_log($sql);
                if($stmt = $pdoConnect->prepare($sql)) {
                    $stmt->bindParam(":name", $param_name);
                    $stmt->bindParam(":language", $param_lang);
                    $stmt->bindParam(":date", $param_date);
                    $stmt->bindParam(":id", $param_id);
                    $para_name = $name;
                    $param_lang = $lang;
                    $param_date = $date;
                    $param_id = $id;
                    if($stmt->execute()) {
                      header("location: index.php");
                      exit();
                    } else {
                          echo "Something went wrong, try again later.";
                    }
                }
           } else {
                  $errormsg = '<div > 'All fields are required to continue</div>';
           }
            unset($stmt);
        unset($pdoConnect);
    } else {
          if(isset($_GET["id"]) && !empty(trim($_GET["id"]))) {
              $id = trim($_GET["id"]);
              $sql = "SELECT * FROM users WHERE id = :id";
              if($stmt = $pdoConnect->prepare($sql)) {
                  $stmt->bindParam(":id", $param_id);
                  $param_id = $id;
                  if($stmt->execute()) {
                      if($stmt->rowCount() == 1) {
                          $row = $stmt->fetch(PDO::FETCH_ASSOC);
                          //$id = $row["id"];
                          $name = $row["name"];
                          $language = $row["language"];
                          $userdate = $row["date"];

                      } else {
                            header("location: error.php");
                            exit();
                      }
                  } else {
                        echo "Something went wrong with UPDATE, try again later.";
                  }
              }
              // Close $stmt statement
              unset($stmt);
              // Close connection
              unset($pdoConnect);
          } else {
              // URL doesn't contain valid 'id' parameter.
                header("location: error.php");
                exit();
          }
    }

?>



Is This A Good Question/Topic? 0
  • +

Replies To: php update page no errors prob something small

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3810
  • View blog
  • Posts: 13,849
  • Joined: 08-August 08

Re: php update page no errors prob something small

Posted 30 January 2019 - 07:17 PM

What you've got is a string of if...else statements. That defeats the idea behind CRUD, where you'd normally have a controller and a router. The router would determine what controller method to call. By separating the concerns you make the code easier to debug. I suggest you read up on RESTful CRUD.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1