PHP Error when insert value into database [closed]
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()
returnsTRUE
on success orFALSE
on error.Use
mysql_num_rows()
to find out how many rows were returned for a SELECT statement ormysql_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')
精彩评论