我想要從一個表中取出多行數據並根據用戶選擇,只將所選行存儲到另一個表中。從一個表中獲取多行並在php中存儲到另一個表Mysql
這段代碼的問題是來自'FLIGHTS'表的數據正在從數據庫中準確地獲取,但是當我試圖將它存儲到另一個'bookFlight'表中時,它僅爲所有列存儲NULL值。需要幫助!
<?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>";
?>
您需要通過表單傳遞/發佈值。 –
**警告**:如果您只是學習PHP,請不要學習過時的['mysql_query'](http://php.net/manual/en/function.mysql-query.php)界面。這很糟糕,並且已經在PHP 7中被刪除了。像[PDO不是很難學的東西](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-php-pdo- for-database-access /)以及[PHP The Right Way](http://www.phptherightway.com/)等指南有助於解釋最佳實踐。確保**你的用戶參數[妥善轉義](http://bobby-tables.com/php),否則你將以嚴重的[SQL注入漏洞](http://bobby-tables.com/ )。 – tadman
在函數調用之外定義查詢也是您需要打破的習慣。將錯誤的字符串作爲參數傳遞是非常容易的,特別是當你在處理諸如'$ sql1','$ sql2'之類的東西時。將查詢字符串直接提供給函數完全消除了任何歧義。 – tadman