major trouble matching selecting most current row
I am having major trouble selecting the most current/matching row in my database.
F开发者_如何学JAVAor example:
$query = "SELECT * FROM `Password_Reset`";
$request = mysql_query($query,$connection) or die(mysql_error());
$result = mysql_fetch_array($request);
$result['token']
is taking the first row's no matter what every time the query is run.
I am pretty sure it has to do with not being specific enough with my select query but I have not found a way to match it up.
to be even MORE specific. This is the whole query:
$query = "SELECT * FROM `Password_Reset`";
$request = mysql_query($query,$connection) or die(mysql_error());
$result = mysql_fetch_array($request);
$token = $result['token'];
$alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcedfghijklmnopqrstuvwxyz1234567890";
$rand = str_shuffle($alpha);
$salt = substr($rand,0,40);
$hashed_password = sha1($salt . $_POST['Password']);
$user_email = $result['email'];
// these should match, and do so every where else on my other pages and in the db, I'm just not able to pull the corresponding $token. its taking the $token in the first row every time.
if($get_token == $token) {
header("Location: http://www.cysticlife.org/index.php");
exit;
}else{
if(empty($_POST['Password'])) {
$valid = false;
$error_msgs[] = 'Whoops! You must enter a password.';
}
if($_POST['Password'] != $_POST['passwordConfirm'] || empty($_POST['Password'])) {
$valid = false;
$error_msgs[] = "Your password entries didn't match...was there a typo?";
}
if($valid) {
$query = "UPDATE `cysticUsers` SET `encrypted_password` = '$hashed_password' WHERE `Email` = '$user_email'";
mysql_query($query,$connection);
}
}
}
thanks in advance
If you want to order your rows by when they were submitted to the database, you will need to add an ID-Field to your table, if you haven't got one already.
It will probably be the easiest way to add an field called id
to the table, set it as index and use auto_increment
. Then, you can simply order your rows by ID:
SELECT * FROM `Password_Reset` ORDER BY `id` ASC
Btw, if you only need the first row, it's good to add a LIMIT 1
to your query, for better performance.
精彩评论