I'm new to php and still learning much about how databases work. I've been struggling trying to figure out how to add additional data into MySQL table with phpMyAdmin. I believe I at least added the additional inputs into my database table correctly, but on the php side of things I'm at a loss. I'm sure I'm forgetting something very simple here.
I'm able to successfully input $username, $email and $password, which are working fine with my DB table. Now I'm trying to add $firstname and $lastname but no luck! Admittedly my knowledge of this is very small.
Any help at all is really very appreciated!
Here's the PHP:
<?php
include('classes/DB.php');
if (isset($_POST['createaccount'])) {
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'];
if (!DB::query('SELECT username FROM users WHERE username=:username', array(':username'=>$username))) {
if (strlen($username) >= 3 && strlen($username) <= 32) {
if (preg_match('/[a-zA-Z0-9_]+/', $username)) {
if (strlen($password) >= 6 && strlen($password) <= 60) {
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
if (!DB::query('SELECT email FROM users WHERE email=:email', array(':email'=>$email))) {
DB::query('INSERT INTO users VALUES (\'\', :username, :password, :email, \'0\', \'\')', array(':username'=>$username, ':password'=>password_hash($password, PASSWORD_BCRYPT), ':email'=>$email));
Just in-case, DB.php:
<?php
class DB {
private static function connect() {
$pdo = new PDO('mysql:host=____;dbname=____;charset=utf8', '____', '____');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
}
public static function query($query, $params = array()) {
$statement = self::connect()->prepare($query);
$statement->execute($params);
if (explode(' ', $query)[0] == 'SELECT') {
$data = $statement->fetchAll();
return $data;
}
}
}