I am getting this error: Column ‘Username’ cannot be null.
First of all I am using jQuery UI. I made a modal form for registering users.
Here an extraction from my JS:
if ( bValid ) {
var form = $('#register_form');
var data = form.serialize();
$.post('index.php?section=register', data, function(response) {
alert(response);
$( "#dialog_create_user_ok" ).dialog( "open" );
});
$( this ).dialog( "close" );
}
This seems to work well. Checked it with firebug and I get the correct post values. Username=…&Password=…
Now an extraction from my register.php which is defined by section=register (used a Tutorial):
if ('POST' == $_SERVER['REQUEST_METHOD']) {
if (!isset($_POST['Username'], $_POST['Password'])) {
return INVALID_FORM;
}
//Check for already existing User
$sql = 'SELECT
ID
FROM
User
WHERE
Username = ?
LIMIT
1';
$stmt = $db->prepare($sql);
if (!$stmt) {
return $db->error;
}
$stmt->bind_param('s', $Username);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows) {
return 'User already exists.';
}
$stmt->close();
//Add User
$sql = 'INSERT INTO
User(Username)
VALUES
(?)';
$stmt = $db->prepare($sql);
if (!$stmt) {
echo "hier bin ich";
return $db->error;
}
$stmt->bind_param('s', $Username);
if (!$stmt->execute()) {
return $stmt->error;
}
$UserID = $stmt->insert_id;
//Update password for User
$sql = 'UPDATE
User
SET
Password = ?
WHERE
ID = ?';
$stmt = $db->prepare($sql);
if (!$stmt) {
return $db->error;
}
$Hash = md5(md5($UserID).$Password);
$stmt->bind_param('si', $Hash, $UserID);
if (!$stmt->execute()) {
return $stmt->error;
}
return showInfo('Added user.');
}
return $ret;
It’s really strange. This code worked for me today. Then I had to change folders to get a better structure and now this.
Btw. this is my table User (ID = primary key):
!
http://imageshack.us/photo/my-images/823/unbenanntjfz.jpg/
It looks like your code is binding
$Usernameto your MySQL statements, which from the code provided is not defined.Perhaps you meant to bind
$_POST['Username']? Same thing with$Passwordlater in the script.