MySQL & PHP 更新问题,使用准备好的语句

Posted

技术标签:

【中文标题】MySQL & PHP 更新问题,使用准备好的语句【英文标题】:MySQL & PHP Update issue, using prepared statements 【发布时间】:2015-09-15 01:40:11 【问题描述】:

我被要求与某人一起开展一个项目(从代码中猜测行业没有奖品)。虽然我对网站的 html 和 CSS 很满意,但这确实是我第一次尝试创建一个可以查看、删除和更新记录的管理页面。

我的问题来自于尝试更新通过 URL 传递的 id 链接到的特定记录。当前数据显示正常。当我尝试将新数据添加到字段时,我得到声明的所有变量的“未定义索引”。根本没有数据更新。

以前,我遇到过一个问题,看起来数据可以正常更新,但该特定记录的所有字段都呈现为空白。

表单标签的“动作”部分没有附加页面,因为我假设如果脚本发生在同一页面上,它不需要指向任何地方。

删除功能已被禁用。我认为同样的问题也会在那里持续存在,直到被重新引用。

请对我温柔一点。

<html>
<?php
    // declare connection variables
    $host = "localhost";
    $username = "root";
    $password = "";
    $dbname = "tsdb";
    $socket = "/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock";

    // DB Connect
    $mysqli = new MySQLi($host, $username, $password, $dbname, ini_get('mysqli.default_port'), $socket) or die ('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);

    // get URL record 'id'
    $id = $_GET['id'];

    // get result of record from 'id'
    $query = "SELECT * FROM models WHERE id =$id";
    $result = $mysqli->query($query);
    $row = $result->fetch_array();
    $result->free();
    ?>

    <!-- vertical table for record edit -->

    <div class="container-fluid">
        <div class="row">
            <div class="col-md-6">
                <h3>Edit Details</h3>

                <form class="form-group" id="editRecord" action="#" method="post">
                    <table class="table table-striped">
                        <tr>
                            <th scope="row">Record ID:</th>

                            <td><?php echo $row['id']; ?></td>
                        </tr>

                        <tr>
                            <th scope="row">Display Name:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['name']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Featured:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['featured']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Sex:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['sex']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Age:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['age']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Ethnicity:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['ethnicity']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Contact Number:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['number']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Email:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['email']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Display Email ?:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['displayEmail']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Postcode:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['postcode']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Nearest Station:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['station']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Parking:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['parking']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Shower:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['shower']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Outcalls:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['outcalls']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Price:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['price']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Heading:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['heading']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Bio:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['bio']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Created:</th>

                            <td><?php echo $row['created']; ?></td>
                        </tr>

                        <tr>
                            <th scope="row">Last Updated:</th>

                            <td><?php echo $row['updated']; ?></td>
                        </tr>
                    </table>

                    <div class="btn-group">
                        <button class="btn btn-default" name="update" type="submit">Update Record</button> 
                        <button class="btn btn-default" name="delete" type="submit">Delete Record</button>
                    </div>

                </form>
            </div>
        </div>
    </div>

    <?php   

    // button execute code
    if (isset($_POST['update'])) 

        $name = $_POST['name'];
        $featured = $_POST['featured'];
        $sex = $_POST['sex'];
        $age = $_POST['age'];
        $ethnicity = $_POST['ethnicity'];
        $number = $_POST['number'];
        $email = $_POST['email'];
        $displayEmail = $_POST['displayEmail'];
        $postcode = $_POST['postcode'];
        $station = $_POST['station'];
        $parking = $_POST['parking'];
        $shower = $_POST['shower'];
        $outcalls = $_POST['outcalls'];
        $price = $_POST['price'];
        $heading = $_POST['heading'];
        $bio = $_POST['bio'];

        $updateQuery = "UPDATE models SET name=?, featured=?, sex=?, age=?, ethnicity=?, number=?, email=?, displayEmail=?, postcode=?, station=?, parking=?, shower=?, outcalls=?, price=?, heading=?, bio=? WHERE id = 'id'"; 

    $updatestmt = $mysqli->prepare($updateQuery);                    
    $updatestmt->bind_param('sssissssssssssss', $name, $featured, $sex, $age, $ethnicity, $number, $email, $displayEmail, $postcode, $station, $parking, $shower, $outcalls, $price, $heading, $bio);

    $updatestmt->execute();
    $updatestmt->close();
    
    elseif (isset($_POST['delete'])) 
        $deleteAlert = "Feature not available!";
        echo "<script type='text/javascript'>alert('$deleteAlert');</script>";  
    ;
    $mysqli->close();
    ?>
</body>
</html>

【问题讨论】:

看看最后的$updateQueryWHERE id = 'id'应该是$id 每个输入都需要有一个 name 属性,以便 php 知道要检索哪个输入(和值) 【参考方案1】:

您没有指定输入标签的名称,例如应该是&lt;input name="featured"

【讨论】:

谢谢!效果很好!我不敢相信我错过了这么简单的事情。我假设它从 $row 读取值。

以上是关于MySQL & PHP 更新问题,使用准备好的语句的主要内容,如果未能解决你的问题,请参考以下文章

PHP MYSQL 使用数组中的值更新多行

从 4.1 升级到 MySQL 5.5 后,Php-MySQL UPDATE & INSERT 停止工作

PHP Mysql获取表上次更新时间和日期[重复]

PHP $stmt->num_rows 不适用于准备好的语句[重复]

PHP MySQL & AJAX 搜索过滤器时间延迟

ZKEYS公有云管理系统一键部署操作流程(更新)