Ariful Haque Anna
Ariful Haque Anna

Reputation: 37

how to display the date of birth from a database to a form and update it in php & PDO?

I have three pages where i want to update an existing row of a specific user by their unique id.But when i click to the update link it shows all the field with their values except the date of birth. I am trying to find out the problem. but failed as i am quite new to php. any one please help if you can. Here is my HTML form

<form action="update.php" method="post">
    <table>
        <?php
        include_once ("../vendor/autoload.php");
        use  Admission\Admission\Admission;
        $object_for_showDetails = new Admission();
        $object_for_showDetails->setData($_GET);

        $data = $object_for_showDetails->details();
        $values = $data;

        ?>
        <th align="center" colspan="2"> <h3>Applicant's Information</h3></th>
        <tr>
            <td>Name of Applicant:<spam>*</spam></td>
            <td><input class="nice" type="text" name="name" value="<?php echo $values['student_name']?>"></td>
            <td><input type="hidden" name="id" value="<?php echo $values['uid']?>"></td>

        </tr>

        <tr>
            <td>Email:<spam>*</spam></td>
            <td><input class="nice" type="email" name="email" value="<?php echo $values['email']?>"></td>
        </tr>

        <tr>
            <td>  Gender:<spam>*</spam></td>

            <td>  <input type="radio" class="radio" value="male" name="gender" <?php echo ($values['gender']=='male')?'checked':'';?> />Male
                <input type="radio" class="radio" value="female" name="gender" <?php echo ($values['gender']=='female')?'checked':'';?>  /> Female </td>

        </tr>
        <tr>
            <td>  Birth Date:<spam>*</spam></td>
            <td>  <input class="nice" type="date" name="<?php echo $values['birth_date']?>" > </td>

        </tr>
        <tr>
            <td>  Religion:<spam>*</spam></td>
            <td> <input class="nice" type="text" name="religion" value="<?php echo $values['religion']?>"></td>

        </tr>
        <tr>
            <td> Phone:<spam>*</spam></td>
            <td> <input class="nice" type="text" name="phone" value="<?php echo $values['phone']?>"></td>

        </tr>
        <th align="center" colspan="2"> <h3>Parent's information</h3></th>
        <tr>
            <td> Father Name::<spam>*</spam></td>
            <td> <input class="nice" type="text" name="father_name" value="<?php echo $values['father_name']?>"></td>
        </tr>
        <tr>
            <td> Mother Name::<spam>*</spam></td>
            <td>  <input class="nice" type="text" name="mother_name" value="<?php echo $values['mother_name']?>"></td>
        </tr>
        <tr>
            <td> Guardian/Parent's Phone:<spam>*</spam></td>
            <td> <input class="nice" type="text" name="guardian_phone" value="<?php echo $values['guardian_phone']?>"></td>
        </tr>
        <td>
            <a href="details.php?id=<?php echo $data['uid']?>">Details</a>|
            <a href="edit.php?id=<?php echo $data['uid']?>">Update Info</a>|
            <a href="delete.php?id=<?php echo $data['uid']?>">Delete</a>
        </td>
        <tr>
            <td colspan="2" align="center"><input type="submit" value="update" class="btn"></td>
        </tr>
    </table>
</form>

Here is my update.php file

 <?php
include_once ("../vendor/autoload.php");
use  Admission\Admission\Admission;
if($_SERVER['REQUEST_METHOD'] == 'POST'){
    $object_of_update = new Admission();

    $object_of_update->setData($_POST);

    $object_of_update->update();
}
else{
    header("location:create.php");
}
?>

and last the update methode under the main class file Admission.php

where setData() is for set the values and details() is for showing the data which fetch data from the database of a specific user and the update() method for updating the values from the database.

    public function store()
    {
        try {
            $pdo = new PDO ('mysql:host=localhost;dbname=university', 'root', '');
            $query_for_insert = "INSERT INTO `student_form`(`id`, `uid`, `student_name`, `email`, `birth_date`, `gender`, `religion`, `phone`, `father_name`, `mother_name`, `guardian_phone`) 
  VALUES (:id,:uid,:student_name,:email,:birth_date,:gender,:religion,:phone,:father_name,:mother_name,:guardian_phone)";
            $statement = $pdo->prepare($query_for_insert);
            $statement->execute(array(
                ':id' => null,
                ':uid' => uniqid(),
                ':student_name' => $this->name,
                ':email' => $this->email,
                ':birth_date' => $this->birth_date,
                ':gender' => $this->gender,
                ':religion' => $this->religion,
                ':phone' => $this->phone,
                ':father_name' => $this->father_name,
                ':mother_name' => $this->mother_name,
                ':guardian_phone' => $this->guardian_phone,
            ));
            if ($statement) {
                session_start();
                $_SESSION['message'] = "You have successfully Applied to the University Of Dhaka.Please Bring your Admit card during Admission test";
                header("location:register.php");
            }
        } catch (PDOException $e) {
            echo "Connection Error" . $e->getMessage();
        }

    }
public function update(){
     echo "update method";
    try {
        $pdo = new PDO ('mysql:host=localhost;dbname=university', 'root', '');
        $queryForUpdata = "UPDATE `student_form` SET `student_name`=:student_name,`email`= :email,`birth_date` =:birth_date,`gender`=:gender,`religion`=:religion,`phone`=:phone,`father_name`=:father_name,`mother_name`=:mother_name,`guardian_phone`=:guardian_phone WHERE uid="."'".$this->id."'";
        $statement = $pdo->prepare($queryForUpdata);
        $statement->execute(array(
            ':student_name' => $this->name,
            ':email' => $this->email,
            ':birth_date' => $this->birth_date,
            ':gender' => $this->gender,
            ':religion' => $this->religion,
            ':phone' => $this->phone,
            ':father_name' => $this->father_name,
            ':mother_name' => $this->mother_name,
            ':guardian_phone' => $this->guardian_phone
        ));
        if ($statement) {
            session_start();
            $_SESSION['message'] = "You have successfully Updated Your Information";
            header('location:register.php');
        }
    }
    catch (PDOException $e) {
        echo "Connection Error" . $e->getMessage();
    }

}

Please if anyone can fix this out i will be grateful to you.

Upvotes: 1

Views: 3173

Answers (1)

ksimunovic
ksimunovic

Reputation: 81

It looks like you are not setting "value" tag on "Birth date" input so when your record updates and page refreshes you are missing its value.

Also, your input name is birth_data while in php you are trying to access birth_date

<input class="nice" type="date" name="birth_date" value="<?php echo is_object($values["birth_date"]) ? $values["birth_date"]->format("Y-m-d") : $values["birth_date"]?>">

Upvotes: 1

Related Questions