javascript - Update 语句将值设置为 null 而不是主键

标签 javascript php html mysql

我正在开发一个网络应用程序,允许您通过表单将乘客分配到货车上。 您单击页面表格中的乘客,会出现弹出窗口,您选择货车,然后单击添加。我遇到的问题是我的更新语句将 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>

我需要更新我的 vanInfo 表中所选货车的 RiderInfo van_key 列 P_Key。

最佳答案

我认为你在这里错过了很多东西。

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

其次,我建议您命名所选 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";

关于javascript - Update 语句将值设置为 null 而不是主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54219899/

相关文章:

javascript - 从纯文本文件数据库中获取随机行

javascript - 如何将 HTML 表格转换为 jQuery DataTable?

php - PHP 中带有命名空间的变量函数

javascript - 使用填充的下拉列表复制 php 表的新行

javascript - HTML 脚本标记变灰且未加载

html - CSS :active not working (IE 10)

javascript - immutable.js 可以与服务器上的 redis 互换吗?

javascript - Firebase getDownloadURL() 不返回任何内容

javascript - 使用ng-if来保护页面angular js上的不同内容

JavaScript 值传递