douhuang2282 2017-08-31 16:06
浏览 32
已采纳

从一个表中获取多行并将其存储到php Mysql中的另一个表

I want to fetch multiple row data from one table and on users selection, store only the chosen row into another table.

The problem with this code is that data from table 'FLIGHTS' is fetching accurately from database but when I am trying to store it into another table 'bookFlight' it is storing only NULL values for all columns. Want help!

<?php

$username = "root"; 
$password = ""; 
$hostname = "localhost"; 
$dbhandle = mysql_connect($hostname, $username, $password)  
            or die("Unable to connect to MySQL");
$selected = mysql_select_db("dbtest",$dbhandle)
            or die("Could not select dbtest");

session_start();

         ////// STORING DATA INTO TABLE 'bookFlight' ////////

 if(isset($_POST['Submit']))
{
         $company_name   = mysql_real_escape_string($_POST['company_name']);
         $flight_category= mysql_real_escape_string($_POST['flight_category']);
         $rates           = mysql_real_escape_string($_POST['rates']);
         $qry    = "INSERT INTO bookFlight"."(company_name,flight_category,rates)". 
                  "VALUES('$company_name','$flight_category','$rates')"; 

    $retval = mysql_query( $qry, $dbhandle );
    if(! $retval ) {
           die('<br><br> Could not enter data: ' . mysql_error());
        }
      else  {  echo "Entered data successfully
";   }               
}
              ////// FETCHING DATA FROM TABLE 'flights' ////////

$sql = "SELECT * FROM flights where type_id = 
                                      (SELECT type_id FROM tour WHERE city = 
                                     '{$_SESSION['destination_Address']}')";

$myData = mysql_query($sql,$dbhandle);
$num = mysql_num_rows($myData);

  echo "<table border=1>
  <tr>
  <th> COMPANY NAME :  </th>
  <th> FLIGHT CATEGORY : </th>
  <th> RATES :  </th>
  </tr>";

 for ($i=0; $i <$num; $i++)
{
$record = mysql_fetch_array($myData,MYSQL_ASSOC); 
echo "<form method=post>";
  echo "<tr>";
  echo "<td>" . $company_name[]= $record['company_name']      ."</td>";
  echo "<td>" . $flight_category[]= $record['flight_category']."</td>";
  echo "<td>" . $rates[]= $record['rates']                    ." </td>";
  echo "<td>" . "<input type='submit' name='Submit' value='SELECT'></td>";
  echo "</tr>";  
}
echo"</table>";
echo "</form>";

?>
  • 写回答

1条回答 默认 最新

  • dtg25862 2017-08-31 16:32
    关注

    That's because you are not sending anything except submit with your form, and that's why $_POST array has nothing except submit value.

    You need to pass the row id(I'm assuming id column in this case) along with the form data so that you could insert that particular row in bookFlight table.

    for ($i=0; $i <$num; $i++){
        $record = mysql_fetch_array($myData,MYSQL_ASSOC); 
        echo "<form action='?id=".$record['id']."' method='post'>";
        ...
    }
    

    And once the user hits the submit button, this is how you can INSERT the data.

    if(isset($_POST['Submit'])){
        $qry = "INSERT INTO bookFlight (company_name, flight_category, rates) SELECT company_name, flight_category, rates FROM flights WHERE id = '".$_GET['id']."'";
        $retval = mysql_query( $qry, $dbhandle );
        if(! $retval ) {
            die('<br><br> Could not enter data: ' . mysql_error());
        }else {  
            echo "Entered data successfully
    ";   
        }               
    }
    

    Sidenote: Don't use mysql_* functions, they are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use mysqli or pdo instead. And this is why you shouldn't use mysql_* functions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?