1

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.

1 Answers1

1

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"]?>">
ksimunovic
  • 81
  • 1
  • 3
  • i added this before and now again added this but still not working :( i have done print_r($values); than it gives all the values including date of birth but when i want to show this to my Birth date field is not showing. – Ariful Haque Anna Jan 22 '17 at 14:13
  • @ArifulHaqueAnna is it not showing in database or in html page? – ksimunovic Jan 22 '17 at 14:25
  • no every thing is showing in database and also in html page but in html page the date of birth is not showing in the Birth date field. I have checked. – Ariful Haque Anna Jan 22 '17 at 14:32
  • @ArifulHaqueAnna but your updated questions shows different line for date input than one I provided in my answer. I tested my answer by setting`$values["birth_date"] = "test123";` in php code on top of html form and it seems to work for me. – ksimunovic Jan 22 '17 at 14:39
  • it's ok when i put value in another form called registe.php it store the data and Birth Date as date format but brother as it is an date format so that why when i bring the data from the database it not working i am spending a lot of time. :( – Ariful Haque Anna Jan 22 '17 at 14:55
  • yes it will work but i just need to show the date of birth in date format as i set the input field in date format. – Ariful Haque Anna Jan 22 '17 at 15:03
  • @ArifulHaqueAnna i have updated my answer, please try now – ksimunovic Jan 22 '17 at 15:06
  • 1
    i got it ....i got the answer. form http://stackoverflow.com/questions/2215354/php-date-format-when-inserting-into-datetime-in-mysql – Ariful Haque Anna Jan 22 '17 at 15:23
  • and also thanks to you for helping. thank you very much :) – Ariful Haque Anna Jan 22 '17 at 15:24
  • :( as i select your answer excepted i have a short of reputation you would help me ;) – Ariful Haque Anna Jan 22 '17 at 15:35