jeudi 29 octobre 2020

What am I doing wrong? php script to INSERT INTO database table [duplicate]

This is nothing special. just something for my personal end. Is there something im doing wrong? Similar Code seems to work fine on my other page but on this page it doesn't seem to work. my includes are accurate. my item table structure is correct. im confused? Nothing is getting added into DB,... Page runs fine... no obvious errors....

<!DOCTYPE  html>
<head>
<title>Add Item To Database</title>
</head>
<body>
<?php include'includes/sql.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, Image, Sku, Weight, Category, Active) VALUES ('".$_POST['item_name']."', '".$_POST['item_price']."', 'NULL', '".$_POST['item_sku']."', '".$_POST['item_weight']."', '".$_POST['item_category']."', '1')"; 
    mysqli_query($conn, $add);



}

?>



</body>



</html>

yet this works just fine

<html>
<head>
<title>Brewtensils - Contact Us</title>
<script>function validateforms() {
  var x = document.forms["commentform"]["email"].value;
  if (x == "") {
    alert("Email must be filled out");
    return false;
  }
  
  var x = document.forms["commentform"]["phone"].value;
  if (x == "") {
    alert("Please enter your phone number");
    return false;
  }
  
  var x = document.forms["commentform"]["comment"].value;
  if (x == "") {
    alert("Comment must be filled out");
    return false;
  }
}
</script>
<link rel="stylesheet" href="css/main.css" type="text/css">
<link rel="stylesheet" href="css/header.css" type="text/css">
<link rel="stylesheet" href="css/footer.css" type="text/css">

</head>


<body style="background-color:66a722;">
<?php $page = 4;?>
<?php include'includes/sql.php'?>
<?php include'includes/header.php'?>



<div id='secondarybg'>

<?php

if($_SERVER['REQUEST_METHOD'] != 'POST')
{

    ?>
    
    <div id='contactinfo'><center><br><br><br><br><br><br><p>If you have any questions, comments, or suggestions<br>
                              feel free to contact us.<br> 937-938-9426 <br>support@brewtensils.store<br>Thanks for your input!</p><br><br><br></center>
                            
                              
                              <center>
                                <form id="commentform" name="commentform" method="post" onsubmit="return validateforms()">
                                    <center><input type="text" name="email" placeholder="Email" class="text" size="32" id="email"/></center><br>
                                    <center><input type="text" name="phone" placeholder="Your Phone No." class="text" size="25" id="phone"/></center><br>
                                    <center><textarea cols="50" rows="4" name="comment" placeholder="Enter your Comment, Suggestion, or Question here."></textarea> </center> 
                                    <center><br>
                                    <input type="submit" class="login" value="Send" name="send">
                                    </center>
                                </form>
                              </center>
    </div>
<?php 
}
else {
    
     
     $required = array('email', 'phone', 'comment');

// Loop over field names, make sure each one exists and is not empty
$error = false;
foreach($required as $field) {
  if (empty($_POST[$field])) {
    $error = true;
  }
}

if ($error) {


echo'';

} else {
 

    
     
 
        //the form has been posted without, so save it
        //notice the use of mysql_real_escape_string, keep everything safe!
        //also notice the sha1 function which hashes the password
        $time= date("Y-m-d h:i:sa");
        $sql = "INSERT INTO
                    comments(email, phone, comment, time)
                VALUES('".$_POST['email']."',
                       '".$_POST['phone']."',
                       '".$_POST['comment']."',
                        '".$time."'
                        )";
                         
        mysqli_query($conn, $sql);
        
        echo'<center><div id="commentsuccess"><p id="commentsuccesstext">Thanks for your submission!<br>
                        Staff will review this info and if necessary get back<br>
                        with you in 1-3 business days.</p></div></center>';
        
        
    }
    
}
            














?>
<br><br><br><br><br><br>

    </div>



<?php include'includes/footer.php';?>








</body>


</html>



Aucun commentaire:

Enregistrer un commentaire