使用外键php将数据插入表中

时间:2017-12-13 22:18:06

标签: php mysql

我有一个有1个主键和2个外键的表,我不认为我正确插入它们。当我尝试在phpmyadmin上做它我有选择dropdown list这就是为什么我不认为我可以使用下面的插入代码?

我试图只插入seat_id,但没有用。然后我试图获取信息并插入它,这也没有用。

它说它提交给php,但实际上并没有。

这是输出代码

    You are currently accessing database; your username is: admin.
    admin@airways.com
    5A
    5B
    5C
    successfully taken seat.
    Back to seating.

relations table

    <?php
    error_reporting(E_ALL);
    ini_set('display_errors',1);
    include_once 'includes/db_connect.php';
    include_once 'includes/functions.php';
    sec_session_start();


    if (login_check($mysqli) == true) {
    $logged = 'accessing';
} else {
    $logged = 'not accessing';
}
                $username = $_SESSION['username'];
                echo  '<p>You are currently '.$logged. ' database; your username is: '.$username. '.</p><br>';

                $getemail = mysqli_query($mysqli, "SELECT email FROM members WHERE username = '$username' ");
                $email = '';
                while($gear = mysqli_fetch_array( $getemail )) {
                    echo $gear['email'];
                    $email = $gear['email'];
                } 

                echo $gear."<br>";


    if(isset($_POST['seatme'])){
                print 'initiate POST<br>';

        if(!empty($_POST['flight'])){
            print 'SEATME exists <br>';

            foreach($_POST['flight'] as $key=>$selected){
                echo $selected."<br>";

if ( $result = mysqli_query($mysqli, "INSERT INTO seating (flight_id, email, seat_id) VALUES ('1', '".$username."', '".$selected."')",  MYSQLI_USE_RESULT))
    {
    print 'Insert OK<br>';
    }else{
    print 'something wrong here<br>';
    }

?> 
        You are currently accessing database; your username is: admin.


admin@airways.com
initiate POST
SEATME exists 
9A
something wrong here
Array
(
    [flight] => Array
        (
            [0] => 9A
            [1] => 9B
            [2] => 9C
        )

    [seatme] => Submit
)

Here is a picture of what that looks like

                    <div class="plane">
  <div class="cockpit" >
    <h1>Please select a seat</h1>
  </div>
  <div class="exit exit--front fuselage text-center">
  </div>
  <form action="flightselect.php" method="post" name="form"> 
  <ol class="cabin fuselage">
    <li class="row row--1" style="padding-left: 15%;">
      <ol class="seats" type="A">
        <li class="seat">
          <input type="checkbox" name="flight[]" id="1A" value="1A" />
          <label for="1A">1A</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="1B" value="1B" />
          <label for="1B">1B</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="1C" value="1C" />
          <label for="1C">1C</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="1D" value="1D" />
          <label for="1D">1D</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="1E" value="1E" />
          <label for="1E">1E</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="1F" value="1F" />
          <label for="1F">1F</label>
        </li>
      </ol>
    </li>
    <li class="row row--2" style="padding-left: 15%;">
      <ol class="seats" type="A">
        <li class="seat">
          <input type="checkbox" name="flight[]" id="2A" value="2A" />
          <label for="2A">2A</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="2B" value="2B" />
          <label for="2B">2B</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="2C" value="2C" />
          <label for="2C">2C</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="2D" value="2D" />
          <label for="2D">2D</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="2E" value="2E" />
          <label for="2E">2E</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="2F" value="2F" />
          <label for="2F">2F</label>
        </li>
      </ol>
    </li>
    <li class="row row--3" style="padding-left: 15%;">
      <ol class="seats" type="A">
        <li class="seat">
          <input type="checkbox" name="flight[]" id="3A" value="3A" />
          <label for="3A">3A</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="3B" value="3B" />
          <label for="3B">3B</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="3C" value="3C" />
          <label for="3C">3C</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="3D" value="3D" />
          <label for="3D">3D</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="3E" value="3E" />
          <label for="3E">3E</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="3F" value="3F" />
          <label for="3F">3F</label>
        </li>
      </ol>
    </li>
    <li class="row row--4" style="padding-left: 15%;">
      <ol class="seats" type="A">
        <li class="seat">
          <input type="checkbox" name="flight[]" id="4A" value="4A" />
          <label for="4A">4A</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="4B" value="4B" />
          <label for="4B">4B</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="4C" value="4C" />
          <label for="4C">4C</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="4D" value="4D" />
          <label for="4D">4D</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="4E" value="4E" />
          <label for="4E">4E</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="4F" value="4F" />
          <label for="4F">4F</label>
        </li>
      </ol>
    </li>
    <li class="row row--5" style="padding-left: 15%;">
      <ol class="seats" type="A">
        <li class="seat">
          <input type="checkbox" name="flight[]" id="5A" value="5A" />
          <label for="5A">5A</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="5B" value="5B" />
          <label for="5B">5B</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="5C" value="5C" />
          <label for="5C">5C</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="5D" value="5D" />
          <label for="5D">5D</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="5E" value="5E" />
          <label for="5E">5E</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="5F" value="5F" />
          <label for="5F">5F</label>
        </li>
      </ol>
    </li>
    <li class="row row--6" style="padding-left: 15%;">
      <ol class="seats" type="A">
        <li class="seat">
          <input type="checkbox" name="flight[]" id="6A" value="6A" />
          <label for="6A">6A</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="6B" value="6B" />
          <label for="6B">6B</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="6C" value="6C" />
          <label for="6C">6C</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="6D" value="6D" />
          <label for="6D">6D</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="6E" value="6E" />
          <label for="6E">6E</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="6F" value="6F" />
          <label for="6F">6F</label>
        </li>
      </ol>
    </li>
    <li class="row row--7" style="padding-left: 15%;">
      <ol class="seats" type="A">
        <li class="seat">
          <input type="checkbox" name="flight[]" id="7A" value="7A" />
          <label for="7A">7A</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="7B" value="7B" />
          <label for="7B">7B</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="7C" value="7C" />
          <label for="7C">7C</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="7D" value="7D" />
          <label for="7D">7D</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="7E" value="7E" />
          <label for="7E">7E</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="7F" value="7F" />
          <label for="7F">7F</label>
        </li>
      </ol>
    </li>
    <li class="row row--8" style="padding-left: 15%;">
      <ol class="seats" type="A">
        <li class="seat">
          <input type="checkbox" name="flight[]" id="8A" value="8A" />
          <label for="8A">8A</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="8B" value="8B" />
          <label for="8B">8B</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="8C" value="8C" />
          <label for="8C">8C</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="8D" value="8D" />
          <label for="8D">8D</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="8E" value="8E" />
          <label for="8E">8E</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="8F" value="8F" />
          <label for="8F">8F</label>
        </li>
      </ol>
    </li>
    <li class="row row--9" style="padding-left: 15%;">
      <ol class="seats" type="A">
        <li class="seat">
          <input type="checkbox" name="flight[]" id="9A" value="9A" />
          <label for="9A">9A</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="9B" value="9B" />
          <label for="9B">9B</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="9C" value="9C" />
          <label for="9C">9C</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="9D" value="9D" />
          <label for="9D">9D</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="9E" value="9E" />
          <label for="9E">9E</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="9F" value="9F" />
          <label for="9F">9F</label>
        </li>
      </ol>
    </li>
    <li class="row row--10" style="padding-left: 4%";>
      <ol class="seats" type="A">
        <li class="seat">
          <input type="checkbox" name="flight[]" id="10A" value="10A" />
          <label for="10A">10A</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="10B" value="10B" />
          <label for="10B">10B</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="10C" value="10C" />
          <label for="10C">10C</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="10D" value="10D" />
          <label for="10D">10D</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="10E" value="10E" />
          <label for="10E">10E</label>
        </li>
        <li class="seat">
          <input type="checkbox" name="flight[]" id="10F" value="10F" />
          <label for="10F">10F</label>
        </li>
      </ol>
    </li>
  </ol>
  <div class="exit exit--back fuselage">    
  </div>
</div>
<input type="submit" name="seatme" value="Submit" style="font-weight: bold;">
</form>

1 个答案:

答案 0 :(得分:0)

我将使用此空间,因为我可以显示代码而不是发表评论。 我稍后会删除这个答案。尝试按照我的建议逐步调试一步一步。 否则它似乎应该适用于您的代码。

if(isset($_POST['seatme'])){
    print 'SEATME exists <br>;
    if(!empty($_POST['flight'])){
        print 'FLIGHT exists <br>';
            foreach($_POST['flight'] as $key=>$selected){
             //    echo $selected."<br>";
            $sql = "INSERT INTO seating (flight_id, email, seat_id) VALUES ('1', '".$username."', '".$selected."')";
            $resut=mysqli_query($mysqli, $sql) or die('ERROR is here: ' . mysqli_error($mysqli)); // it should work displaying error
            }

    }else{
        echo "<font color='red'>Please select seats .</font><br/>";
        }

echo "<font color='green'>successfully taken seat.";
echo "<br/><a href='user.php'>Back to seating.</a>";
    }else{
        echo "<font color='red'>Please select seats .</font><br/>";
        }