如何将出生日期从数据库显示到表单并在 php 和 PDO 中更新?

Posted

技术标签:

【中文标题】如何将出生日期从数据库显示到表单并在 php 和 PDO 中更新?【英文标题】:how to display the date of birth from a database to a form and update it in php & PDO? 【发布时间】:2017-06-07 02:24:42 【问题描述】:

我有三个页面,我想通过其唯一 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】:

您似乎没有在“出生日期”输入中设置“值”标签,因此当您的记录更新和页面刷新时,您会丢失它的值。

另外,你输入的名字是birth_data,而在php中你试图访问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"]?>">

【讨论】:

我之前添加了这个,现在又添加了这个,但仍然没有工作:(我已经完成了 print_r($values); 它提供了包括出生日期在内的所有值,但是当我想显示这个时我的出生日期字段未显示。 @ArifulHaqueAnna 它没有显示在数据库或 html 页面中吗? 不是所有东西都显示在数据库和 html 页面中,但在 html 页面中,出生日期未显示在出生日期字段中。我已经检查过了。 @ArifulHaqueAnna 但您更新的问题显示的日期输入行与我在答案中提供的行不同。我通过在 html 表单顶部的 php 代码中设置$values["birth_date"] = "test123"; 测试了我的答案,它似乎对我有用。 我明白了....我得到了答案。表格***.com/questions/2215354/…

以上是关于如何将出生日期从数据库显示到表单并在 php 和 PDO 中更新?的主要内容,如果未能解决你的问题,请参考以下文章

Magento从php表单中收集出生日期并插入DB

如何从 javascript 变量发送表单值并在 php 中作为整数接收? [复制]

如何在 MySQL 数据库中显示日期和价格,并在 PHP 中以各种格式显示

jQuery - 如何在显示警报消息后阻止表单发布数据?

将日期插入数据库后如何在vue js中显示成功消息

PHP如何从出生日期开始计算年龄