简体   繁体   English

更新表单中的数据库字段

[英]Update database fields in form

Hello so i'm learning php and basiclly i want that any field that has been already added to the database to be "editable" 您好,所以我正在学习php,基本上我希望已添加到数据库中的任何字段都是“可编辑的”

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) 我确实尝试过创建一个新的update.php文件,将数据发送到该文件中,然后像这样填充字段(但效果不佳)

this is my update.php file 这是我的update.php文件

$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 注意:未定义索引:> 14行的C:\\ xampp \\ htdocs \\ test \\ update.php中的名字

Notice: Undefined index: lastname in C:\\xampp\\htdocs\\test\\update.php on line >15 注意:未定义的索引:> 15行的C:\\ xampp \\ htdocs \\ test \\ update.php中的姓氏

Notice: Undefined index: plz in C:\\xampp\\htdocs\\test\\update.php on >line 16 注意:未定义的索引:>第16行的C:\\ xampp \\ htdocs \\ test \\ update.php中的plz

About the update.php page: the error messages you're getting are saying that 'firstname', 'lastname' and 'plz' are not defined. 关于update.php页面:您收到的错误消息是未定义'firstname','lastname'和'plz'。

This is because you tried to define them using POST: 这是因为您尝试使用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. 但实际上,firstname,lastname和plz的值未发布到此页面。 POST is only 'activated' when you click a submit button in a form where you wrote 仅当您单击编写表单的提交按钮时,才“激活” POST

<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: 但是您没有通过表单进入update.php页面,而是通过此表中的超链接创建的:

        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['....']. 但是在update.php页面上,无法使用$ _POST ['....']定义'firstname','lastname'和'plz'。

The only thing on the update.php page that IS defined, is 'id'. IS.update.php页面上唯一定义的是“ id”。 This is because 'id' was the only thing that you sent to this page using the get method. 这是因为“ id”是您使用get方法发送到此页面的唯一内容。 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. 因此,仅定义了“ id”。 Now define 'firstname', 'lastname' and 'plz' by getting those values from your database, using a query. 现在,通过使用查询从数据库中获取这些值来定义“名字”,“姓氏”和“ plz”。

Like this: 像这样:

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

(I guess the variable that contains the 'id' is $contactDto->setId(); ??) (我猜包含'id'的变量是$ 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'! 然后可以定义“名字”,“姓氏”和“ 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM