Как да направим лесна контактна форма с PHP


GitHub: Вземи кода
YouTube: PHP Уроци: Как да направим лесна контактна форма


index.php

<?php

define("DB_HOST", "localhost");
define("DB_NAME", "tut_db");
define("DB_USERNAME", "root");
define("DB_PASSWORD", "");

include('Database.php');

$db = new Database();

?>

<form method="POST">
    <input type="email" name="email" placeholder="Your email"><br>
    <input type="text" name="subject" placeholder="Subject"><br>
    <textarea name="message" rows="10" cols="60"></textarea><br>
    <input type="submit" name="send" value="Send">
</form>

<?php 
    if(isset($_POST['send'])){
        if(!empty($_POST['email']) && !empty($_POST['subject']) && !empty($_POST['message'])){
            $email = $_POST['email'];
            $subject = $_POST['subject'];
            $message = $_POST['message'];

            $insertData = array(
                'from_email' => $email,
                'subject' => $subject,
                'message' => $message,
                'sent_time' => time(),
                
            );

            $db->insert('emails', $insertData);
            mail('me@itsvalentin.com', $subject, $message);
        }
    }

?>


Database.php

<?php 

    class Database {
        public function __construct() {
            $this->db = new PDO("mysql:host=".DB_HOST."; dbname=".DB_NAME, DB_USERNAME, DB_PASSWORD);
        }

        public function query($sql){
        	$query = $this->db->prepare($sql);
        	$query->execute();

        }

        public function selectOne($sql){
            $query = $this->db->prepare($sql);
            $query->execute();
            return $query->fetch();          
        }

        public function selectAll($sql){
            $query = $this->db->prepare($sql);
            $query->execute();     
            return $query->fetchAll();       
        }

        public function update($table, $data, $where){
            $set_data = '';
            $where_data = '';
            foreach($data as $key => $value) {
                $set_data .= ", {$key} = '{$value}'";
            }
            foreach($where as $key => $value) {
                $where_data .= "AND {$key} = '{$value}'";
            }
            $set_data = ltrim($set_data,',');
            $where_data = ltrim($where_data,'AND') ;
            
            $this->db->query("UPDATE $table SET {$set_data} WHERE {$where_data}");
            return true;
        }

        public function insert($table, $where){
            $set = '';
            $i = 0;
            foreach($where as $key => $value) {
                if($i == 0){
                    $set .= "SET {$key} = '{$value}'";
                }else{
                    $set .= ", {$key} = '{$value}'";
                }
                $i++;
            }
            $set = ltrim($set,',');
            
            $this->db->query("INSERT INTO $table {$set}");
            $id = $this->db->lastInsertId();
            return $id;
        }

        public function delete($table, $data){
            foreach($data as $key => $value) {
                $set_data .= ", {$key} = '{$value}'";
            }

            $set_data = ltrim($set_data,',');

            $this->db->query("DELETE FROM $table WHERE {$set_data}");

        }

    }	
?>


SQL

CREATE TABLE `emails` (
  `id` int(11) NOT NULL,
  `from_email` varchar(255) NOT NULL,
  `subject` text NOT NULL,
  `message` text NOT NULL,
  `sent_time` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

ALTER TABLE `emails`
  ADD PRIMARY KEY (`id`);
  
ALTER TABLE `emails`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;

Related Posts

На живо
CURRENTLY OFFLINE