Skip to content

Instantly share code, notes, and snippets.

@marcosrivasr
Created December 2, 2021 00:17
Show Gist options
  • Save marcosrivasr/a2811dc0654b4706a5ca8dd4b4492312 to your computer and use it in GitHub Desktop.
Save marcosrivasr/a2811dc0654b4706a5ca8dd4b4492312 to your computer and use it in GitHub Desktop.
Archivos para el curso para crear un mini instagram con PHP
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
<?php
namespace Marcosrivasr\Instagram\lib;
use PDO;
use PDOException;
use Marcosrivasr\Instagram\config\Constants;
class Database{
private $host;
private $db;
private $user;
private $password;
private $charset;
public function __construct(){
$this->host = Constants::$HOST;
$this->db = Constants::$DB;
$this->user = Constants::$USER;
$this->password = Constants::$PASSWORD;
$this->charset = Constants::$CHARSET;
}
function connect(){
try{
$connection = "mysql:host=" . $this->host . ";dbname=" . $this->db . ";charset=" . $this->charset;
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false,
];
$pdo = new PDO($connection, $this->user, $this->password, $options);
return $pdo;
}catch(PDOException $e){
print_r('Error connection: ' . $e->getMessage());
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
</head>
<body>
<div class="container w-25 mt-5">
<form action="auth" method="POST">
<h2>Login</h2>
<div class="mb-3">
<label for="username" class="form-label">Username</label>
<input type="text" class="form-control" id="username" name="username">
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Password</label>
<input type="password" name="password" class="form-control" id="exampleInputPassword1">
</div>
<div class="mb-3">
<a href="signup" class="link-primary">Create a new account</a>
</div>
<button type="submit" class="btn btn-primary">Login</button>
</form>
</div>
</body>
</html>
-- MySQL dump 10.13 Distrib 8.0.22, for Linux (x86_64)
--
-- Host: localhost Database: instagram
-- ------------------------------------------------------
-- Server version 8.0.21-0ubuntu0.20.04.4
/*!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 */;
/*!50503 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `likes`
--
DROP TABLE IF EXISTS `likes`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `likes` (
`id` int NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`post_id` int NOT NULL,
`user_id` int NOT NULL,
PRIMARY KEY (`id`),
KEY `post_id` (`post_id`),
KEY `fk_likeuser` (`user_id`),
CONSTRAINT `fk_likeuser` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`),
CONSTRAINT `likes_ibfk_1` FOREIGN KEY (`post_id`) REFERENCES `posts` (`post_id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `posts`
--
DROP TABLE IF EXISTS `posts`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `posts` (
`post_id` int NOT NULL AUTO_INCREMENT,
`user_id` int NOT NULL,
`title` text NOT NULL,
`media` text NOT NULL,
PRIMARY KEY (`post_id`),
KEY `user_id` (`user_id`),
CONSTRAINT `posts_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `users`
--
DROP TABLE IF EXISTS `users`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `users` (
`user_id` int NOT NULL AUTO_INCREMENT,
`username` varchar(25) NOT NULL,
`password` varchar(255) NOT NULL,
`profile` varchar(255) NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2021-11-24 18:07:44
@EmmanuelDev20
Copy link

Por si alguno tiene problemas con la creación de la base de datos. Les dejo el código reorganizado:

CREATE DATABASE nombre_base_de_datos;
USE nombre_base_de_datos;

DROP TABLE IF EXISTS users;

CREATE TABLE users (
user_id int NOT NULL AUTO_INCREMENT,
username varchar(25) NOT NULL,
password varchar(255) NOT NULL,
profile varchar(255) NOT NULL,
PRIMARY KEY (user_id)
);

DROP TABLE IF EXISTS posts;

CREATE TABLE posts (
post_id int NOT NULL AUTO_INCREMENT,
user_id int NOT NULL,
title text NOT NULL,
media text NOT NULL,
PRIMARY KEY (post_id),
KEY user_id (user_id),
CONSTRAINT posts_ibfk_1 FOREIGN KEY (user_id) REFERENCES users (user_id)
);

DROP TABLE IF EXISTS likes;

CREATE TABLE likes (
id int NOT NULL AUTO_INCREMENT COMMENT 'primary key',
post_id int NOT NULL,
user_id int NOT NULL,
PRIMARY KEY (id),
KEY post_id (post_id),
KEY fk_likeuser (user_id),
CONSTRAINT fk_likeuser FOREIGN KEY (user_id) REFERENCES users (user_id),
CONSTRAINT likes_ibfk_1 FOREIGN KEY (post_id) REFERENCES posts (post_id)
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment