$username = $_POST['username']; $name = $_POST['name']; $email = $_POST['email']; $password = $_POST['password']; $confirmpassword = $_POST['confirmpassword']; $time = time(); if($password != $confirmpassword) { echo '<meta http-equiv="refresh" content="0; url=?error=1" />'; die(); } else { if($username != '') { echo '<meta http-equiv="refresh" content="0; url=?error=2" />'; die(); } else { $checkemail = $db->prepare("SELECT * FROM `member` WHERE `email` = ?"); $checkemail->bind_param('s', $email); $checkemail->execute(); $checkemail->store_result(); $emailrows = $checkemail->num_rows; $checkemail->free_result(); if($emailrows > '0') { echo '<meta http-equiv="refresh" content="0; url=?error=3" />'; die(); } else { $checkdisplay = $db->prepare("SELECT * FROM `member` WHERE `display` = ?"); $checkdisplay->bind_param('s', $name); $checkdisplay->execute(); $checkdisplay->store_result(); $displayrows = $checkdisplay->num_rows; $checkdisplay->free_result(); if($displayrows > '0') { echo '<meta http-equiv="refresh" content="0; url=?error=4" />'; die(); } else { if (!preg_match("/([w-]+@[w-]+.[w-]+)/",$email)) { echo '<meta http-equiv="refresh" content="0; url=?error=5" />'; die(); } else { $createaccount = $db->prepare("INSERT INTO `member` (`display`, `email`, `salt`, `password`, `rank`, `joined`, `ip`) VALUES (?, ?, ?, ?, ?, ?, ?)"); $ip = $_SERVER['REMOTE_ADDR']; $salt = sha1(rand(10000000, 99999999)); $encrypt = sha1($password . $salt); $rank = '1'; $createaccount->bind_param('ssssiis', $name, $email, $salt, $encrypt, $rank, $time, $ip); $createaccount->execute(); $getid = $db->prepare("SELECT `id` FROM `member` WHERE `display` = ?"); $getid->bind_param('s', $name); $getid->execute(); $getid->bind_result($userid); $getid->free_result(); if(!$userid) { echo '<meta http-equiv="refresh" content="0; url=?error=6" />'; die(); } else { $encryptip = sha1($ip); session_regenerate_id(); $_SESSION['sesid'] = $userid; $_SESSION['sesip'] = $encryptip; session_write_close(); ob_get_clean(); echo 'success'; echo $userid; echo $encryptip; } } } } } }
I’m just learning the ropes of MySQLi, so got this mostly from a tutorial (though had to throw in parts from different sites – hence the errors).
Is anyone able to explain why $userid returns 0
instead of 1
that it should be? I’m hoping it’s a simple mistake.
The query is correct, I’ve looked over it multiple times, and yes, $name
is set.
Advertisement
Answer
Strictly speaking, you don’t need to select user id this way. As you just inserted data in the table, then you have call insert_id() function to get autogenerated id. That’s all.
As of such “problems” in general – again: you have to isolate that code snippet, add code to verify every single one premise (var_dumping input, selecting raw DB content without conditions, comparing data from db with input (I mean code that compares and outputs the result)) and – if still unable to solve – be able to post both code along with output in your question, to make it sane and answerable.
You have to understand that code doesn’t exist in vacuum. It runs on the server with it’s distinct environment, it uses a database, it processes some input – ALL THESE THINGS TOTALLY UNKNOWN FOR THE READER – and thus it makes a question that consists of only code totally useless.