我正在將數據填充到Assembly表格的下拉列表中(表格中的值由用戶本身使用php添加)。現在我想在零件表中使用Assembly_Name。要從此下拉菜單中選擇值並需要插入到Part表的Assembly_Name列中。我無法選擇下拉值並將其插入到零件表中。從數據庫填充下拉列表並將該值插入到另一個數據庫中
Part.php
<html>
<body>
<form action="insert_part.php" method="post">
<!--Assembly_Id: <input type="text" name="Assembly_Id">-->
<?PHP
// Connect to your database ** EDIT THIS **
mysql_connect("localhost","root","abc"); // (host, username, password)
// Specify database ** EDIT THIS **
mysql_select_db("test") or die("Unable to select database"); //select db
$result = mysql_query("select assembly_id,assembly_name from assembly ORDER BY Assembly_Id");
echo '<select name="assembly_name"><OPTION>';
echo "Select an option</OPTION>";
while ($row = mysql_fetch_array($result)){
$assembly_name= $row["assembly_name"];
echo "<OPTION value=\"$assembly_name\">$assembly_name</OPTION>";
}
echo '</SELECT>';
?>
Part_name: <input type="text" name="Part_name">
<input type="submit">
</form>
<hr><hr>
<?php
$con = mysql_connect("localhost","abc");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$result = mysql_query("SELECT * FROM Part ORDER BY Part_Id");
echo "<table border='1'>
<tr>
<th>Assembly Name</th>
<th>Part Id</th>
<th>Part Name</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Assembly_Name'] ."</td>";
echo "<td>" . $row['Part_Id'] . "</td>";
echo "<td>" . $row['Part_Name'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</body>
</html>
insert_part.php
<?php
$con = mysql_connect("localhost","abc");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$assembly_name = isset($_POST['assembly_name'])
$sql="INSERT INTO Part (assembly_name,Part_Id, Part_Name) VALUES ('$_POST[assembly_name]','$_POST[Part_Id]','$_POST[Part_name]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
header("Location:part.php");
exit;
mysql_close($con);
?>
在提交的價值,我收到以下錯誤:
Parse error: syntax error, unexpected T_VARIABLE in C:\wamp\www\insert_part.php on line 10
[**請不要在新代碼**中使用'mysql_ *'函數](http://bit.ly/phpmsql),它們不再被維護[並已正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。請參閱[**紅框**](http://j.mp/Te9zIL)?瞭解[*已準備好的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/mysqli) - [這篇文章](http://j.mp/QEx8IB)可以幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 – Lion 2013-02-12 14:57:04