开发者

PHP Error when insert value into database [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

Here is the code:

function user_register($user) {

    $link 开发者_运维百科= db_connect();
    $query = sprintf('INSERT INTO users(user_name, user_password, user_email, display_name, register_date, active_date, user_reputation, user_status)
        VALUES("%s", "%s", "%s", "%s", "%s", "%s", %d, %d)',
        mysql_real_escape_string($user['username']),
        mysql_real_escape_string(md5($user['password'])),
        mysql_real_escape_string($user['email']),
        mysql_real_escape_string($user['name']),
        mysql_real_escape_string(get_current_time()),
        mysql_real_escape_string(get_current_time()),
        1,
        1);

    $result = mysql_query($query) or die();
    $num_rows = mysql_num_rows($result);

    mysql_free_result($result);
    mysql_close($link);
    if ( $num_rows == 1 ) {
        return true;
    }
    else {
        return false;
    }
}


Since you're not specifying the error you're getting, I'll just guess that mysql_num_rows doesn't like you passing it a boolean value instead of a resource. For this, please read the manual:

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

Use mysql_num_rows() to find out how many rows were returned for a SELECT statement or mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.

http://php.net/mysql_query
http://php.net/mysql_affected_rows

You also don't need to use mysql_free_result since you're not getting any results, just a boolean true/false.


Change the double quotes in your query to single quotes:

INSERT INTO users(user_name, user_password, user_email, display_name, register_date, active_date, user_reputation, user_status)
    VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d')
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜