-1

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/bootstrap@5.0.1/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>
Barmar
  • 741,623
  • 53
  • 500
  • 612
recepbatir
  • 13
  • 3
  • `VALUE` should be `VALUES` – Barmar May 26 '21 at 19:04
  • You should have gotten an exception from that syntax error. – Barmar May 26 '21 at 19:04
  • You never execute the statement, maybe that's why you didn't get an exception. – Barmar May 26 '21 at 19:05
  • Those steps are between 3:06:00 and 3:09:00 in the video. – Barmar May 26 '21 at 19:08
  • I have changed `VALUE` to `VALUES` but still can't send data to the database. – recepbatir May 26 '21 at 19:10
  • After I changed it, I tried to execute it but it gives HTTP ERROR 500. – recepbatir May 26 '21 at 19:18
  • Check your PHP error log for the details. – Barmar May 26 '21 at 19:19
  • See https://stackoverflow.com/questions/2687730/how-can-i-make-php-display-the-error-instead-of-giving-me-500-internal-server-er – Barmar May 26 '21 at 19:19
  • Did you add all the `$statement->bindParam()` calls, like in the video? – Barmar May 26 '21 at 19:19
  • Warning: Undefined array key "title" in /opt/lampp/htdocs/php-crash-course-2020/14_product_crud/create.php on line 8 Warning: Undefined array key "description" in /opt/lampp/htdocs/php-crash-course-2020/14_product_crud/create.php on line 9 Warning: Undefined array key "price" in /opt/lampp/htdocs/php-crash-course-2020/14_product_crud/create.php on line 10 – recepbatir May 26 '21 at 19:53
  • I get `undefined array key` error. But it is defined below in the form. – recepbatir May 26 '21 at 19:54
  • 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 – recepbatir May 26 '21 at 19:55
  • You're missing the `)` at the end of the column list before `VALUES` – Barmar May 26 '21 at 19:57

2 Answers2

0

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/bootstrap@5.0.1/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>
0

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')");


?>
recepbatir
  • 13
  • 3