0

数据库地图:

Carriers "table1": carriername, carrierid, ect... Post data
carrierinfo "table2": carriername, id, contact, ect... carrier list

我正在使用mysql_queryfromtable2作为我的carriername下拉菜单。使用它,我可以将运营商名称发布到table1. 我正在尝试使用相同的下拉菜单发布相关id的 selected carriername。所以简而言之,我需要一个只显示运营商名称但同时 从 table2查询carriername&的下拉菜单。id发布表格后,我需要将carriername&发布carrierid到 table1。下面是我正在使用的下拉菜单。请让我知道如何连接这些点并添加所需的内容。如果它是可能的。

 <?php

if (isset($_POST["submit"]) && $_POST["submit"] == "Submit")
{
    for ($count = 1; $count <= 9; $count++)
    {
        $fields[$count] = "";
        if (isset($_POST["field" . $count . ""]))
        {
            $fields[$count] = trim($_POST["field" . $count . ""]);
            //echo $fields[$count] . "<br />";
        }
    }

    $con = mysql_connect("", "", "");
    mysql_select_db("", $con);

    $carrierid = mysql_real_escape_string($_POST['carrierid']);
    $fromzip = mysql_real_escape_string($_POST['fromzip']);
    $tozip = mysql_real_escape_string($_POST['tozip']);
    $typeofequipment = mysql_real_escape_string($_POST['typeofequipment']);
    $weight = mysql_real_escape_string($_POST['weight']);
    $length = mysql_real_escape_string($_POST['length']);
    $paymentamount = mysql_real_escape_string($_POST['paymentamount']);
    $contactperson = mysql_real_escape_string($_POST['contactperson']);
    $loadtype = mysql_real_escape_string($_POST['loadtype']);
    $date = mysql_real_escape_string($_POST['date']);

    $insert = "INSERT INTO Carriers (`carriername` ,`carrierid`, `fromzip` ,`tozip` ,`typeofequipment` ,`weight` ,`length` ,`paymentamount` ,`contactperson` ,`loadtype` ,`date`) 
    SELECT carriername ,'$carrierid' ,'$fromzip' ,'$tozip' ,'$typeofequipment' ,'$weight' ,'$length' ,'$paymentamount' ,'$contactperson' ,'$loadtype', NOW()) FROM carrierinfo WHERE id = '$carrierid'";

    mysql_query($insert) or die(mysql_error());

    $select = "SELECT `carriername` ,`fromzip` ,`tozip` ,`typeofequipment` ,`weight` ,`length` ,`paymentamount` ,`contactperson` ,`loadtype` FROM `Carriers` ORDER BY `date` DESC;";
    $result = mysql_query($select) or die(mysql_error());

}
?>
<style ="text-align: center; margin-left: auto; margin-right: auto;"></style>
</head>
<body>
<input type="button" onclick="window.location.href='search.php';" value="Search Board" /><div
 style="border: 2px solid rgb(0, 0, 0); margin: 16px 20px 20px; width: 400px; background-color: rgb(236, 233, 216); text-align: center; float: left;">
<form action="" method="post";">
  <div
  style="margin: 8px auto auto; width: 300px; font-family: arial; text-align: left;"><br>
  <table style="font-weight: normal; width: 100%; font-size: 12px;"
 border="1" bordercolor="#929087" cellpadding="6" cellspacing="0">
   <table
 style="font-weight: normal; width: 100%; text-align: right; font-size: 12px;"
 border="1" bordercolor="#929087" cellpadding="6" cellspacing="0">
    <tbody>
    <tr>
    <td style="width: 10%;">Carrier:</td><td>
 <?php  

$con = mysql_connect("", "", "");
    mysql_select_db("", $con);

$query=("SELECT * FROM carrierinfo"); 

$result=mysql_query($query) or die ("Unable to Make the Query:" . mysql_error() ); 
echo "<select name='carrierid'>";
while($row=mysql_fetch_array($result)){  
echo "<OPTION VALUE='".$row['id']."'>".$row['carriername']."</OPTION>";
}
echo "</select>"; 

?>
              </td>
        </tr>
        <tr>
              <td style="width: 10%;">Load Type:</td><td>
              <select name="loadtype">
              <option></option>
              <option value="TL">Truck Load</option>
              <option value="Partial">Partial</option>
              </select>
              </td>
        </tr>
        <tr>
              <td style="width: 35%;">Pick Zip:</td><td> <input id="fromzip" name="fromzip" maxlength="50"
 style="width: 100%;" type="text">
        </tr>
        <tr>
              <td style="width: 35%;">Drop Zip:</td><td> <input id="tozip" name="tozip" maxlength="50"
 style="width: 100%;" type="text">
        </tr>
        <tr>
              <td style="width: 35%;">Weight:</td><td> <input id="weight" name="weight" maxlength="50"
 style="width: 100%;" type="text">
        </tr>
        <tr>
              <td style="width: 35%;">Length:</td><td> <input id="length" name="length" maxlength="50"
 style="width: 100%;" type="text">
        </tr>
        <tr>
        <td style="width: 10%;">Equip:</td><td>
              <select name="typeofequipment">
              <option></option>
              <option value="AUTO">Auto Carrier</option>
              <option value="DD">Double Drop</option>
              <option value="F">Flatbed</option>
              <option value="LB">Lowboy</option>
              <option value="Rail">Rail</option>
              <option value="Ref">Reefer</option>
              <option value="RGN">Removable Goose Neck</option>
              <option value="SD">Step Deck</option>
              <option value="TANK">Tanker (Food, liquid, etc.)</option>
              <option value="V">Van</option>
              </select>
              </td>
              </tr>
              <tr>
              <td style="width: 35%;">Contact:</td><td> <input id="contactperson" name="contactperson" maxlength="50"
 style="width: 100%;" type="text">
        </tr>
        <tr>
              <td style="width: 35%;">Rate:</td><td> <input id="paymentamount" name="paymentamount" maxlength="50"
 style="width: 100%;" type="text">
        </tr>
          </tbody>
  </table>
  <p style="text-align: center;"><input name="submit" value="Submit"
 class="submit" type="submit"><input type="button" onclick="window.location.href='newcarrier.php';" value="Add Carrier" /></p>
  </div>
</form>
</div>
<p style="margin-bottom: -20px;">&nbsp;</p>
</body>         
4

1 回答 1

0

使用连接:

SELECT t1.carrierid, t2.carriername, t2.id
FROM table1 t1 JOIN table2 t2 USING (carriername)

连接表应该是您学习 SQL 时首先要学习的内容之一。

更新:

没关系,您填充下拉列表的原始查询是正确的。填充选择时,您应该执行以下操作:

echo "<select name='carrierid'>";
while($row=mysql_fetch_array($result)){  
  echo "<OPTION VALUE='".$row['id']."'>".$row['carriername']."</OPTION>";
}
echo "</select>";

发布数据时,去掉这一行:

$carriername = mysql_real_escape_string($_POST['carriername']);

并将插入查询更改为:

$insert = "INSERT INTO Loads (`carriername` ,`carrierid`, `fromzip` ,`tozip` ,`typeofequipment` ,`weight` ,`length` ,`paymentamount` ,`contactperson` ,`loadtype` ,`date`) 
  SELECT carriername ,'$carrierid' ,'$fromzip' ,'$tozip' ,'$typeofequipment' ,'$weight' ,'$length' ,'$paymentamount' ,'$contactperson' ,'$loadtype', NOW())
  FROM Carriers
  WHERE id = '$carrierid'";

对表单的更改使其显示运营商名称,但在发布数据中返回 ID。然后新的 INSERT 查询使用该 ID 从表 2 中获取运营商名称,并在插入表 1 时将其与所有其他发布数据结合起来。

于 2012-11-09T01:51:55.417 回答