1

3 つのテーブルを更新して 2 に挿入しようとしています。問題は、エラーが発生し、いくつかのテーブルを更新して挿入することです。これを配置するより良い方法があると考えています。素敵なインプレ教えてください。申し訳ありませんが、これは恐ろしいコーディングです。:(

    <?php 

    require 'includes/db_connect.php';

    if ( !empty($_POST)) {
        // keep track validation errors
        $vehiclenumError = null;
        $chauffeuridError = null;

        // keep track post values
        $alias = $_POST['alias'];
        $vehiclenum = $_POST['vehiclenum'];
        $chauffeurid = $_POST['chauffeurid'];

        // validate input
        $valid = true;

        if (empty($vehiclenum)) {
            $vehiclenumError = 'Please select a Vehicle #';
            $valid = false;
        }

        if (empty($chauffeurid)) {
            $chauffeuridError = 'Please select a Chauffeur #';
            $valid = false;
        }   

        // insert data
        if ($valid) {
            $pdo = Database::connect();
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql = "INSERT INTO unit_logs (alias,vehiclenum,chauffeurid,status) values (?, ?, ?, 1)";
            $sql1 = "INSERT INTO active_units (alias,vehiclenum,chauffeurid,status) values (?, ?, ?, 1)";           
            $sql2 = "UPDATE ipads SET status='1' WHERE alias=$alias";
            $sql3 = "UPDATE vehicles SET active='1'  WHERE vehiclenum=$vehiclenum";
            $sql4 = "UPDATE chauffeurs SET active='1' WHERE chauffeurid=$chauffeurid";
            $q = $pdo->prepare($sql);
            $l = $pdo->prepare($sql1);
            $x = $pdo->prepare($sql2);
            $z = $pdo->prepare($sql3);
            $h = $pdo->prepare($sql4);
            $q->execute(array($alias,$vehiclenum,$chauffeurid));        
            $l->execute(array($alias,$vehiclenum,$chauffeurid));
            $x->execute(array($alias));
            $z->execute(array($vehiclenum));
            $h->execute(array($chaffeurid));
            Database::disconnect();
            header("Location: testing.php");
        }
    }
    ?>

<code>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <link   href="css/bootstrap.min.css" rel="stylesheet">
    <script src="js/bootstrap.min.js"></script>
</head>

<body>
    <div class="container">

                <div class="span10 offset1">
                    <div class="row">
                        <h3>Tablet Check Out</h3>
                    </div>
                    <?php
                    mysql_connect('localhost', 'allen', 'w0wr0cks');
                    mysql_select_db('wcldb');
                    $sql = "SELECT alias FROM ipads WHERE status = 0 ORDER BY alias ASC";
                    $result = mysql_query($sql);
                    ?>
                    <form class="form-horizontal" action="checkout.php" method="post">
                        <div class="control-group">
                        <label class="control-label">Tablet Alias</label>
                        <div class="controls">
                    <select name="alias">
                    <?php 
                    while ($row = mysql_fetch_array($result)) {
                        echo "<option value='" . $row['alias'] ."'>" . $row['alias'] ."</option>";
                    } ?>
                        </select>
                        </div>
                    </div>
                    <?php
                    mysql_connect('localhost', 'allen', 'w0wr0cks');
                    mysql_select_db('wcldb');
                    $sql = "SELECT vehiclenum FROM vehicles WHERE ownertype = 'JKS' AND active = '0' ORDER BY vehiclenum ASC";
                    $result = mysql_query($sql);
                    ?>
                      <div class="control-group <?php echo !empty($vehiclenumError)?'error':'';?>">
                        <label class="control-label">Vehicle #</label>
                        <div class="controls">
                     <select name="vehiclenum">
                            <?php 
                    while ($row = mysql_fetch_array($result)) {
                        echo "<option value='" . $row['vehiclenum'] ."'>" . $row['vehiclenum'] ."</option>";
                    }
                        echo '</select>';
                    ?>
                        </div>
                      </div>
                      <?php
                    mysql_connect('localhost', 'allen', 'w0wr0cks');
                    mysql_select_db('wcldb');
                    $sql = "SELECT chauffeurid FROM chauffeurs WHERE ownertype = 'JKS' AND active='0' ORDER BY chauffeurid ASC";
                    $result = mysql_query($sql);
                    ?>
                      <div class="control-group <?php echo !empty($chauffeuridError)?'error':'';?>">
                        <label class="control-label">Driver #</label>
                        <div class="controls">
                          <?php 
                        echo "<select name='chauffeurid'>";
                    while ($row = mysql_fetch_array($result)) {
                        echo "<option value='" . $row['chauffeurid'] ."'>" . $row['chauffeurid'] ."</option>";
                    }
                        echo '</select>';
                    ?>  
                        </div>
                      </div>


                      <div class="form-actions">
                          <button type="submit" class="btn btn-success">Create</button>
                          <a class="btn" href="testing.php">Back</a>
                        </div>
                    </form>
                </div>

    </div> <!-- /container -->
  </body>
</html>

4

0 に答える 0