noam76 icon

insert select form

noam76 | PRO | 03/21/20 06:04:04 PM UTC | 0 ⭐ | 945 👁️ | Never ⏰ | []
PHP |

6.11 KB

|

None

|

0 👍

/

0 👎

OLD FILE
Table name: categories
# ID               - type: int(11)      - AUTO_INCREMENT
# stock_in_date    - type: timestamp   - current_timestamp()
# product_number   - type: varchar(255) utf8_unicode_ci
# categorie_number - type: int(11)
# supplier_ID      - type: int(11)
# amount_in        - type: int(11)
 
 
<?php
header('Content-Type: text/html; charset=utf-8');
include("database connection"); //insert data server
 
try {
  // read from categories table, to select in form
  $sql = "SELECT categories_name, categories_number FROM categories ORDER BY categories_name ASC";
  $stmt = $pdo->prepare($sql);
  $stmt->execute();
  $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
 
  // read from products table, to select in form
  $sqlproducts = "SELECT product_name, product_number FROM products ORDER BY product_name ASC";
  $stmt1 = $pdo->prepare($sqlproducts);
  $stmt1->execute();
  $resultsproducts = $stmt1->fetchAll(PDO::FETCH_ASSOC);
 
  // read from suppliers table, to select in form
  $sqlsuppliers = "SELECT supplier_name, ID FROM suppliers ORDER BY supplier_name ASC";
  $stmt2 = $pdo->prepare($sqlsuppliers);
  $stmt2->execute();
  $resultssupliers = $stmt2->fetchAll(PDO::FETCH_ASSOC);
  
  if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $pdo->beginTransaction();
    $categorienumber = $_REQUEST["output"];
    $prodnumber = $_REQUEST["output1"];
    $suppliernumber = $_REQUEST["output2"];
    $amount = $_POST['amount'];
 
    /*echo "Prod:";
    echo $prodnumber;
    echo '<br>';
    echo "Cat:";
    echo $categorienumber;
    echo '<br>';
    echo "Supplier:";
    echo $suppliernumber;
    echo '<br>';
    echo "Amount:";
    echo $amount;
    echo '<br>';*/
    
    if(empty($prodnumber) || empty($categorienumber) || empty($suppliernumber) || empty($amount)) {
      $status = "יש למלא את כל השדות";
      echo "<script type='text/javascript'>alert('$status');</script>";
    } else {
 
    // way 1
    $sql = "INSERT INTO stock_input (product_number, categorie_number, supplier_ID, amount_in) VALUES (:product_number, :categorie_number, :supplier_ID, :amount_in)";
    $stmt = $pdo->prepare($sql);      
    $stmt->execute(['product_number' => $prodnumber, 'categorie_number' => $categorienumber, 'supplier_ID' => $suppliernumber, 'amount_in' => $amount]);
 
  // way 2
    /*
     $table_data_integration = "stock_input";
     $data_integration = array
    (
     "product_number" => "$prodnumber",
     "categorie_number" => "$categorienumber",
     "supplier_ID" => "$suppliernumber",
     "amount_in" => "$amount",
    );
    $pdo->query("INSERT INTO `$table_data_integration` (`".implode('`,`',array_keys($data_integration))."`) VALUES ('".implode('\',\'',array_values($data_integration))."');");
    */
    // $last_id = $conn->lastInsertId(); // read last ID from the table
 
  // way 3
    /*
    $data = [
    'product_number' => $prodnumber,
    'categorie_number' => $categorienumber,
    'supplier_ID' => $suppliernumber,
    'amount_in' => $amount,
    ];
    $sql = "INSERT INTO stock_input (product_number, categorie_number, supplier_ID, amount_in) VALUES (:product_number, :categorie_number, :supplier_ID, :amount_in)";
    $stmt= $pdo->prepare($sql);
    $stmt->execute($data);
    */
 
    $status = "נקלט בהצלחה";
    echo "<script type='text/javascript'>alert('$status');</script>";
    }
  }
  //$prodnumber = "";
  //$categorienumber = "";
  //$suppliernumber = "";
  //$amount = "";
}
 
catch(Exception $e){
  $pdo->rollBack();
  // echo "שגיאה, לא נעשה עדכון<br />";
  echo "<script type='text/javascript'>alert('שגיאה, לא נעשה עדכון')</script>";
  // echo "<br>" . 'N° : '.$e->getCode();
  $pdo = null;
}
?>
 
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel = "stylesheet" type = "text/css" href = "myStyle.css"/>
    <title>הכנסה למלאי</title>
    <style>
    .form-control {
     border: -1px solid #ccc;
     text-align: right;
    }
    </style>
</head>
<body>
 <form action="" method="POST" class="form-style-9">
 <div align="right">
     <H1 align="right">מלאי נכנס</H1>
  <ul>
    <li>
    <!-- רשימה שם קטגוריה שמירת המספר -->
    <select class="field-style field-split align-right" name='categoriesnumber' onChange='outputValue(this)' >
    <option value='' selected disabled>בחר קטגוריה</option>
    <?php foreach($results as $row) 
    {
      echo "<option value='" . $row['categories_number'] . "'>" . $row['categories_name'] . "</option>";
    }?>    
    </select>
    <input type='hidden' id='output' name='output' readonly />
    
    <!-- שם מוצר ושמירת המספר -->
    <select class="field-style field-split align-left" name='productsnumber' onChange='outputValue1(this)'>
    <option value='' selected disabled>בחר מוצר</option>
    <?php foreach($resultsproducts as $row)
    {
      echo "<option value='" . $row['product_number'] . "'>" . $row['product_name'] . "</option>";
    }?>    
    </select>
    <input type='hidden' id='output1' name='output1' readonly />
    </li>
    
    <li>
    <!-- שם הספק -->
    <select class="field-style field-split align-right" name='suppliersnumber' onChange='outputValue2(this)'>
    <option value='' selected disabled>בחר שם הספק</option>
    <?php foreach($resultssupliers as $row)
    {
      echo "<option value='" . $row['ID'] . "'>" . $row['supplier_name'] . "</option>";
    }?>    
    </select>
    <input type='hidden' id='output2' name='output2' readonly />
      
    <input type="number" name="amount" placeholder="הכנס כמות" class="field-style field-split align-left " />
    
    <li>
     <input type="submit" value="הכנס מלאי חדש" style = "font-size:20px; float:right">
    </li>
  </ul>
 </div>
 </form>
</body>
</html>
<script>
  function outputValue(item)
  {
   document.getElementById('output').value = item.value;
  }
  function outputValue1(item)
  {
   document.getElementById('output1').value = item.value;
  }
  function outputValue2(item)
  {
   document.getElementById('output2').value = item.value;
  }
</script>

Comments