使用另一个表的值多次更新表

时间:2015-08-07 00:46:17

标签: php mysql forms pdo updates

对我来说,这是一个简单的问题,但我一直在寻找并且找不到合适的解决方案。

我有三张桌子。一个用于联系,一个用于状态,一个用于是/否问题。 contacts表是主表,它引用其他两个表以获取某些字段的值。 yes / no表在contacts表中被引用三次。州表被引用一次。

我已经能够编写插入php文件而没有问题,相应的选择框显示状态和yes / no表中的值,并将值插入contacts表中。

我遇到的问题是更新php文件。我无法找到可以多次使用yes / no表来更新contacts表中字段的代码示例。

我正在使用当前版本的xampp与当前版本的php和mysql。我使用PDO编码作为mysql的连接。

这是当前更新的mysql代码(我知道需要连接到状态和yesno表,我只是不知道它应该是什么。)

<?php

require '../app/start.php';

if (!empty($_POST)) {
    $id = $_POST['id'];
    $deceased = $_POST['deceased'];
    $active = $_POST['active'];
    $realname = $_POST['realname'];
    $slug = $_POST['slug'];
    $nickname = $_POST['nickname'];
    $prefernick = $_POST['prefernick'];
    $dob = $_POST['dob'];
    $address = $_POST['address'];
    $city = $_POST['city'];
    $statelookup = $_POST['statelookup'];
    $zipcode = $_POST['zipcode'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    $emailtwo = $_POST['emailtwo'];
    $facebook = $_POST['facebook'];
    $twitter = $_POST['twitter'];

    $updatePage = $db ->prepare("
        UPDATE contacts
        SET
            deceased = :deceased,
            active = :active,
            realname = :realname,
            slug = :slug,
            nickname = :nickname,
            prefernick = :prefernick,
            dob = :dob,
            address = :address,
            city = :city,
            statelookup = :statelookup,
            zipcode = :zipcode,
            phone = :phone,
            email = :email,
            emailtwo = :emailtwo,
            facebook = :facebook,
            twitter = :twitter,
            updated = NOW()
        WHERE id = :id
    ");

    $updatePage->execute([
        'id' => $id,
        'deceased' => $deceased,
        'active' => $active,
        'realname' => $realname,
        'slug' => $slug,
        'nickname' => $nickname,
        'prefernick' => $prefernick,
        'dob' => $dob,
        'address' => $address,
        'city' => $city,
        'statelookup' => $statelookup,
        'zipcode' => $zipcode,
        'phone' => $phone,
        'email' => $email,
        'emailtwo' => $emailtwo,
        'facebook' => $facebook,
        'twitter' => $twitter,
    ]);

    header('Location: ' . BASE_URL . '/adminix/list-contacts.php');
}

if (!isset($_GET['id'])) {
    header('Location: ' . BASE_URL . '/adminix/list-contacts.php');
        die();
}

$page = $db->prepare("
    SELECT *
    FROM contacts
    WHERE id = :id
");

$page->execute(['id' => $_GET['id']]);

$page = $page->fetch(PDO::FETCH_ASSOC);

require VIEW_ROOT . '/adminix/edit-contacts.php';

这是当前更新的PHP代码和表单(我知道需要更改选择选项,我只是不知道从联系人表中提取现有值应该是什么,但也显示可用来自states表或yesno表的选项。)

<?php require VIEW_ROOT . '/templates/header.php'; ?>

<?php
// Require SQL Look-ups

$yesno1 = $db->query("
    SELECT *
    FROM yesno
    ORDER BY yesno_id DESC
")->fetchAll(PDO::FETCH_ASSOC);

$yesno2 = $db->query("
    SELECT *
    FROM yesno
    ORDER BY yesno_id ASC
")->fetchAll(PDO::FETCH_ASSOC);

$states = $db->query("
    SELECT *
    FROM states
    ORDER BY states_id DESC
")->fetchAll(PDO::FETCH_ASSOC);

?>

    <h2>EDIT CONTACTS PAGE</h2>

    <form action="<?php echo BASE_URL; ?>/adminix/edit-contacts.php" method="POST" autocomplete="off">

        <table width="100%" border="1">
            <tr>
                <td>
                    <label for="deceased">
                        Deceased:
                    </label>
                </td>
                <td>
                    <select name="deceased" id="deceased">
                        <?php
                        foreach ($yesno1 as $rowone) {
                            print '<option selected="'.$page['deceased'].'">'.$rowone['yesno'].'</option>';
                        }
                        ?>
                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    <label for="active">
                        Active:
                    </label>
                </td>
                <td>
                    <select name="active" id="active">
                        <?php
                        foreach ($yesno2 as $rowtwo) {
                            print '<option value="'.$rowtwo['yesno_id'].'">'.$rowtwo['yesno'].'</option>';
                        }
                        ?>
                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    <label for="realname">
                        Real Name:
                    </label>
                </td>
                <td>
                    <input type="text" name="realname" id="realname" value="<?php echo escape($page['realname']); ?>">
                </td>
            </tr>
            <tr>
                <td>
                    <label for="slug">
                        Slug:
                    </label>
                </td>
                <td>
                    <input type="text" name="slug" id="slug" value="<?php echo escape($page['slug']); ?>">
                </td>
            </tr>
            <tr>
                <td>
                    <label for="nickname">
                        Nick Name:
                    </label>
                </td>
                <td>
                    <input type="text" name="nickname" id="nickname" value="<?php echo escape($page['nickname']); ?>">
                </td>
            </tr>
            <tr>
                <td>
                    <label for="prefernick">
                        Prefer Nick Name:
                    </label>
                </td>
                <td>
                    <select name="prefernick" id="prefernick">
                        <?php
                        foreach ($yesno2 as $rowthree) {
                            print '<option value="'.$rowthree['yesno_id'].'">'.$rowthree['yesno'].'</option>';
                        }
                        ?>
                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    <label for="dob">
                        Date of Birth:
                    </label>
                </td>
                <td>
                    <input type="date" name="dob" id="dob"  value="<?php echo escape($page['dob']); ?>">
                </td>
            </tr>
            <tr>
                <td>
                    <label for="address">
                        Address:
                    </label>
                </td>
                <td>
                    <input type="text" name="address" id="address"  value="<?php echo escape($page['address']); ?>">
                </td>
            </tr>
            <tr>
                <td>
                    <label for="city">
                        City:
                    </label>
                </td>
                <td>
                    <input type="text" name="city" id="city"  value="<?php echo escape($page['city']); ?>">
                </td>
            </tr>
            <tr>
                <td>
                    <label for="statelookup">
                        State:
                    </label>
                </td>
                <td>
                    <select name="statelookup" id="statelookup">
                        <?php
                        foreach ($states as $rowfour) {
                            print '<option value="'.$rowfour['states_id'].'">'.$rowfour['state_lookup'].'</option>';
                        }
                        ?>
                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    <label for="zipcode">
                        Zip Code:
                    </label>
                </td>
                <td>
                    <input type="text" name="zipcode" id="zipcode"  value="<?php echo escape($page['zipcode']); ?>">
                </td>
            </tr>
            <tr>
                <td>
                    <label for="phone">
                        Phone:
                    </label>
                </td>
                <td>
                    <input type="text" name="phone" id="phone"  value="<?php echo escape($page['phone']); ?>">
                </td>
            </tr>
            <tr>
                <td>
                    <label for="email">
                        Email:
                    </label>
                </td>
                <td>
                    <input type="text" name="email" id="email"  value="<?php echo escape($page['email']); ?>">
                </td>
            </tr>
            <tr>
                <td>
                    <label for="emailtwo">
                        Email Two
                    </label>
                </td>
                <td>
                    <input type="text" name="emailtwo" id="emailtwo"  value="<?php echo escape($page['emailtwo']); ?>">
                </td>
            </tr>
            <tr>
                <td>
                    <label for="facebook">
                        Facebook:
                    </label>
                </td>
                <td>
                    <input type="text" name="facebook" id="facebook"  value="<?php echo escape($page['facebook']); ?>">
                </td>
            </tr>
            <tr>
                <td>
                    <label for="twitter">
                        Twitter:
                    </label>
                </td>
                <td>
                    <input type="text" name="twitter" id="twitter"  value="<?php echo escape($page['twitter']); ?>">
                </td>
            </tr>
            <tr>
                <td>
                    <input type="hidden" name="id" value="<?php echo escape($page['id']) ?>">
                    <input type="submit" value="Submit Edited Contact">
                </td>
            </tr>
        </table>

    </form>

<?php require VIEW_ROOT . '/templates/footer.php'; ?>

yesno表有两个字段,yesno_id和yesno,yesno字段的值是Yes和No.State表有twofields,states_id和state_lookup以及50个美国州的列表。

非常感谢任何可以提供的帮助。

1 个答案:

答案 0 :(得分:0)

Ok, I think I understand. The structure of the yesno table is insufficient for your needs. In fact, I would suggest that you not use that table at all. Instead change the column types of 'deceased' and 'prefernick' to an enum.

deceased ENUM('Yes', 'No')
prefernick ENUM('Yes', 'NO')

Then in you html

<select name="deceased" id="deceased">
<option <?php echo $page['deceased'] === 'Yes' ? 'selected' : ''; ?> value="Yes">Yes</option>
<option <?php echo $page['deceased'] === 'No' ? 'selected' : ''; ?> value="No">No</option>
</select>

Be sure to do the same with the 'prefernick' select as well.

In your PHP:

   //You forgot to prefix the keys with colons.
   $updatePage->execute([
        ':id' => $id,
        ':deceased' => $deceased,
        ':active' => $active,
        ':realname' => $realname,
        ':slug' => $slug,
        ':nickname' => $nickname,
        ':prefernick' => $prefernick,
        ':dob' => $dob,
        ':address' => $address,
        ':city' => $city,
        ':statelookup' => $statelookup,
        ':zipcode' => $zipcode,
        ':phone' => $phone,
        ':email' => $email,
        ':emailtwo' => $emailtwo,
        ':facebook' => $facebook,
        ':twitter' => $twitter,
    ]);

I hope this helps