0

I am creating a small web store where I can create new products, recently it hasn't been working at all, it should work like this -> I insert the name, price, then I choose one of the three categories (Books, Hard drives and Furniture) then based on which I choose a new insert comes up for example for books a weight insert appears for furniture three inserts appear which are Width Length and Height, it used to work only if I inserted all values for one category (like I would also have to insert the width length and height and the other one which is the size and then it would insert into a database, now it just doesn't work at all and I don't know where is my mistake)

Here is the create_product.php with the form( I use display none and then jquery to .show which categories insert appears( maybe that isn't the right way to go about it? ) )

<?php
// include database and object files
include_once 'config/database.php';
include_once 'objects/product.php';
include_once 'objects/category.php';

// get database connection
$database = new Database();
$db = $database->getConnection();

// pass connection to objects
$product = new Product($db);
$category = new Category($db);

// set page headers
$page_title = "Create Product";
include_once "layout_header.php";

echo "<div class='right-button-margin'>";
    echo "<a href='index.php' class='btn btn-default pull-right'>Read Products</a>";
echo "</div>";

?>
<?php 
// if the form was submitted 
if($_POST){

    // set product property values
    $product->name = $_POST['name'];
    $product->price = $_POST['price'];
    $product->category_id = $_POST['category_id'];
    $product->size = $_POST['size'];
    $product->weight = $_POST['weight'];
    $product->lenght = $_POST['lenght'];
    $product->width = $_POST['width'];
    $product->height = $_POST['height'];


    // create the product
    if($product->create()){
        echo "<div class='alert alert-success'>Product was created.</div>";
    }

    // if unable to create the product, tell the user
    else{
        echo "<div class='alert alert-danger'>Unable to create product.</div>";
    }
}
?>

<!-- HTML form for creating a product -->


<script>
    // script for when you change the categories so that the appropriate input field appears
$(document).ready(function(){
    $('.form-control').on('change', function() {


      if ( this.value == 1)
      {
        $("#weight").show();
        $("#size").hide();
        $("#height").hide();
      }


      if ( this.value == 2)
      {
        $("#weight").hide();
        $("#size").show();
        $("#height").hide();

      }
      if ( this.value == 3)
      {
        $("#weight").hide();
        $("#size").hide();
        $("#height").show();

      }


    });
});

 </script>

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">

    <table class='table table-hover table-responsive table-bordered'>

        <tr>
            <td>Name</td>
            <td><input type='text' name='name' class='form-control' /></td>
        </tr>

        <tr>
            <td>Price</td>
            <td><input type='text' name='price' class='form-control' /></td>
        </tr>

    <div >
        <tr id="weight" style="display:none">

            <td >Weight</td>
            <td><input type="text" name='weight' class='form-control'></input></td>
        </tr>
    </div>


        <tr id="size" style="display:none">

            <td>Size</td>
            <td><input name='size' class='form-control'></input></td>
        </tr>

        <tr id="height" style="display:none">

            <td>Height</td>
            <td><input name='height' class='form-control'></input></td>
            <td>Length</td>
            <td><input name='lenght' class='form-control'></input></td>
            <td>Width</td>
            <td><input name='width' class='form-control'></input></td>
        </tr>

        <tr>
            <td>Category</td>
            <td>
            <?php
// read the product categories from the database
$stmt = $category->read();

// put them in a select drop-down
echo "<select class='form-control' name='category_id'>";
    echo "<option>Select category...</option>";

    while ($row_category = $stmt->fetch(PDO::FETCH_ASSOC)){
        extract($row_category);
        echo "<option value='{$id}'>{$name}</option>";
    }

echo "</select>";
?>
            </td>
        </tr>

        <tr>
            <td></td>
            <td>
                <button type="submit" class="btn btn-primary">Create</button>
            </td>
        </tr>

    </table>
</form>

<?php

// footer
include_once "layout_footer.php";
?>

and here is the product.php where the query happens

<?php
class Product{

    // database connection and table name
    private $conn;
    private $table_name = "products";

    // object properties
    public $id;
    public $name;
    public $price;
    public $category_id;
    public $size;
    public $weight;
    public $lenght;
    public $width;
    public $height;

    public function __construct($db){
        $this->conn = $db;
    }

    function create(){

        //write query
        $this->size = !empty($this->size) ? $this->size: null;
        $this->weight= !empty($this->weight) ? $this->weight : null;
        $this->lenght= !empty($this->lenght) ? $this->lenght : null;
        $this->width= !empty($this->width) ? $this->width : null;
        $this->height= !empty($this->height) ? $this->height : null;

        $query = "INSERT INTO
                        " . $this->table_name . "
                    VALUES
                        name=:name, price=:price,  category_id=:category_id, size=:size, weight=:weight, lenght=:lenght, width=:width,height=:height";

        $stmt = $this->conn->prepare($query);

        // bind values 
        $stmt->bindParam(":name", $this->name);
        $stmt->bindParam(":price", $this->price);
        $stmt->bindParam(":category_id", $this->category_id);
        $stmt->bindParam(":size", $this->size);
        $stmt->bindParam(":weight", $this->weight);
        $stmt->bindParam(":lenght", $this->lenght);
        $stmt->bindParam(":width", $this->width);
        $stmt->bindParam(":height", $this->height);

        if(!empty($stmt->execute())){
            return true;
        }else{
            return false;
        }

    }

}

?>

In my database, all of the attributes (size, weight, etc. ) are set that they can be NULL so that shouldn't be a problem

Ferran Buireu
  • 28,630
  • 6
  • 39
  • 67
  • When you write a question, please write it in a way that's easy to read. The first paragraph is basically just one gigantic sentence, which makes it really hard to follow. You also need to be a bit more explicit than just _"now it just doesn't work at all"_, which is a terrible explanation that doesn't tell us much. What specifically happens? Errors? Checked the web servers error log? – M. Eriksson Apr 25 '20 at 09:56
  • You should add some [error handling](https://stackoverflow.com/questions/32648371/my-pdo-statement-doesnt-work) to your queries. That would tell you that the syntax is wrong. Either do: `INSERT INTO theTable (col1, col2) VALUES (:col1, :col2)` or `INSERT INTO theTable SET col1 = :col1, col2 = :col2`. You've mixed them up. – M. Eriksson Apr 25 '20 at 10:04

0 Answers0