Advertisement
noam76

insert select form

Mar 21st, 2020
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.11 KB | None | 0 0
  1. OLD FILE
  2. Table name: categories
  3. # ID               - type: int(11)      - AUTO_INCREMENT
  4. # stock_in_date    - type: timestamp   - current_timestamp()
  5. # product_number   - type: varchar(255) utf8_unicode_ci
  6. # categorie_number - type: int(11)
  7. # supplier_ID      - type: int(11)
  8. # amount_in        - type: int(11)
  9.  
  10.  
  11. <?php
  12. header('Content-Type: text/html; charset=utf-8');
  13. include("database connection"); //insert data server
  14.  
  15. try {
  16.   // read from categories table, to select in form
  17.   $sql = "SELECT categories_name, categories_number FROM categories ORDER BY categories_name ASC";
  18.   $stmt = $pdo->prepare($sql);
  19.   $stmt->execute();
  20.   $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
  21.  
  22.   // read from products table, to select in form
  23.   $sqlproducts = "SELECT product_name, product_number FROM products ORDER BY product_name ASC";
  24.   $stmt1 = $pdo->prepare($sqlproducts);
  25.   $stmt1->execute();
  26.   $resultsproducts = $stmt1->fetchAll(PDO::FETCH_ASSOC);
  27.  
  28.   // read from suppliers table, to select in form
  29.   $sqlsuppliers = "SELECT supplier_name, ID FROM suppliers ORDER BY supplier_name ASC";
  30.   $stmt2 = $pdo->prepare($sqlsuppliers);
  31.   $stmt2->execute();
  32.   $resultssupliers = $stmt2->fetchAll(PDO::FETCH_ASSOC);
  33.  
  34.   if($_SERVER['REQUEST_METHOD'] == 'POST') {
  35.     $pdo->beginTransaction();
  36.     $categorienumber = $_REQUEST["output"];
  37.     $prodnumber = $_REQUEST["output1"];
  38.     $suppliernumber = $_REQUEST["output2"];
  39.     $amount = $_POST['amount'];
  40.  
  41.     /*echo "Prod:";
  42.     echo $prodnumber;
  43.     echo '<br>';
  44.     echo "Cat:";
  45.     echo $categorienumber;
  46.     echo '<br>';
  47.     echo "Supplier:";
  48.     echo $suppliernumber;
  49.     echo '<br>';
  50.     echo "Amount:";
  51.     echo $amount;
  52.     echo '<br>';*/
  53.    
  54.     if(empty($prodnumber) || empty($categorienumber) || empty($suppliernumber) || empty($amount)) {
  55.       $status = "יש למלא את כל השדות";
  56.       echo "<script type='text/javascript'>alert('$status');</script>";
  57.     } else {
  58.  
  59.     // way 1
  60.     $sql = "INSERT INTO stock_input (product_number, categorie_number, supplier_ID, amount_in) VALUES (:product_number, :categorie_number, :supplier_ID, :amount_in)";
  61.     $stmt = $pdo->prepare($sql);      
  62.     $stmt->execute(['product_number' => $prodnumber, 'categorie_number' => $categorienumber, 'supplier_ID' => $suppliernumber, 'amount_in' => $amount]);
  63.  
  64.   // way 2
  65.     /*
  66.      $table_data_integration = "stock_input";
  67.      $data_integration = array
  68.     (
  69.      "product_number" => "$prodnumber",
  70.      "categorie_number" => "$categorienumber",
  71.      "supplier_ID" => "$suppliernumber",
  72.      "amount_in" => "$amount",
  73.     );
  74.     $pdo->query("INSERT INTO `$table_data_integration` (`".implode('`,`',array_keys($data_integration))."`) VALUES ('".implode('\',\'',array_values($data_integration))."');");
  75.     */
  76.     // $last_id = $conn->lastInsertId(); // read last ID from the table
  77.  
  78.   // way 3
  79.     /*
  80.     $data = [
  81.     'product_number' => $prodnumber,
  82.     'categorie_number' => $categorienumber,
  83.     'supplier_ID' => $suppliernumber,
  84.     'amount_in' => $amount,
  85.     ];
  86.     $sql = "INSERT INTO stock_input (product_number, categorie_number, supplier_ID, amount_in) VALUES (:product_number, :categorie_number, :supplier_ID, :amount_in)";
  87.     $stmt= $pdo->prepare($sql);
  88.     $stmt->execute($data);
  89.     */
  90.  
  91.     $status = "נקלט בהצלחה";
  92.     echo "<script type='text/javascript'>alert('$status');</script>";
  93.     }
  94.   }
  95.   //$prodnumber = "";
  96.   //$categorienumber = "";
  97.   //$suppliernumber = "";
  98.   //$amount = "";
  99. }
  100.  
  101. catch(Exception $e){
  102.   $pdo->rollBack();
  103.   // echo "שגיאה, לא נעשה עדכון<br />";
  104.   echo "<script type='text/javascript'>alert('שגיאה, לא נעשה עדכון')</script>";
  105.   // echo "<br>" . 'N° : '.$e->getCode();
  106.   $pdo = null;
  107. }
  108. ?>
  109.  
  110. <html lang="en">
  111. <head>
  112.     <meta charset="UTF-8">
  113.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  114.     <link rel = "stylesheet" type = "text/css" href = "myStyle.css"/>
  115.     <title>הכנסה למלאי</title>
  116.     <style>
  117.     .form-control {
  118.      border: -1px solid #ccc;
  119.      text-align: right;
  120.     }
  121.     </style>
  122. </head>
  123. <body>
  124.  <form action="" method="POST" class="form-style-9">
  125.  <div align="right">
  126.      <H1 align="right">מלאי נכנס</H1>
  127.   <ul>
  128.     <li>
  129.     <!-- רשימה שם קטגוריה שמירת המספר -->
  130.     <select class="field-style field-split align-right" name='categoriesnumber' onChange='outputValue(this)' >
  131.     <option value='' selected disabled>בחר קטגוריה</option>
  132.     <?php foreach($results as $row)
  133.     {
  134.       echo "<option value='" . $row['categories_number'] . "'>" . $row['categories_name'] . "</option>";
  135.     }?>    
  136.     </select>
  137.     <input type='hidden' id='output' name='output' readonly />
  138.    
  139.     <!-- שם מוצר ושמירת המספר -->
  140.     <select class="field-style field-split align-left" name='productsnumber' onChange='outputValue1(this)'>
  141.     <option value='' selected disabled>בחר מוצר</option>
  142.     <?php foreach($resultsproducts as $row)
  143.     {
  144.       echo "<option value='" . $row['product_number'] . "'>" . $row['product_name'] . "</option>";
  145.     }?>    
  146.     </select>
  147.     <input type='hidden' id='output1' name='output1' readonly />
  148.     </li>
  149.    
  150.     <li>
  151.     <!-- שם הספק -->
  152.     <select class="field-style field-split align-right" name='suppliersnumber' onChange='outputValue2(this)'>
  153.     <option value='' selected disabled>בחר שם הספק</option>
  154.     <?php foreach($resultssupliers as $row)
  155.     {
  156.       echo "<option value='" . $row['ID'] . "'>" . $row['supplier_name'] . "</option>";
  157.     }?>    
  158.     </select>
  159.     <input type='hidden' id='output2' name='output2' readonly />
  160.      
  161.     <input type="number" name="amount" placeholder="הכנס כמות" class="field-style field-split align-left " />
  162.    
  163.     <li>
  164.      <input type="submit" value="הכנס מלאי חדש" style = "font-size:20px; float:right">
  165.     </li>
  166.   </ul>
  167.  </div>
  168.  </form>
  169. </body>
  170. </html>
  171. <script>
  172.   function outputValue(item)
  173.   {
  174.    document.getElementById('output').value = item.value;
  175.   }
  176.   function outputValue1(item)
  177.   {
  178.    document.getElementById('output1').value = item.value;
  179.   }
  180.   function outputValue2(item)
  181.   {
  182.    document.getElementById('output2').value = item.value;
  183.   }
  184. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement