1

I am creating an online registration system that allows clubs register themselves and their players for the upcoming season. I have created classes for the two entities and also the functions for database connection, and database query.

While the registration process for the club is without issue, the process for the players however is not working and I do not see anything wrong with it. After filling the form and submitting, it keeps executing the else clause. Here is the code, please any ideas? I would be much grateful.

NB: Errors are turned on, and the code does not display any errors.

<?php include("includes/header.php"); ?>
<?php include("includes/navigation.php"); ?>
<div class="strip"></div>

<?php

  public function save() {

    return isset($this->id) ? $this->update() : $this->create();

   }

   public function create() {

    global $database;

    $properties = $this->clean_properties();

    $sql = "INSERT INTO " . static::$db_table . " (" . implode(",", array_keys($properties)) . ")";
    $sql .= " VALUES ('" . implode("','", array_values($properties)) . "')";

    if ($database->query($sql)) {
        $this->id = $database->the_insert_id();
        return true;
    } else {
        return false;
    }

} // End of Create Method

public function update() {

    global $database;
    $properties = $this->clean_properties();

    $property_pairs = array();

    foreach ($properties as $key => $value) {
        $property_pairs[] = "{$key}='{$value}'";
    }


    $sql = "UPDATE " . static::$db_table . " SET ";
    $sql .= implode(", ", $property_pairs);
    $sql .= " WHERE id= " . $database->escape_string($this->id);

    $database->query($sql);

    return (mysqli_affected_rows($database->connection) == 1) ? true : false;

} // End of Update Method

  $player = new Player();
  if (isset($_POST['submit'])) {
  if ($player) {
     $player->user_id = 10;
     $player->player_id = sanitize($_POST['player_id']);
     $player->jersey = (int) $_POST['jersey'];
     $player->surname = sanitize($_POST['surname']);
     $player->firstName = sanitize($_POST['firstName']);
     $player->otherNames = sanitize($_POST['otherNames']);
     $player->dob = $_POST['dob'];
     $player->placeOfBirth = sanitize($_POST['placeOfBirth']);
     $player->nationality = sanitize($_POST['nationality']);
     $player->stateOfOrigin = sanitize($_POST['stateOfOrigin']);
     $player->previousClub = sanitize($_POST['previousClub']);
     $player->previousLicenseNo = (int)$_POST['previousLicenseNo'];
     $player->height = sanitize($_POST['height']);
     $player->weight = sanitize($_POST['weight']);
     $player->set_file("user_image");
     $player->upload_photo();

     if ($player->save()) {
        redirect_to("add_player.php");
     } else {
        $session->message('<p class="text-danger text-center bg-danger">Something went wrong with the code</p>');
        redirect_to("add_player.php");
     }
    }
  }


?>


  <form class="form_sec" action="new_player.php" enctype="multipart/form-data" method="POST">
     <div class="container">
       <div class="banner_strip player_strip">
          <div class="row">
            <div class="col-md-12">
              <div class="club_logo">
                <h1 class="text-center text-success bg-success">Register a maximum of (30) players</h1>
              </div>
           </div>
           <div class="col-md-12">
              <?= $session->message(); ?>
            </div>
          </div>
        </div>
        <div class="form_container">
            <div class="row">
               <div class="col-md-12">
                 <div class="row">
                    <div class="col-md-6">
                     <div class="form-group">
                        <label for="jersey">Jersey No.</label>
                          <input type="text" name="jersey" value="" class="form-control">
                       </div>
                     <div class="form-group">
                       <label for="surname">Surname</label>
                       <input type="text" name="surname" value="" class="form-control">
                     </div>
                     <div class="form-group">
                       <label for="firstName">First Name</label>
                       <input type="text" name="firstName" value="" class="form-control">
            </div>
                     <div class="form-group">
                        <label for="otherNames">Othernames</label>
                        <input type="text" name="otherNames" value="" class="form-control">
                     </div>
                     <div class="form-group">
                        <label for="dob">Date of Birth</label>
                        <input type="date" name="dob" value="" class="form-control">
                    </div>
                    <div class="form-group">
                    <label for="placeOfBirth">Place of Birth</label>
                    <input type="text" name="placeOfBirth" value="" class="form-control">
                </div>
                <div class="form-group">
                    <label for="nationality">Nationality</label>
                    <input type="text" name="nationality" value="" class="form-control">
                </div>
            </div>
          <div class="col-md-6">
            <div class="form-group">
              <label for="stateOfOrigin">State of Origin</label>
              <input type="text" name="stateOfOrigin" value="" class="form-control">
            </div>
            <div class="form-group">
              <label for="previousClub">Previous Club</label>
              <input type="text" name="previousClub" value="" class="form-control">
            </div>
            <div class="form-group">
              <label for="player_id">Player ID (If any)</label>
              <input type="text" name="player_id" value="" class="form-control">
            </div>
            <div class="form-group">
              <label for="previousLicenseNo">Previous License No.</label>
              <input type="text" name="previousLicenseNo" value="" class="form-control">
            </div>
            <div class="form-group">
              <label for="height">Height</label>
              <input type="text" name="height" value="" class="form-control">
            </div>
            <div class="form-group">
              <label for="height">Weight</label>
              <input type="text" name="weight" value="" class="form-control">
            </div>
            <div class="form-group">
              <label for="user_image">Upload Picture</label>
              <input type="file" name="user_image" value="" class="form-control">
            </div>
          </div>
          <div class="col-md-12">
            <input type="submit" name="submit" value="Register Player" class="btn btn-block btn-success">
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
 </form>


<?php include("includes/footer.php"); ?>
Bobbymax
  • 11
  • 2
  • 1
    Please provide further code examples of the `Player::save()` method. Also, I don't know why you redirect on failure, you're already on what appears to be the edit page? – Scuzzy Feb 12 '17 at 08:18
  • The form was being submitted on another page initially, you would see that from the form action. However for the purpose of submitting this post, I put all codes together. As regards the Player::save() method, The code seems to long for this comment section, but it works as I used it for the club registration. I hope this helps. – Bobbymax Feb 12 '17 at 08:28
  • Stack Overflow is not a code review site. One doesn't post their code asking "what is the problem with it" . Only questions regarding certain problems are on topic. – Your Common Sense Feb 12 '17 at 08:35
  • @YourCommonSense Disagree. Not to mention, the duplicate has nothing to do with what's being asked. If what you said is true, this would be off-topic, not a duplicate. – Trojan404 Feb 12 '17 at 08:54
  • The problem is in the create function. It keeps going to the else statement because your function call to create is going to its else statement. This is looking at it without testing, but I'm betting it's the query. Try outputting the query as raw text to see how it's formatted and go from there. – Trojan404 Feb 12 '17 at 08:57

0 Answers0