简体   繁体   中英

Fetch multiple rows from one table and Store to another table in 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\n";   }               
}
              ////// 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>";

?>

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\n";   
    }               
}

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM