简体   繁体   English

PHP MySQL从下拉菜单插入

[英]PHP MySQL Insert from drop down menu

Just a quick question. 只是一个简单的问题。 I am sure it is not as complicated as I think. 我确信它并不像我想的那么复杂。 I have a page that has 4 drop down menus. 我有一个包含4个下拉菜单的页面。 These are all populated from a database which is MySQL. 这些都是从MySQL数据库中填充的。 Upon a submission I would like these options to be inserted into a different table. 提交后,我希望将这些选项插入另一个表中。 For the life of me I can no figure this out. 对于我的一生,我无法弄清楚。 Please if you could just let me know a bit of code that would achieve this it would be greatly appreciated. 请您让我知道一些实现此目标的代码,将不胜感激。 I have been attempting this for a day or so now. 我已经尝试了大约一天了。 Thanks in advance for the help. 先谢谢您的帮助。 Also note these are just snippets of code. 另请注意,这些只是代码片段。 Some might not have closing tags etc. 有些可能没有关闭标签等。

PHP CODE: PHP代码:

    <?php

//connecting to DB
$myServer   = "";
$myUsername = "";
$myPassword = "";
$myDatabase = "";

$myQuery1   = "SELECT DispatchUrgency.DispatchUrgencyID, DispatchUrgency.DispatchUrgencyName FROM DispatchUrgency ORDER BY DispatchUrgency.DispatchUrgencyName";
$myQuery2   = "SELECT DispatchStatus.DispatchStatusID, DispatchStatus.DispatchStatusName FROM DispatchStatus ORDER BY DispatchStatus.DispatchStatusID";
$myQuery3   = "SELECT Installer.InstallerID, CONCAT_WS(' ', PackingSlipLocation.ContactName, Installer.InstallerRegion, PackingSlipLocation.ContactPhone) as expr1 FROM PackingSlipLocation INNER JOIN Installer ON PackingSlipLocation.PackingSlipLocationID=Installer.InstallerPackingSlipLocationID";
$myQuery4   = "SELECT Location.LocationID, CONCAT_WS(' ', SystemName, id1poc, sitenamelocation, siteaddress1) as expr2 FROM Location";
$myQuery5   = "SELECT DispatchUrgency.DispatchUrgencyID FROM DispatchUrgency WHERE DispatchUrgencyName = '".$_POST['element_11']."'";

$myConnection = mysql_connect($myServer,$myUsername,$myPassword);
if (!$myConnection){
   die('Conncetion Failed:' . mysql_error()  );
}
@mysql_select_db($myDatabase) or die("Unable to select database");


$result1 = mysql_query($myQuery1) or die (mysql_error());
$result2 = mysql_query($myQuery2) or die (mysql_error());
$result3 = mysql_query($myQuery3) or die (mysql_error());
$result4 = mysql_query($myQuery4) or die (mysql_error());
$options = "";

if(mysql_num_rows($result1)){ 
$select1= '<select name="element_11" class="element select large" id="element_11">';   
while($rs=mysql_fetch_array($result1)){ 
      $select1.='<option value="'.$rs['DispatchUrgencyID'].'">'.$rs['DispatchUrgencyName'].'</option>'; 
  } 
} 
$select1.='</select>'; 


if(mysql_num_rows($result2)){ 
$select2= '<select class="element select large" id="element_12" name="element_12">';   
while($rs=mysql_fetch_array($result2)){ 
      $select2.='<option value="'.$rs['DispatchStatusID'].'">'.$rs['DispatchStatusName'].'</option>'; 
  } 
} 
$select2.='</select>';


if(mysql_num_rows($result3)){ 
$select3= '<select class="element select large" id="element_13" name="element_13">';   
while($rs=mysql_fetch_array($result3)){ 
      $select3.='<option value="'.$rs['Installer.InstallerID'].'">'.$rs['expr1'].'</option>'; 
  } 
} 
$select3.='</select>';


if(mysql_num_rows($result4)){ 
$select4= '<select class="element select large" id="element_14" name="element_14">';   
while($rs=mysql_fetch_array($result4)){ 
      $select4.='<option value="'.$rs['Installer.InstallerID'].'">'.$rs['expr2'].'</option>'; 
  } 
} 
$select4.='</select>';

?>

HTML FORM: HTML格式:

<form id="form_631187" class="appnitro"  method="post" action="">
        <div class="form_description">
            <h2>Dispatch Form</h2>
        </div>                      


    <ul>

        <li id="li_1" >
        <label class="description" for="element_1">Dispatch ID </label>
        <div>
            <input id="element_1" name="element_1" class="element text medium" type="text" maxlength="255" value=""/> 
        </div> 
        </li>       <li id="li_11" >
        <label class="description" for="element_11">Urgency</label>
        <div>


        <?php echo $select1; ?>

        </div> 
        </li>       
        <li id="li_12" >

        <label class="description" for="element_12">Status</label>

        <div>

        <?php echo $select2; ?>

        </div> 

        </li>       
        <li id="li_13" >

        <label class="description" for="element_13">Installers</label>

        <div>   

        <?php echo $select3; ?>

        </div> 

        </li>   
        <li id="li_14" >

        <label class="description" for="element_14">Location </label>

        <div>

        <?php echo $select4; ?>

        </div> 

        </li>   

2 things I noticed: 我注意到的2件事:

<form id="form_631187" class="appnitro"  method="post" action="">

has no action parameter. 没有动作参数。 You need to put something like: 您需要输入以下内容:

<form id="form_631187" class="appnitro"  method="post" action="form_handler.php">

Then you need to create form_handler.php and get the forms contents in the $_POST array. 然后,您需要创建form_handler.php并在$ _POST数组中获取表单内容。 On this page that you create you need to do the update/insert to the table that holds the values in the form. 在创建的此页面上,您需要对包含表单中值的表进行更新/插入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM