Skip to content

Instantly share code, notes, and snippets.

View J2112O's full-sized avatar

Josh Otwell J2112O

View GitHub Profile
-- MySQL dump 10.13 Distrib 5.7.21, for Linux (x86_64)
--
-- Host: localhost Database: projects
-- ------------------------------------------------------
-- Server version 5.7.21-0ubuntu0.16.04.1
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
INSERT INTO `user_details`(`id`, `first_name`, `last_name`)
VALUES (NULL,'John','Doe'),
(NULL, 'Jane', 'Doe');
// connection information
$pdo = new PDO('mysql:host=localhost;dbname=some_db_name;charset=utf8', 'some_user_name', 'some_password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Assume the 'user input' values have been sanitized and validated.
$first_name = 'Josh';
$last_name = 'Otwell';
// prepare insert statement
$insert_sql = "INSERT INTO `user_details`(`first_name`, `last_name`)
VALUES(:first_name, :last_name)";
$insert_stmt = $pdo->prepare($insert_sql);
$insert_stmt->bindValue(':first_name', $first_name);
$insert_stmt->bindValue(':last_name', $last_name);
$insert_stmt->execute();
$last_insert_id = $pdo->lastInsertId();
echo 'Last Insert ID: '.$last_insert_id;
<?php
try {
// connection information
$pdo = new PDO('mysql:host=localhost;dbname=some_db_name;charset=utf8', 'some_user_name', 'some_password');
$pdo - ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// begin a transaction
$pdo->beginTransaction();
// Assume the 'user input' values have been sanitized and validated.
$first_name = 'Josh';
$last_name = 'Otwell';
Last Insert ID: 3
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"/>
<title>Upload PDF</title>
</p><h4 class="text-center" style="margin-top: 100px;">Upload A PDF To The Database</h4>
<div class="d-flex justify-content-center align-self-center" style="margin-top: 115px;">
<form action="submit.php" method="POST" accept-charset="utf-8" enctype="multipart/form-data">
<div class="formgroup container-fluid">
<label for="project_name">Project Name</label>
<input type="text" name="project_name"/>
</div>
<div class="formgroup container-fluid">
<?php
if (isset($_POST['submit']) && !empty($_FILES['pdf_file']['name'])) {
//a $_FILES 'error' value of zero means success. Anything else and something wrong with attached file.
if ($_FILES['pdf_file']['error'] != 0) {
echo 'Something wrong with the file.';
} else { //pdf file uploaded okay.
//project_name supplied from the form field
$project_name = htmlspecialchars($_POST['project_name']);
//attached pdf file information
$file_name = $_FILES['pdf_file']['name'];