我想从一个表和用户选择中获取多个行数据,只将选定的行存储到另一个表中。
此代码的问题是表'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>";
?>
答案 0 :(得分:0)
那是因为你没有向你的表单发送除submit
之外的任何内容,这就是为什么$_POST
数组除了submit
值之外什么都没有。
您需要传递行ID (我假设在这种情况下是 id 列)以及表单数据,以便您可以插入 < / em> bookFlight
表中的特定行。
for ($i=0; $i <$num; $i++){
$record = mysql_fetch_array($myData,MYSQL_ASSOC);
echo "<form action='?id=".$record['id']."' method='post'>";
...
}
一旦用户点击提交按钮,您就可以INSERT
数据。
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";
}
}
旁注:不使用mysql_*
函数,从PHP 5.5开始不推荐使用它们,在PHP 7.0中完全删除它们。请改用mysqli
或pdo
。 And this is why you shouldn't use mysql_*
functions。