0
votes

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;
           }
      }

}
1

1 Answers

0
votes

I'm also quite new at php/mysql so it might not be the right answer but your Insert statement is incorrect from what I know.

DB::query('INSERT INTO users VALUES (\'\', :username, :password, :email, \'0\', \'\')', array(':username'=>$username, ':password'=>password_hash($password, PASSWORD_BCRYPT), ':email'=>$email));

I always specify the columns name so it looks like this:

INSERT INTO table_name (columnname1, columnname2, etc.) VALUES (columnvalue1, columnvalue2, etc.);

Again not 100% sure but that's how I normally do it.

Here's an example of one of my INSERT :

mysql_query("INSERT INTO memberkey (member_id, randomkey) VALUES (".$_SESSION['id'].", '".$keyn."')");