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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度