dongliang1893 2019-02-02 20:52
浏览 56
已采纳

更新表单中的数据库字段

Hello so i'm learning php and basiclly i want that any field that has been already added to the database to be "editable"

Let me make it more clear with an example

My from looks like this (ver basic)

Firstname [input field]

Lastname [input field]

ZIP Code [input field]

send button

 <form method="post" action="">
            <table>
                <tr>
                    <td>Vorname</td>
                    <td><input type="text" name="firstname"></td>
                </tr>
                <tr>
                    <td>Nachname</td>
                    <td><input type="text" name="lastname"></td>
                </tr>
                <tr>
                    <td>PLZ</td>
                    <td><input type="number" name="plz"></td>
                </tr>
            </table>
            <button type="submit" name="send">Send</button>
  </form>

When the send button is pressed the output is beeing shown in a table below like so

|---------------------|------------------|------------------|
|      Firstname      |     Lastname     |   Zip Code       |
|---------------------|------------------|------------------|
|       Tomas         |       Möller     |   123123         |
|---------------------|------------------|------------------|

and next to those to rows i still have these two (which didn't fit)

|---------------------|------------------|
|     Update          |     Delete       |
|---------------------|------------------|
|      update         |         delete   |
|---------------------|------------------|
 <table class="table">
            <thead>
            <tr>
                <th scope="col">#</th>
                <th scope="col">First Name</th>
                <th scope="col">Last Name</th>
                <th scope="col">PLZ</th>
                <th scope="col">Delete</th>
                <th scope="col">Update</th>
            </tr>
            </thead>
            <tbody>
            <?php
            /**
             * @var $contact ContactDto
             */
            $contacts = $contactRepository->findAll();


            foreach ($contacts as $contact) {
                ?>
                <tr>
                    <th scope="row"><?php echo $contact->getId()?></th>
                    <td><?php echo $contact->getFirstname() ?></td>
                    <td><?php echo $contact->getLastname() ?></td>
                    <td><?php echo $contact->getPlz() ?></td>
                    <td><a href=delete.php?id=<?php echo (int)$contact->getId()?> >Delete</a></td>
                    <td><a href=update.php?id=<?php echo (int)$contact->getId()?> >Update</a></td>
                </tr>

                <?php
                }
                ?>
            </tbody>
        </table>

So basicaly each row has this own update and delte button.

Here is my problem..im not sure how to do this the right way.

I want when the update is clicked, the value from to fields "go back" to the top field and when send is pressed again the values are updated.

Here is my query

public function update(ContactDto $contactDto) {

        $stmt = $this->pdo->prepare("UPDATE contacts SET firstname=:firstname,
                                                                  lastname=:lastname,
                                                                  plz=:plz
                                                                  WHERE id=:id");
        $stmt->bindValue(':firstname', $contactDto->getFirstname(), PDO::PARAM_STR);
        $stmt->bindValue(':lastname', $contactDto->getLastname(), PDO::PARAM_STR);
        $stmt->bindValue(':plz', $contactDto->getPlz(), PDO::PARAM_INT);
        $stmt->bindValue(':id', $contactDto->getId(), PDO::PARAM_INT);
        $stmt->execute();

    }

I did try and make a new update.php file where the data is send, and the fields get filled like so (but that didnt quite work)

this is my update.php file

$contactRepository = new ContactRepository(
    DatabaseConnection::getConnection()
);

$contactDto = new ContactDto();
$contactDto->setId($_GET['id']);
$contactDto->setFirstName($_POST['firstname']);
$contactDto->setLastName($_POST['lastname']);
$contactDto->setPlz($_POST['plz']);

$contactRepository->update($contactDto);

?>

<form method="post" action="update.php">
    <input type="hidden" name="id" value="<?php echo $contactDto->getId(); ?>" />
    <?PHP
    ?>
    <table>
        <tr>
            <th>First Name: </th>
            <td>
                <input type="text" id="vorname" name="firstname" placeholder="Dein Vorname" size="35" value="<?php echo $contactDto->getFirstName(); ?>">
            </td>
        </tr>
        <tr>
            <th>Last Name: </th>
            <td>
                <input type="text" id="nachname" name="lastname" placeholder="Dein Nachname" size="35" value="<?php echo $contactDto->getLastName(); ?>">
            </td>
        </tr>
    </table>

    <input type="submit" name="save" value="Update" >

Which leaves me with..

>

Notice: Undefined index: firstname in C:\xampp\htdocs\test\update.php on line >14

Notice: Undefined index: lastname in C:\xampp\htdocs\test\update.php on line >15

Notice: Undefined index: plz in C:\xampp\htdocs\test\update.php on >line 16

  • 写回答

1条回答 默认 最新

  • douwen5066 2019-02-05 12:03
    关注

    About the update.php page: the error messages you're getting are saying that 'firstname', 'lastname' and 'plz' are not defined.

    This is because you tried to define them using POST:

    $contactDto->setFirstName($_POST['firstname']); $contactDto->setLastName($_POST['lastname']); $contactDto->setPlz($_POST['plz']);

    But in fact, the values of firstname, lastname and plz were not posted to this page. POST is only 'activated' when you click a submit button in a form where you wrote

    <form method="POST"...
    

    But you didn't get on the update.php page via a form, but via a hyperlink in this table you made:

            foreach ($contacts as $contact) {
                ?>
                <tr>
                    <th scope="row"><?php echo $contact->getId()?></th>
                    <td><?php echo $contact->getFirstname() ?></td>
                    <td><?php echo $contact->getLastname() ?></td>
                    <td><?php echo $contact->getPlz() ?></td>
                    <td><a href=delete.php?id=<?php echo (int)$contact->getId()?> >Delete</a></td>
                    <td><a href=update.php?id=<?php echo (int)$contact->getId()?> >Update</a></td>
                </tr>
    
                <?php
                }
                ?>
    

    This is not wrong tho! This table is very good.

    But on the update.php page, you can't define 'firstname', 'lastname' and 'plz' using $_POST['....'].

    The only thing on the update.php page that IS defined, is 'id'. This is because 'id' was the only thing that you sent to this page using the get method. Again: this is the right way to do it so don't change that.

    **

    What you DO need to change, is this:

    **

    change your code:

    $contactDto = new ContactDto();
    $contactDto->setId($_GET['id']);
    $contactDto->setFirstName($_POST['firstname']);
    $contactDto->setLastName($_POST['lastname']);
    $contactDto->setPlz($_POST['plz']);
    

    To this:

      $contactDto = new ContactDto();
        $contactDto->setId($_GET['id']);
    

    So only 'id' is defined. Now define 'firstname', 'lastname' and 'plz' by getting those values from your database, using a query.

    Like this:

    $getstuff = "SELECT * FROM yourtablename WHERE id = $thevariablethatcontainstheid";
    

    (I guess the variable that contains the 'id' is $contactDto->setId(); ??)

    Then execute the query on the next line:

    $results = mysqli_query($yourconnectingthing,$getstuff);
    

    And write this below:

    $row = $results->fetch_assoc();
    

    And then you can define 'firstname', 'lastname' and 'plz'! By writing this:

    $firstname = $row['firstname'];
    $lastname = $row['lastname'];
    $plz = $row['plz'];
    

    Now the last thing you need to change is the values in the editing form. This was your code:

    <tr>
                <th>First Name: </th>
                <td>
                    <input type="text" id="vorname" name="firstname" placeholder="Dein Vorname" size="35" value="<?php echo $contactDto->getFirstName(); ?>">
                </td>
            </tr>
    

    change it to:

    <tr>
                <th>First Name: </th>
                <td>
                    <input type="text" id="vorname" name="firstname" placeholder="Dein Vorname" size="35" value="<?php echo $firstname ?>">
                </td>
            </tr>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化