简体   繁体   中英

debugging: i don't know where to fix my PHP code, there isn't an error and it isn't working

So i've been working on a social media site, and right now i'm making the registering portion of the site and this is what i have so far. It would be extremely helpful because i have just started learning PHP a little bit ago, and I don't know where to go from here since it does not result in a error.

if(isset($_POST['create-account'])){
    $username = $_POST['username'];
    $password = $_POST['password'];
    $email = $_POST['email'];

    DB::query('INSERT INTO users VALUES (\'\',:username,: password, :email)'. array(':username'=>$username, ':password'=>$password,':email'=>$email));
    echo 'succsessfull';
}
?>
<h1>Register</h1>
<form action="create-account.php" method='post'>
    <input type='text' name='username' value='' placeholder="Username..."><p />
    <input type='password' name='password' value='' placeholder="Password..."><p />
    <input type='email' name='email' value='' placeholder="Email..."><p />
    <input type='submit' name='createaccount' value='submit'>
</form>

i also have a file called DB(data base) and the code for that is, and thank you for helping me with my problem:

    <?php
class DB{
    private static function connect(){
        $pdo = new PDO('mysql:host=127.0.0.1;dbname=mysocial;charset=utf8;', 'root', '');
        $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);
        //$data = $statement->fetchAll();
        //return $data;
    }
}
?>

i don't know what else to say about my problem, besides hopefully i can finish this project

so basically i was a dumb, and when it said

if(isset($_POST[create-account])){

i should have put createaccount instead of create-account

Quick fix

<?php

// createaccount and not create-account
if(isset($_POST['createaccount'])){
    $username = $_POST['username'];
    $password = $_POST['password'];
    $email = $_POST['email'];
    
    $query = "INSERT INTO users VALUES (null,:username,:password, :email)";
    $params =  array(
                ':username'=>$username, 
                ':password'=>$password,
                ':email'=>$email 
    );
    
    // you had extra parenthesis at the end here. Keep your code clean, make it easier to read
   // DB::query()'. array(':username'=>$username, ':password'=>$password,':email'=>$email));
   
    if( DB::query( $query, $params ) )
    {
        echo 'succsessfull';
    }
    else {
        echo 'Failed';
    }
}

?>
<h1>Register</h1>
<form action="create-account.php" method='post'>
    <input type='text' name='username' value='' placeholder="Username..."><p />
    <input type='password' name='password' value='' placeholder="Password..."><p />
    <input type='email' name='email' value='' placeholder="Email..."><p />
    <input type='submit' name='createaccount' value='submit'>
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM