从Web表单输入数据到mysql

时间:2018-03-12 03:33:58

标签: php html mysql pdo

这是我的第一个Web开发项目。我正在尝试构建一个允许我们跟踪维护任务和信息的Web应用程序。我在提交时收到“无效参数编号:绑定变量数与令牌数不匹配”。我有一种感觉,这是因为某些值为空。 php实现最有效吗?还有另一种方法可以提取所有表单项吗?如何允许NULL条目?

    <form method="post"action="pc12_status2.php">
        <div class="form-row">
            <div class="form-group col-md-2">
                <label for="aircraftModel1">MODEL</label>
                <input type="text" class="form-control" id="aircraftModel1">
            </div>
            <div class="form-group col-md-2">
                <label for="aircraftLocation">LOCATION</label>
                <input type="text" class="form-control" id="aircraftLocation" placeholder="Location">
            </div>
            <div class="form-group col-md-2">
                <label for="aircraftTail">AIRCRAFT TAIL No.</label>
                <input type="text" class="form-control" id="aircraftTail" placeholder="Tail Number">
            </div>
            <div class="form-group col-md-2">
                <label for="dateFlight">DATE OF FLIGHT</label>
                <input type="date" class="form-control" id="dateFlight">
            </div>
            <div class="form-group col-md-4">
                <label for="mechName">NAME OF CE/MECH</label>
                <input type="text" class="form-control" id="mechName" placeholder="Last, First">
            </div>
        </div>
        <div class="form-row form-group sectionBorder">
            <legend>AIRCRAFT STATUS</legend>
            <div class="col-md-1">
                <label for="aircraftStatus1"></label>
                <select class="form-control" id="aircraftStatus1">
                    <option></option>s
                    <option>FMC</option>
                    <option>PMCM</option>
                    <option>PMCS</option>
                    <option>NMCM</option>
                    <option>NMCS</option>
                    <option>DEPOT</option>
                    <option>NRT</option>
                </select>
            </div>
                <div class="col-md-1">
                    <label for="aircraftStatus2"></label>
                    <select class="form-control" id="aircraftStatus2">
                        <option></option>
                        <option>FMC</option>
                        <option>PMCM</option>
                        <option>PMCS</option>
                        <option>NMCM</option>
                        <option>NMCS</option>
                        <option>DEPOT</option>
                        <option>NRT</option>
                    </select>
            </div>
                <div class="col-md-1">
                    <label for="aircraftStatus3"></label>
                    <select class="form-control" id="aircraftStatus3">
                        <option></option>
                        <option>FMC</option>
                        <option>PMCM</option>
                        <option>PMCS</option>
                        <option>NMCM</option>
                        <option>NMCS</option>
                        <option>DEPOT</option>
                        <option>NRT</option>
                    </select>
            </div>
            <div class="col-md-1">
                    <label for="aircraftStatus4"></label>
                    <select class="form-control" id="aircraftStatus4">
                        <option></option>
                        <option>FMC</option>
                        <option>PMCM</option>
                        <option>PMCS</option>
                        <option>NMCM</option>
                        <option>NMCS</option>
                        <option>DEPOT</option>
                        <option>NRT</option>
                    </select>
            </div>
            <div class="col-md-1">
                    <label for="aircraftStatus5"></label>
                    <select class="form-control" id="aircraftStatus5">
                        <option></option>
                        <option>FMC</option>
                        <option>PMCM</option>
                        <option>PMCS</option>
                        <option>NMCM</option>
                        <option>NMCS</option>
                        <option>DEPOT</option>
                        <option>NRT</option>
                    </select>
            </div>
            <div class="col-md-1">
                    <label for="aircraftStatus6"></label>
                    <select class="form-control" id="aircraftStatus6">
                        <option></option>
                        <option>FMC</option>
                        <option>PMCM</option>
                        <option>PMCS</option>
                        <option>NMCM</option>
                        <option>NMCS</option>
                        <option>DEPOT</option>
                        <option>NRT</option>
                    </select>
            </div>
            <div class="col-md-6">
                <label for="reasonPMC"></label>
                <input type="text" class="form-control" id="reasonPMC" placeholder="REASON FOR PMC">
            </div>
        </div>
        <div class="form-row sectionBorder">
            <legend>AIRCRAFT FLIGHT INFORMATION</legend>
            <div class="form-group col-md-2">
                <label for="hobbsStart">HOBBS START</label>
                <input type="text" class="form-control" id="hobbsStart" placeholder="HRS">
            </div>
            <div class="form-group col-md-2">
                <label for="hobbsEnd">HOBBS END</label>
                <input type="text" class="form-control" id="hobbsEnd" placeholder="HRS">
            </div>
            <div class="form-group col-md-2">
                <label for="landings">LANDINGS</label>
                <input type="text" class="form-control" id="Landings" placeholder="">
            </div>
            <div class="form-group col-md-2">
                <label for="engineCycles">ENGINE CYCLES</label>
                <input type="text" class="form-control" id="engineCycles" placeholder="">
            </div>
            <div class="form-group col-md-2">
                <label for="fuelLoad">FUEL LOAD</label>
                <input type="text" class="form-control" id="fuelLoad" placeholder="">
            </div>
            <div class="form-group col-md-2">
                <label for="configuration">CONFIGURATION</label>
                <input type="text" class="form-control" id="configuration" placeholder="TT / ISR / AW">
            </div>
            <div class="form-row">
                    <small class="form-text text-muted">* Hobbs End Time will be entered on the new AFG-13 for the next flight mission/day in the HOBBS Start Block<br>** The last Aircraft Status Symbol will be transcribed over to the new AFG-13</small>
            </div>
        </div>
        <div class="sectionBorder">
            <legend>PRE-FLIGHT / POST-FLIGHT INSPECTIONS</legend>
            <div class="form-row">
                <div class="form-group col-md-3">
                    <label>TYPE OF INSPECTION</label>
                </div>
                <div class="form-group col-md-3">
                    <label>COMPLETED BY (PID)</label>
                </div>
                <div class="form-group col-md-6">
                    <label>DATE AND TIME COMPLETED</label>
                </div>
            </div>
            <div class="form-group form-row">
                <label for="precompleteBy" class="col-md-3 col-form-label">PRE-FLIGHT INSPECTION</label>
                <div class="col-md-3">
                    <input type="text" class="form-control" id="precompleteBy" placeholder="PID">
                </div>
                <div class="col-md-6">
                    <input type="datetime-local" class="form-control" id="datepreCompleted">
                </div>
            </div>
            <div class="form-group form-row">
                <label for="postcompleteBy" class="col-md-3 col-form-label">POST-FLIGHT INSPECTION</label>
                <div class="col-md-3">
                    <input type="text" class="form-control" id="postcompleteBy" placeholder="PID">
                </div>
                <div class="col-md-6">
                    <input type="datetime-local" class="form-control" id="datepostCompleted">
                </div>
            </div>
        </div>
        <div class="sectionBorder">
            <legend>SCHEDULED MAINTENANCE</legend>
            <div class="form-group form-row">
                <label for="nextInsp" class="col-md-3 col-form-label">NEXT SCHEDULED INSPECTION</label>
                <div class="col-md-3">
                    <input type="text" class="form-control" id="nextInsp">
                </div>
                <label for="remainingInsp" class="col-md-3 col-form-label">TIME REMAINING UNTIL INSPECTION</label>
                <div class="col-md-3">
                    <input type="text" class="form-control" id="remainingInsp">
                </div>
            </div>
        </div>
        <div class="form-row form-group">
          <input type="submit" class="form-control" value="Submit" name="submit" id="submit">
        </div>
    </form>

PHP:     

$pdo_dsn='mysql:dbname=mydbname;host=localhost';
$pdo_user='user';
$pdo_password='password';

try {

$conn = new PDO($pdo_dsn, $pdo_user, $pdo_password);

$conn->setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$qry=$conn->prepare('INSERT INTO pc12_status (aircraft_model, location, tail_no, flight_date, name_of_ce_mech, acft_status_1, acft_status_2,
acft_status_3, acft_status_4, acft_status_5, acft_status_6, pmc_reason, hobbs_start, hobbs_end, landings, engine_cycles, fuel_load, configuration, pre_flight_by_pid, pre_flight_date_time, post_flight_by_pid, post_flight_date_time, next_insp_due, time_remain_insp)
VALUES (:aircraftModel, :aircraftLocation, :aircraftTail, :dateFlight, :mechName, :aircraftStatus1, :aircraftStatus2, :aircraftStatus3,
:aircraftStatus4, :aircraftStatus5, :aircraftStatus6, :reasonPMC, :hobbsStart, :hobbsEnd, :landings, :engineCycles,
:fuelLoad, :configuration, :precompleteBy, :datepreCompleted, :postcompleteBy, :datepostCompleted, :nextInsp, :remainingInsp)');

$qry->execute(array(":aircraftModel", $aircraft_model));
$qry->execute(array(":aircraftLocation", $location));
$qry->execute(array(":aircraftTail", $tail_no));
$qry->execute(array(":dateFlight", $flight_date));
$qry->execute(array(":mechName", $name_of_ce_mech));
$qry->execute(array(":aircraftStatus1", $aircraft_status_1));
$qry->execute(array(":aircraftStatus2", $aircraft_status_2));
$qry->execute(array(":aircraftStatus3", $aircraft_status_3));
$qry->execute(array(":aircraftStatus4", $aircraft_status_4));
$qry->execute(array(":aircraftStatus5", $aircraft_status_5));
$qry->execute(array(":aircraftStatus6", $aircraft_status_6));
$qry->execute(array(":reasonPMC", $pmc_reason));
$qry->execute(array(":hobbsStart", $hobbs_start));
$qry->execute(array(":hobbsEnd", $hobbs_end));
$qry->execute(array(":landings", $landings));
$qry->execute(array(":engineCycles", $engine_cycles));
$qry->execute(array(":fuelLoad", $fuel_load));
$qry->execute(array(":configuration", $configuration));
$qry->execute(array(":precompleteBy", $pre_flight_by_pid));
$qry->execute(array(":datepreCompleted", $pre_flight_date_time));
$qry->execute(array(":postcompleteBy", $post_flight_by_pid));
$qry->execute(array(":datepostCompleted", $post_flight_date_time));
$qry->execute(array(":nextInsp", $next_insp_due));
$qry->execute(array(":remainingInsp", $time_remain_insp));

$stmt->execute();

1 个答案:

答案 0 :(得分:0)

您收到错误消息,因为您没有绑定与查询中的参数标记相同数量的值。您的代码仅为每次执行查询绑定一个值。您需要做的是使用PDOStatement::bindParam分别绑定值,然后执行查询:

$qry->bindParam(":aircraftModel", $aircraft_model);
...
$qry->bindParam(":remainingInsp", $time_remain_insp);
$qry->execute();

您也可以使用PDOStatement::bindValue - 请参阅PHP手册,了解两者之间的区别。

或者,您可以将所有值传递给执行调用:

$qry->execute(array(":aircraftModel" => $aircraft_model,
":aircraftLocation" => $location, ..., ":remainingInsp" => $time_remain_insp))

请注意,数组的格式与您使用的格式略有不同。再次查看manual ...