如何显示从数据库到表单的出生日期,并在php& PDO?

时间:2017-01-22 13:55:38

标签: php mysql pdo

我有三个页面,我希望通过其唯一ID来更新特定用户的现有行。但是当我点击更新链接时,它会显示除了出生日期之外的所有字段及其值。我想找出问题所在。但失败了,因为我对php很新。任何人请尽可能帮助。 这是我的HTML表单

<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>

这是我的update.php文件

 <?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");
}
?>

并持续使用主类文件Admission.php

下的更新方法

其中setData()用于设置值和details()用于显示从特定用户的数据库获取数据的数据和用于更新数据库中的值的update()方法。

    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();
    }

}

如果有人能解决这个问题,我将非常感激你。

1 个答案:

答案 0 :(得分:1)

看起来你没有设置&#34;价值&#34;标签&#34;出生日期&#34;输入所以当您的记录更新和页面刷新时,您将丢失其值。

此外,您的输入名称是birth_data,而在php中您尝试访问birth_date

{{1}}