1

会社からアイテムを受け取ったら、PHPフォームにすべてのアイテムの詳細を入力し、MySQLに保存します

MySQLには2つのテーブルがあり、1つは受信で、2つ目は在庫です

だから私がアイテムを手に入れたとき、そのアイテムは2つのテーブルの受け取りと在庫に保存されます

ただし、在庫(テーブル)アイテムが同じであるため、アイテム名と会社が同じ場合は更新したいので、数量のみが変更されます

しばらくすると、新しいアイテムが正常に保存されます

どうすればこれを行うことができますか この問題を解決するのを手伝ってください ありがとう

  mysql_query("INSERT INTO  receive SET date='$date',company='$company',itemname='$itemname',quantity='$quantity',category='$category',signature='$signature'");

    $result = mysql_query("INSERT INTO stock SET date='$date',company='$company',itemname='$itemname',quantity='$quantity',category='$category',signature='$signature'")

これは私の完全なスクリプトです

 // creates the new record form
 // since this form is used multiple times in this file, I have made it a function that is easily reusable
 function renderForm($id ,$date ,$company,$itemname,$quantity,$category,$signature,  $error)

 {
 ?>
 <form id="searchform" action="" method="post" enctype="multipart/form-data">
  <div align="center">
 <fieldset>

   <div align="center">
     <legend align="center" >Stock Receive!</legend>
   </div>
   <div class="fieldset">
     <p>
   <label class="field" for="date">Date: </label>

       <input name="date" type="text" class="tcal" value="<?php echo date("Y-m-d");; ?>" size="30"/>
     </p>
               <p>
       <label class="field" >Company :</label>
       <input  name="company" type="text" id="company" value="<?php echo $company; ?>"  size="30"/>



     </p> 
   <p>
     <label class="field" for="item">Item: </label>

     <input name="itemname" type="text"  id="itemname" value="<?php echo $itemname; ?>" size="30"/>
   </p>
      <p>
       <label class="field" >Quantity :</label>
       <input  name="quantity" type="text"  id="quantity" value="<?php echo $quantity; ?>"  size="30"/>
     </p> 
       <p>
       <label class="field" >Category :</label>
       <input  name="category" type="text" id="category" value="<?php echo $category; ?>"  size="30"/>



     </p> 
   <p>
     <label class="field" for="username">Signature : </label>

     <input name="signature" type="text"  id="signature" readonly  value="<?php echo $_SESSION['SESS_FIRST_NAME']; ?>">
   </p>




  </div>
 </fieldset>
   <p align="center" class="required style3">Please Fill The Complete Form </p>
   <div align="center">
     <input name="submit" type="submit" class="style1" value="Submit">

   </div>
 </form> 


 <?php 
 // if there are any errors, display them
 if ($error != '')
 {
 echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 }
 ?> 




     <?php
     }

         $itemname = $_GET['itemname'];

         // connect to the database
         include 'connect-db.php';

         // check if the form has been submitted. If it has, start to process the form and save it to the database
         if (isset($_POST['submit']))
         {
         // get form data, making sure it is valid
         $id = mysql_real_escape_string(htmlspecialchars($_POST['id']));
         $date = mysql_real_escape_string(htmlspecialchars($_POST['date']));
         $company = mysql_real_escape_string(htmlspecialchars($_POST['company']));
         $itemname = mysql_real_escape_string(htmlspecialchars($_POST['itemname']));
         $quantity = mysql_real_escape_string(htmlspecialchars($_POST['quantity']));
         $category = mysql_real_escape_string(htmlspecialchars($_POST['category']));
         $signature = mysql_real_escape_string(htmlspecialchars($_POST['signature']));


             // check to make sure both fields are entered
             if ($date == '' || $quantity == '')
             {
             // generate error message
             $error = 'ERROR: Please fill in all required fields!';

             // if either field is blank, display the form again
             renderForm($id ,$date ,$company,$itemname,$quantity,$category,$signature,  $error);

             }
             else
             {
             // save the data to the database
              mysql_query("INSERT INTO  receive SET date='$date',company='$company',itemname='$itemname',quantity='$quantity',category='$category',signature='$signature'");

    $result = mysql_query("INSERT INTO stock SET date='$date',company='$company',itemname='$itemname',quantity='$quantity',category='$category',signature='$signature'")

                or die(mysql_error()); 
             echo "<center>Recive  Complete!</center>";
             // once saved, redirect back to the view page

             }
         }else
         // if the form hasn't been submitted, display the form
         {
         renderForm('','','','','','','','','','');
         }



    ?>
4

1 に答える 1

1
INSERT INTO stock SET
date='$date',company='$company',itemname='$itemname',quantity='$quantity',
category='$category',signature='$signature' 
ON DUPLICATE KEY UPDATE quantity='$quantity'

またはする

...ON DUPLICATE KEY UPDATE quantity='$quantity'+quantity 

既存の数量に追加する場合。

重複条件がトリガーされるように、会社とアイテム名に一意の複合キーを配置した後。

ALTER IGNORE TABLE stock ADD UNIQUE someName (company, itemname);
于 2013-11-05T11:28:36.377 に答える