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.

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

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录