在选择选项或单选按钮上切换2插入查询

时间:2015-12-06 19:13:03

标签: php html mysql

我想在下拉选择选项

上运行2个不同的INSERT查询

如果用户选择选项1 ,则会运行表1的插入查询 当用户选择选项2 时,将运行表2的另一个插入查询。

Rscript D:/r_code1.r 123 544        # put loop condition here
Rscript D:/r_code2.r 125 524        # put loop condition here
Rscript D:/r_code3.r 156 553        # put loop condition here
Rscript D:/r_code4.r 187 587        # put loop condition here

HTML是

 <?php 
   if(isset($_POST['submit_form']) && isset($_POST['listings']))  {
switch ($listings) {
          case 'GradeA':
           $stmt = "INSERT INTO Packing_listA (Country, Total_bales) VALUES ('$Country' , '$Total_bales')";
        break;
          case 'GradeB':
           $sql = "INSERT INTO Packing_listB (Country, Total_bales) VALUES ('$Country' , '$Total_bales')";
        break;
       }

     if ($mysqli->query($stmt) || $mysqli->query($sql) === TRUE) {
        $last_id = $mysqli->insert_id;
        $_SESSION['ct_id'] = $last_id;
         echo "<p>New record created successfully. Last inserted ID is:  '". $last_id."'</p>"; 
         echo "<script>setTimeout('delayer()', 1000)</script>"; 
      }    
     } ?>

但我对其他任何方法都持开放态度,例如Radio Buttons或If,Else条件或任何有效的方法 选择选项1时,仅运行关联的查询,反之亦然。

1 个答案:

答案 0 :(得分:0)

首先,我会选择这样的事情:

if (isset($_POST['submit_form']) && isset($_POST['listings'])) {
  $sql = "INSERT INTO "
    ."Packing_list".($_POST['listings'] == 'GradeA'? 'A' : 'B')
    ."(Country, Total_bales)"
    ."VALUES ('$Country' , '$Total_bales')";

  if ($mysqli->query($sql)) {
    $_SESSION['ct_id'] = $mysqli->insert_id;
    echo "<p>New record created successfully. Last inserted ID is:  '".$_SESSION['ct_id'].
    "'</p>";
    echo "<script>setTimeout('delayer()', 1000)</script>";
  }
} 

其次,我认为你可以制作一个更好的代码(在这一个奇怪的想法......)