0

trying to insert into a table into my DB.... this is my code. anything obvious im doing wrong?? DB structure is okay, not getting any errors? Any suggestions? $conn is defined in the includes/sql.php as: Again very first time using PDO. Got used to SQL, then SQLi, now its PDO :P trying to keep up.Any help is greatly appreciated. Thanks Again...

<?php
$host = 'Confidential';
$db   = 'Confidential';
$user = 'Confidential';
$pass = 'Confidential';
$charset = 'utf8mb4';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];
try {
     $conn = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
     throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
?>
<!DOCTYPE  html>
<head>
<title>Add Item To Database</title>
</head>
<body>
<?php include'includes/sql.php';?>
<?php include'includes/header.php';?>






<?php
if($_SERVER['REQUEST_METHOD'] != 'POST')
{
    ?>
<form>
    <label for='item_name'>Item Name</label>
        <input type='text' size='50' name='item_name' id='item_name'>
        
    <label for='item_price'>Price</label>
        <input type='text' size='50' name='item_price' id='item_price'>
        
    <label for='item_weight'>Weight</label> 
        <input type='text' size='50' name='item_weight' id='item_weight'>
        
    <label for='item_sku'>Sku</label>
        <input type='text' size='50' name='item_sku' id='item_sku'>
        
        <label for='item_category'>category</label>
        <select name='item_category' id='item_category'>
            <option value="Grain">Grain</option>
            <option value="Cleaning">Cleaning & Sanitation</option>
            <option value="WineKits">Wine Kits</option>
            <option value="BeerKits">Beer Kits</option>
            <option value="Equiptment">Equiptment</option>
            <option value="Yeast">Yeast</option>
            <option value="Hops">Hops</option>
            <option value="Chemicals">Chemicals</option>
            <option value="Books">Books</option>
            <option value="Additives">Additives & Flavorings</option>
            <option value="Racking">Racking & Bottling</option>
        </select>
        <input type="submit" id="Add" name="Add" value="ADD">
</form>
<?php 
}
else {
    
    $name = $_POST['item_name'];
    $price= $_POST['item_price'];
    $weight= $_POST['item_weight'];
    $sku= $_POST['item_sku'];
    $category= $_POST['item_category'];

    $add = "INSERT INTO items(Name, Price, Sku, Weight, Category) VALUES (?, ?, ?, ?, ?)"; 
    
    $stmt = $conn->prepare($add);
    $stmt->execute([$name, $price, $weight, $sku, $category]);
    



}

?>



</body>



</html>

  • if insert fails, there is **always** an error. 1. Make sure you can see PHP errors in general. 2. Make sure you are checking the same database – Your Common Sense Oct 29 '20 at 10:20
  • Also make sure the insert actually takes place. Actually, this is it. your form is using the GET method, no wonder the query never getsexecuted – Your Common Sense Oct 29 '20 at 10:26

0 Answers0