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 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误