更新声明将值设置为null而不是主键

时间:2019-01-16 15:04:25

标签: javascript php html mysql

我正在开发一个Web应用程序,该应用程序将允许您通过表格将车手分配给货车。 您单击页面上表格中的骑手,将弹出一个对话框,选择货车,然后单击添加。我遇到的问题是我的更新语句将mysql表中的所有值都设置为null,而不是另一个表中的主键。

 <div class="table-responsive" id="refresh">
                                    <table id="mainTable" class="table m-b-0 table-hover">
                                        <thead>
                                            <tr>
                                                <th>Primary Key</th>
                                                <th>Party Name</th>
                                                <th>Address</th>
                                                <th>Number in Party</th>
                                                <th>Number of Coolers</th>
                                                <th>Phone Number</th>
                                                <th>Time of PickUP</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php while($row = $q->fetch()):?>
                                            <tr data-toggle="modal" data-target="#selectVan">
                                                <td id="riderName" name="primKey">
                                                    <?php echo htmlspecialchars($row['P_Key']) ?>
                                                </td>
                                                <td id="riderName" name="riderName">
                                                    <?php echo htmlspecialchars($row['partyNM']) ?>
                                                </td>
                                                <td>
                                                    <?php echo htmlspecialchars($row['address1'])?>
                                                </td>
                                                <td>
                                                    <?php echo htmlspecialchars($row['numParty'])?>
                                                </td>
                                                <td>
                                                    <?php echo htmlspecialchars($row['numCooler'])?>
                                                </td>
                                                <td>
                                                    <?php echo htmlspecialchars($row['phoneNum'])?>
                                                </td>
                                                <td>
                                                    <?php echo htmlspecialchars($row['pickUp'])?>
                                                </td>
                                            </tr>
                                            <?php endwhile; $sql=null;?>
                                        </tbody>
                                        <tfoot>
                                        </tfoot>
                                    </table>
                                    <!--Select a van for rider-->
                                    <div id="selectVan" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
                                        aria-hidden="true">
                                        <div class="modal-dialog">
                                            <div class="modal-content">
                                                <div class="modal-header">
                                                    <h4 class="modal-title" id="myModalLabel">Select Van</h4>
                                                    <button class="btn btn-icon waves-effect waves-light btn-danger m-b-5"
                                                        type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                                                </div>
                                                <div class="modal-body">
                                                <?php
                                                    $sql = 'Select P_Key, nameVan from vanInfo where in_active=0';
                                                    $q = $dbh->query($sql);
                                                    $q->setFetchMode(PDO::FETCH_ASSOC);
                                                ?>
                                                    <form method="post" role="form" class="form-horizontal"
                                                        data-parsley-validate novalidate>
                                                        <div class="form-group row">
                                                            <select class="form-control select2" id="selectedVan" name="selectedVan">
                                                                <option>Available Vans</option>
                                                                <?php while($row = $q->fetch()):?>
                                                                    <option value="<?php echo htmlspecialchars($row['nameVan']) ?>">
                                                                    <?php echo htmlspecialchars($row['nameVan']) ?>
                                                                    </option>
                                                                <?php endwhile;?>
                                                                </optgroup>
                                                            </select>
                                                        </div>
                                                        <div class="form-group row">

                                                        </div>
                                                        <div class="form-horizontal">
                                                            <button type="button" class="btn btn-default waves-effect m-b-5"
                                                                data-dismiss="modal">Close</button>
                                                            <button name="addRiderToVan" type="submit" class="btn btn-primary waves-effect waves-light m-b-5">Add</button>
                                                        </div>
                                                        <?php
                                                            if(isset($_POST["addRiderToVan"])){
                                                            try {
                                                            $riderKey = $_POST["primKey"];
                                                            $vanName = $_POST["selectedVan"];
                                                            $sql = "UPDATE riderInfo set van_key=(select P_Key from vanInfo where 
                                                            nameVan = '$vanName' and in_active = '0') where P_Key = $riderKey";
                                                            $dbh->exec($sql);
                                                            }catch(Exception $e){
                                                            echo 'Caught exception: ',  $e->getMessage(), "\n";
                                                            }
                                                            }
                                                            $sql = null;
                                                        ?>
                                                    </form>
                                                </div>
                                                <div class="modal-footer">
                                                </div>

我需要为选中的货车更新riderInfo van_key列,即我的vanInfo表的P_Key。

1 个答案:

答案 0 :(得分:0)

我认为您在这里缺少很多东西。

首先编辑引入主键的选择查询。

第二,我建议您命名选择HTML项,而不是其中包含的选项。 喜欢:

<select class="form-control select2" name="selectedVan" >

第三,该选项将如下所示:

<option value="<?php echo htmlspecialchars($row['idVan']) ?>"><?php echo htmlspecialchars($row['nameVan']) ?></option>

最后,获取$ _POST [“ selectedVan”](在上面选择)并更新数据库

$sql = "UPDATE riderInfo set van_key=$vanKey where P_Key = $riderKey";