-4

テーブル *order_management* に注文を追加するために使用される *book_order* ページがあり、order_id はそのテーブルで自動インクリメントされます。このページを送信した後、order_id を他のページ *book_order2* に渡して、同じ order_id の下に製品を追加します。そのために、order_id が自動インクリメントされない別の *order_management2* テーブルを作成しました。

私の要件は、book_order ページから book_order2 ページに order_id を渡したいということです。その変数は、追加し続けるまで覚えておく必要があります....新しい注文を追加したい場合は、book_order ページに移動します。 book_order2 ページを使用します。

book_order.php

 <div class="grid_4">
   <div class="da-panel">
   <div class="da-panel-header">
   <span class="da-panel-title">
   <img src="images/icons/color/wand.png" alt="" />
   <font face="Cambria" size="7" color="#009900"><b>Book Order</b></font>
   </span>

   </div>
   <div class="da-panel-toolbar top">
   <ul>

   <li><a href="main.php?page=view_orders"><div class="da-button blue large">View all Orders</div></a></li>

   </ul>
   </div>

   <div class="da-panel-content">



   <?php

   if(isset($_POST['submit']))
     {
       extract($_POST);

       $order_date=date("Y-m-d");

       $sql=mysql_query("select sku,quantity_in_stock,sold_quantity,crdate from stock_info where product_name = '$prod'");
       $array=mysql_fetch_array($sql);
       $sku = $array[0];
       $qis = $array[1];
       $sold_quan = $array[2];
       $crdate = $array[3];

       $sql2=mysql_query("INSERT INTO order_management(order_date,brand,product,price,customer_name,phone_number,email,address,quantity,channel,courier,order_status,sku)
    VALUES
    ('$order_date','$brand','$prod','$pri','$customername','$phonenumber', '$email','$address','$quantity','$channel','$courier','booked','$sku')");



       if($sql2)
         {
           echo "<div class='da-message success'>Successfully Booked Your Order</div>"; 
           ?>
           <script>

           var r = confirm("want to add more products?");
           if (r == true)
             {
               //x="You pressed OK!";
               window.location = "main.php?page=book_order2";
             }
           else
             {
               //x="You pressed Cancel!";
               window.location = "main.php";
             }

           </script>
           <?php
         }
       else 
         {
           die(mysql_error());
         }


       $quantity_left = $qis - $quantity;
       $sold_quan = $sold_quan + $quantity;

       $diff_in_days = (strtotime($order_date) - strtotime($crdate))/(60 * 60 * 24);
       $expctd_stock=round((7*$quantity_left)/$diff_in_days);
       //echo $expctd_stock;



       $sql3 =mysql_query("UPDATE stock_info SET quantity_in_stock = '$quantity_left',last_sold_date='$order_date', sold_quantity='$sold_quan', expected_stock='$expctd_stock' WHERE sku='$sku'");
       /*$sql3 = mysql_query("update order_management set sku='$sku' where order_date=''");*/

       $sql4 =mysql_query("select order_id from order_management where sku='$sku'");
       $idarray=mysql_fetch_array($sql4);
       $id = $idarray[0];
       //$_SESSION['id'] = $id;
     }
   ?>

   <form id="da-ex-validate1" class="da-form" method="post" action="">


   <div class="da-form-row">
   <label>Brand<span class="required">*</span></label>
   <div class="da-form-item small">
   <!--<input type="text" name="brand"  id="brand" class="required" value=""/>-->
   <select name="brand" id="brand" onChange="retrievedata(this.value)">
   <option value="">--- select brand ---</option>

   <?php
   $ord=mysql_query("select * from brand_info");

while($ord1=mysql_fetch_array($ord))
  {

    ?>  
    <option value="<?php echo $ord1['brand'];?>"><?php echo $ord1['brand'];?></option> 

    <?php

  }
?>   
</select>
</div>
</div>
<div class="da-form-row">
  <label>Product<span class="required">*</span></label>
  <div class="da-form-item small">
  <select name="prod" id="prod" onChange="retrievequantity(this.value)">
  <option value="">--- select product    ---</option>
  </select>



  </div>
  </div> 
  <div class="da-form-row">
  <label>Customer name<span class="required">*</span></label>
  <div class="da-form-item small">
  <input type="text" name="customername"  id="customername" class="required char" value=""/>
  </div>
  </div>
  <div class="da-form-row">
  <label>Phone Number<span class="required">*</span></label>
  <div class="da-form-item small">
  <input type="text" name="phonenumber"  id="phonenumber" class="required number" value=""/>
  </div>
  </div> 

  <div class="da-form-row">
  <label>Email<span class="required">*</span></label>
  <div class="da-form-item small">
  <input type="text" name="email"  id="email" class="required email" value=""/>
  </div>
  </div>
  <div class="da-form-row">
  <label>Address<span class="required">*</span></label>
  <div class="da-form-item small">
  <textarea name="address" id="address" class="required"></textarea>

  </div>
  </div>

  <div class="da-form-row">
  <label>Quantity<span class="required">*</span></label>
  <div class="da-form-item small">

  <select name="quantity" id="quantity">
  <option value=""> --- select Quantity--- </option>
  </select>

  </div>
  </div>

  <div class="da-form-row">
  <label>Total Price<span class="required">*</span></label>
  <div class="da-form-item small">
  <input type="text" name="pri" id="pri" class="required number" value=""/>
  </div>
  </div>


  <div class="da-form-row">
  <label>Courier<span class="required"></span></label>
  <div class="da-form-item small">
  <!--<input type="text" name="courier"  id="courier" class="required" value=""/>-->
  <select name="courier" id="courier">
  <option value=""> ---select courier --- </option>
  <?php
  $ord=mysql_query("select courier_name from courier_info");

while($ord1=mysql_fetch_array($ord))
  {

    ?>  
    <option value="<?php echo $ord1['courier_name'];?>"><?php echo $ord1['courier_name'];?></option> 

    <?php

  }
?>   
</select>

</div>
</div> 

<div class="da-form-row">
  <label>Channel<span class="required"></span></label>
  <div class="da-form-item small">
  <!--<input type="text" name="channel"  id="channel" class="required" value=""/>-->
  <select name="channel" id="channel">
  <option value=""> --- select channel ---</option>
  <?php
  $ord=mysql_query("select channel from channel_info");

while($ord1=mysql_fetch_array($ord))
  {

    ?>  
    <option value="<?php echo $ord1['channel'];?>"><?php echo $ord1['channel'];?></option> 

    <?php

  }
?>   
</select>
</div>
</div>
<div class="da-button-row">
  <input type="submit"  name="submit"  value="submit" class="da-button grey" />


  </div>

  </fieldset>
  </form>

  </div>
  <!-- End of .grid_4 --> </div>
  </div>

  <script>
  function retrievedata(data)
{
  var option_html = "";

  <?php
  $sql=mysql_query("SELECT distinct brand,product_name FROM stock_info");
  while($ord1=mysql_fetch_array($sql))
    {

      ?>
      if(data == '<?php echo $ord1['brand']; ?>')
        {
          option_html += "<option><?php echo $ord1['product_name']; ?></option>";
          /*alert(option_html);*/
        }
      <?php
    }

  ?>
  var par = document.getElementById("prod");
  par.innerHTML = "<option>--- select product ---</option>"+option_html;
}

function retrievequantity(product)
{
  var option_quantity_html = "";

  <?php
  $sql=mysql_query("SELECT product_name, quantity_in_stock FROM stock_info");
  while($ord2=mysql_fetch_array($sql))
    {
      $i=1;
      ?>
      if(product == '<?php echo $ord2['product_name']; ?>')
        {
          <?php
          while($i<=intval($ord2['quantity_in_stock'])){?>
            option_quantity_html += "<option><?php echo $i++; ?></option>";
            <?php }?>
        }
      <?php
    }

  ?>

  var par = document.getElementById("quantity");
  par.innerHTML = option_quantity_html;
}

</script>           




book_order2.php





<?php /*?><?php 
        $id = $_SESSION['id'];
        echo $id;
        ?><?php */?>
<?php
include("includes/db.php");



?>


<div class="grid_4">
  <div class="da-panel">
  <div class="da-panel-header">
  <span class="da-panel-title">
  <img src="images/icons/color/wand.png" alt="" />
  <font face="Cambria" size="7" color="#009900"><b>Book Order</b></font>
  </span>

  </div>
  <div class="da-panel-toolbar top">
  <ul>

  <li><a href="main.php?page=view_orders"><div class="da-button blue large">View all Orders</div></a></li>

  </ul>
  </div>

  <div class="da-panel-content">



  <?php

  if(isset($_POST['submit']))
    {
      extract($_POST);

      $order_date=date("Y-m-d");

      $sql=mysql_query("select sku,quantity_in_stock,sold_quantity,crdate from stock_info where product_name = '$prod'");
      $array=mysql_fetch_array($sql);
      $sku = $array[0];
      $qis = $array[1];
      $sold_quan = $array[2];
      $crdate = $array[3];

      $sql2=mysql_query("INSERT INTO order_management2(order_id,order_date,brand,product,price,customer_name,phone_number,email,address,quantity,channel,courier,order_status,sku)
    VALUES
    ('$id','$order_date','$brand','$prod','$pri','$customername','$phonenumber', '$email','$address','$quantity','$channel','$courier','booked','$sku')");


      if($sql2)
        {
          echo "<div class='da-message success'>Successfully Booked Your Order</div>"; 
          ?>
          <script>

          var r = confirm("want to add more products?");
          if (r == true)
            {
              //x="You pressed OK!";
              window.location = "main.php?page=book_order2";
            }
          else
            {
              //x="You pressed Cancel!";
              window.location = "main.php";
            }

          </script>
          <?php
        }
      else 
        {
          die(mysql_error());
        }


      $quantity_left = $qis - $quantity;
      $sold_quan = $sold_quan + $quantity;

      $diff_in_days = (strtotime($order_date) - strtotime($crdate))/(60 * 60 * 24);
      $expctd_stock=round((7*$quantity_left)/$diff_in_days);
      //echo $expctd_stock;



      $sql3 =mysql_query("UPDATE stock_info SET quantity_in_stock = '$quantity_left',last_sold_date='$order_date', sold_quantity='$sold_quan', expected_stock='$expctd_stock' WHERE sku='$sku'");
      /*$sql3 = mysql_query("update order_management set sku='$sku' where order_date=''");*/

      //$sql4 =mysql_query("select order_id from order_management where sku='$sku'");
      //$idarray=mysql_fetch_array($sql4);

    }
?>

<form id="da-ex-validate1" class="da-form" method="post" action="">


  <div class="da-form-row">
  <label>Brand<span class="required">*</span></label>
  <div class="da-form-item small">
  <!--<input type="text" name="brand"  id="brand" class="required" value=""/>-->
  <select name="brand" id="brand" onChange="retrievedata(this.value)">
  <option value="">--- select brand ---</option>
  <?php
  $ord=mysql_query("select * from brand_info");

while($ord1=mysql_fetch_array($ord))
  {

    ?>  
    <option value="<?php echo $ord1['brand'];?>"><?php echo $ord1['brand'];?></option> 

    <?php

  }
?>   
</select>
</div>
</div>
<div class="da-form-row">
  <label>Product<span class="required">*</span></label>
  <div class="da-form-item small">
  <select name="prod" id="prod" onChange="retrievequantity(this.value)">
  <option value="">--- select product    ---</option>
  </select>



  </div>
  </div> 
  <div class="da-form-row">
  <label>Customer name<span class="required">*</span></label>
  <div class="da-form-item small">
  <input type="text" name="customername"  id="customername" class="required char" value=""/>
  </div>
  </div>
  <div class="da-form-row">
  <label>Phone Number<span class="required">*</span></label>
  <div class="da-form-item small">
  <input type="text" name="phonenumber"  id="phonenumber" class="required number" value=""/>
  </div>
  </div> 

  <div class="da-form-row">
  <label>Email<span class="required">*</span></label>
  <div class="da-form-item small">
  <input type="text" name="email"  id="email" class="required email" value=""/>
  </div>
  </div>
  <div class="da-form-row">
  <label>Address<span class="required">*</span></label>
  <div class="da-form-item small">
  <textarea name="address" id="address" class="required"></textarea>

  </div>
  </div>

  <div class="da-form-row">
  <label>Quantity<span class="required">*</span></label>
  <div class="da-form-item small">

  <select name="quantity" id="quantity">
  <option value=""> --- select Quantity--- </option>
  </select>

  </div>
  </div>

  <div class="da-form-row">
  <label>Total Price<span class="required">*</span></label>
  <div class="da-form-item small">
  <input type="text" name="pri" id="pri" class="required number" value=""/>
  </div>
  </div>


  <div class="da-form-row">
  <label>Courier<span class="required"></span></label>
  <div class="da-form-item small">
  <!--<input type="text" name="courier"  id="courier" class="required" value=""/>-->
  <select name="courier" id="courier">
  <option value=""> ---select courier --- </option>
  <?php
  $ord=mysql_query("select courier_name from courier_info");

while($ord1=mysql_fetch_array($ord))
  {

    ?>  
    <option value="<?php echo $ord1['courier_name'];?>"><?php echo $ord1['courier_name'];?></option> 

    <?php

  }
?>   
</select>

</div>
</div> 

<div class="da-form-row">
  <label>Channel<span class="required"></span></label>
  <div class="da-form-item small">
  <!--<input type="text" name="channel"  id="channel" class="required" value=""/>-->
  <select name="channel" id="channel">
  <option value=""> --- select channel ---</option>
  <?php
  $ord=mysql_query("select channel from channel_info");

while($ord1=mysql_fetch_array($ord))
  {

    ?>  
    <option value="<?php echo $ord1['channel'];?>"><?php echo $ord1['channel'];?></option> 

    <?php

  }
?>   
</select>
</div>
</div>
<div class="da-button-row">
  <input type="submit"  name="submit"  value="submit" class="da-button grey" />
  <?php /*?><a href="book_order2.php?&id=<?php echo $id; ?>" name="submit" value="submit"  class="da-button grey">Book Order</a<?php */?>

  </div>

  </fieldset>
  </form>

  </div>
  <!-- End of .grid_4 --> </div>
  </div>

  <script>
  function retrievedata(data)
{
  var option_html = "";

  <?php
  $sql=mysql_query("SELECT distinct brand,product_name FROM stock_info");
  while($ord1=mysql_fetch_array($sql))
    {

      ?>
      if(data == '<?php echo $ord1['brand']; ?>')
        {
          option_html += "<option><?php echo $ord1['product_name']; ?></option>";
          /*alert(option_html);*/
        }
      <?php
    }

  ?>
  var par = document.getElementById("prod");
  par.innerHTML = "<option>--- select product ---</option>"+option_html;
}

function retrievequantity(product)
{
  var option_quantity_html = "";

  <?php
  $sql=mysql_query("SELECT product_name, quantity_in_stock FROM stock_info");
  while($ord2=mysql_fetch_array($sql))
    {
      $i=1;
      ?>
      if(product == '<?php echo $ord2['product_name']; ?>')
        {
          <?php
          while($i<=intval($ord2['quantity_in_stock'])){?>
            option_quantity_html += "<option><?php echo $i++; ?></option>";
            <?php }?>
        }
      <?php
    }

  ?>

  var par = document.getElementById("quantity");
  par.innerHTML = option_quantity_html;
}

</script>                      
4

2 に答える 2

0

注文番号をセッションに保存できます。このようにして、ページ間で保護され、永続化されます。

に注文を挿入したらbook_order.php、セッションに保存します。

$sql2=mysql_query(....); // inserting

if($sql2){
    $_SESSION['order_id'] = mysql_insert_id();
}

これでbook_order2.php、商品を挿入する前に注文 ID を取得できます。

$id = $_SESSION['order_id'];
// insert product with order_id = $id

session_start()PHP セッションを使用するには、セッションを使用するスクリプトの先頭でcalll を呼び出す必要があります。グローバル/ヘッダー インクルードがある場合は、そこで実行できます。


補足:

  • mysql_*廃止されました。PDO または MySQLi へのアップグレードを検討してください。これは、特に からアップグレードする場合に適した PDO チュートリアルですmysql_*
  • 変数を SQL に連結する代わりに、パラメータがバインドされた Prepared Statement を使用します。
于 2013-10-17T16:32:30.680 に答える