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
I have found the solution.
You are missing the execute statement after prepare.