skip to Main Content

I have been learning PHP via https://www.youtube.com/watch?v=2eebptXfEvw tutorial. Timestamp is between 02:33:53 - Start working on Products CRUD (bad version) and 03:13:45 - Form Validation.

I am doing the same thing as the instructor. Created a database using phpmyadmin, then created a form and made a connection with pdo, I am trying to send data from my form to the database but cannot see the data when I look at the database table via phpmyadmin.

Also, I am using Manjaro Linux and installed Xampp server if that has anything to do with it.

Here is my create.php code. I also have an index.php file, it is mostly the same.

<?php

$pdo = new PDO('mysql:host=localhost;port=3306;dbname=products_crud', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// image=&title=&description=&price=

$title = $_POST['title'];
$description = $_POST['description'];
$price = $_POST['price'];
$date = date('Y-m-d H:i:s');

$pdo->prepare("INSERT INTO products (title, image, description, price, create_date
    VALUE (:title, :image, :description, :price, :date)");

?>

<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
    <link rel="stylesheet" href="app.css">

    <title>Products CRUD</title>
</head>

<body>
    <h1>Create new product</h1>

    <form enctype="multipart/form-data" action="create.php" method="POST">
        <div class="mb-3">
            <label>Product Image</label>
            <br>
            <input type="file" name="image">
        </div>
        <div class="mb-3">
            <label>Product Title</label>
            <input type="text" name="title" class="form-control">
        </div>
        <div class="mb-3">
            <label>Product Description</label>
            <textarea class="form-control" name="description"></textarea>

        </div>
        <div class="mb-3">
            <label>Product Price</label>
            <input type="number" step=".01" name="price" class="form-control">
        </div>

        <button type="submit" class="btn btn-primary">Submit</button>
    </form>
</body>

</html>

2

Answers


  1. Chosen as BEST ANSWER

    I have found the solution.

    <?php
    
    $pdo = new PDO('mysql:host=localhost;port=3306;dbname=products_crud', 'root', '');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // image=&title=&description=&price=
    
    $title = $_POST['title'];
    $description = $_POST['description'];
    $price = $_POST['price'];
    $date = date('Y-m-d H:i:s');
    
    // $pdo->exec("INSERT INTO products (title, image, description, price, create_date)
    //     VALUE (:title, :image, :description, :price, :date)");
    
    // Above statement gives this error:
    
    // Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':title, :image, :description, :price, :date)' at line 2 in /opt/lampp/htdocs/php-crash-course-2020/14_product_crud/create.php:13 Stack trace: #0 /opt/lampp/htdocs/php-crash-course-2020/14_product_crud/create.php(13): PDO->exec('INSERT INTO pro...') #1 {main} thrown in /opt/lampp/htdocs/php-crash-course-2020/14_product_crud/create.php on line 13
    
    // So I changed into the line below and it works.I suppose it is about SQL syntax and my MariaDB server version.
    
    $pdo->exec("INSERT INTO products (title, image, description, price, create_date)
        VALUE ('$title', '', '$description', $price, '$date')");
    
    
    ?>
    

  2. You are missing the execute statement after prepare.

    <?php
    
    $pdo = new PDO('mysql:host=localhost;port=3306;dbname=products_crud', 'root', '');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // image=&title=&description=&price=
    
    $title = $_POST['title'];
    $description = $_POST['description'];
    $price = $_POST['price'];
    $date = date('Y-m-d H:i:s');
    
    $pdo->prepare("INSERT INTO products (title, image, description, price, create_date
        VALUE (:title, :image, :description, :price, :date)");
    //this line is added that you are missing
    $pdo->execute(); 
    
    
    ?>
    
    <!doctype html>
    <html lang="en">
    
    <head>
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
    
        <!-- Bootstrap CSS -->
        <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
        <link rel="stylesheet" href="app.css">
    
        <title>Products CRUD</title>
    </head>
    
    <body>
        <h1>Create new product</h1>
    
        <form enctype="multipart/form-data" action="create.php" method="POST">
            <div class="mb-3">
                <label>Product Image</label>
                <br>
                <input type="file" name="image">
            </div>
            <div class="mb-3">
                <label>Product Title</label>
                <input type="text" name="title" class="form-control">
            </div>
            <div class="mb-3">
                <label>Product Description</label>
                <textarea class="form-control" name="description"></textarea>
    
            </div>
            <div class="mb-3">
                <label>Product Price</label>
                <input type="number" step=".01" name="price" class="form-control">
            </div>
    
            <button type="submit" class="btn btn-primary">Submit</button>
        </form>
    </body>
    
    </html>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search