开发者

error in user registration form using php [closed]

开发者 https://www.devze.com 2023-03-06 22:14 出处:网络
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in t开发者_高级运维ime,or an extraordinarily narrow situation that is not g
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in t开发者_高级运维ime, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 11 years ago.

i'm trying to do the following but i have this error

it doesn't store in the my DB

DB name = job , table login , attributes : loginId , loginname , loginpassword

  <html>
<head>
    <?php
    error_reporting(E_ALL);
    ?>
    <?php
    $link = mysql_connect('localhost', 'root', '123456');
    echo "from DB";
    mysql_select_db('job', $link);
    ?>
    <?

    class user {

        private $userId;
        private $username;
        private $password;

        public function getUsername() {
            return $this->username;
        }

        public function setUsername($username) {
            $this->username = $username;
        }

        public function getUserId() {
            return $this->userId;
        }

        public function setUserId($userId) {
            $this->userId = $userId;
        }

        public function getPassword() {
            return $this->password;
        }

        public function setPassword($password) {
            $this->password = $password;
        }

        function __construct() {

        }

    }
    ?>
</head>
<body>
    <form action="" method="POST" >
        username :<input type="text" size="30" name="username" >
        password: <input type="password" size="30" name="password">
        <input type="submit" value="singup!" name="submit" />
    </form>
    <?php

    function addNewuser($user, $link) {
        global $userModel;
        echo "$user->getUsername()";
        $query = sprintf("INSERT INTO login ('loginname','loginpassword') values('%s' ,'%s')",
                        $user->getUsername(),
                        $user->getPassword()
        );

        $generatedId = mysql_insert_id($link);
        mysql_query($query, $link);

        echo "AFTER ADD FUNCTION";


        $query2 = sprintf("SELECT loginname FROM login WHERE loginname='%s' AND loginpassword='%s'",
                        $user->getUsername(),
                        $user->getPassword());

        $result = mysql_query($query2, $link);
        echo "$result";
        while ($row = mysql_fetch_array($result)) {
            echo $row['loginname'];
        }
    }

    if (isset($_POST['submit'])) {
        $user = New User();
        $username = $_POST['username'];
        $password = $_POST['password'];

        $user->setUsername($username);
        $user->setPassword($password);

        addNewuser($user, $link);
        echo "add done ! ";



        mysql_close($link);
    }
    ?>

</body>


Your insert query syntax is wrong. Should be:

    $query = sprintf("INSERT INTO login (loginname,loginpassword) values('%s' ,'%s')",
                    $user->getUsername(),
                    $user->getPassword()
    );

Also please read about SQL injection as your code is vulnerable.


According to your problem, which is

DB name = job , table login , attributes : loginId , loginname , loginpassword

I think your code at this bit

$query = sprintf("INSERT INTO login ('loginname','loginpassword') values('%s' ,'%s')",
                        $user->getUsername(),
                        $user->getPassword()
        );

is missing loginID attribute, you can change it to

$query = sprintf("INSERT INTO login (loginId, loginname, loginpassword) values('','%s' ,'%s')",
                            $user->getUsername(),
                            $user->getPassword()
            );

It should work. And please remember to eliminate single quotes in your query string :)


PHP is case sensitive.

Fix it to:

if (isset($_POST['submit'])) {
    $user = new user();
    ...

Also make sure that PHP erros are on when you are checking the code, because your current implementation should have thrown an error at that line

0

精彩评论

暂无评论...
验证码 换一张
取 消