What's new

Web [PHP][SQL] How to Replace text in a column with new text?

  • Thread starter Triiistan
  • Start date
  • Views 313
T

Triiistan

Contributor
Messages
1,951
Reaction score
496
I am currently trying to use:
Code:
$query = "UPDATE `users` SET `password` = replace(password,$current_pass_hash,$current_pass);";

Any ideas?
 
Z

Zerker24

Enthusiast
Messages
945
Reaction score
206
Are you just trying to update a user's password?

$query = "UPDATE users SET password='new pass' WHERE username='username'";

or if you have a user ID

$userID = 2 // Use whatever ID
$query = "UPDATE users SET password='new pass' WHERE uid=".$userID;
 
T

Triiistan

Contributor
Messages
1,951
Reaction score
496
Are you just trying to update a user's password?

$query = "UPDATE users SET password='new pass' WHERE username='username'";

or if you have a user ID

$userID = 2 // Use whatever ID
$query = "UPDATE users SET password='new pass' WHERE uid=".$userID;
Yeah, I'm making a page for a user to change their password. I asked for their current password, new password, and confirm new password.
 
Z

Zerker24

Enthusiast
Messages
945
Reaction score
206
Yeah, I'm making a page for a user to change their password. I asked for their current password, new password, and confirm new password.

Okay, try something like this assuming that $password is the one the user inputs after the user types them, and $current_pass is loaded from the DB.

Code:
if ($current_pass == $password)
{
    $query = "UPDATE users SET password='new pass' WHERE username='username'";
    //Run query here
}
else
    die("Invalid password, please try again.");
 
T

Triiistan

Contributor
Messages
1,951
Reaction score
496
Okay, try something like this assuming that $password is the one the user inputs after the user types them, and $current_pass is loaded from the DB.

Code:
if ($current_pass == $password)
{
    $query = "UPDATE users SET password='new pass' WHERE username='username'";
    //Run query here
}
else
    die("Invalid password, please try again.");
I got it to work, thanks! However, I am trying to make a success page, where is says text, waits a little, then logs em out. How could I make this work? Thanks! :smile:

PHP:
<?php
if(sleep(3)){
    echo 'Your password was changed successfully, you will now be logged out.';
    header('Location: logout.php');
}
?>
 
Z

Zerker24

Enthusiast
Messages
945
Reaction score
206
I got it to work, thanks! However, I am trying to make a success page, where is says text, waits a little, then logs em out. How could I make this work? Thanks! :smile:

PHP:
<?php
if(sleep(3)){
    echo 'Your password was changed successfully, you will now be logged out.';
    header('Location: logout.php');
}
?>

try this

PHP:
<?php
header( "refresh:3;url=wherever.php" );
echo 'Your password was changed successfully, you will now be logged out.';
?>
 
T

Triiistan

Contributor
Messages
1,951
Reaction score
496
try this

PHP:
header( "refresh:3;url=wherever.php" );
echo 'Your password was changed successfully, you will now be logged out.';
Thanks, I can tell that it would work, except I don't think my code is allowing it. I checkout out my logout.php page and this is what it contains. It is not going back to index.php, it just keeps refreshing every few seconds.

logout.php
PHP:
<?php
require 'core.inc.php';
session_destroy();
header('Location: '.$http_referer);
?>
 
Z

Zerker24

Enthusiast
Messages
945
Reaction score
206
Thanks, I can tell that it would work, except I don't think my code is allowing it. I checkout out my logout.php page and this is what it contains. It is not going back to index.php, it just keeps refreshing every few seconds.

logout.php
PHP:
<?php
require 'core.inc.php';
session_destroy();
header('Location: '.$http_referer);
?>

That is most likely because either you do not have $http_referer declared correctly, or because it is being set to the logout page when it is loaded. maybe try and change that to index.php?
 
T

Triiistan

Contributor
Messages
1,951
Reaction score
496
That is most likely because either you do not have $http_referer declared correctly, or because it is being set to the logout page when it is loaded. maybe try and change that to index.php?
Eh... I tried doing something like this, but I keep getting "no work". (Haha, I know that is improper English, but I'm lazy)

PHP:
<?php
require 'core.inc.php';
session_destroy();
if($http_referer=='http://localhost/Reminder/settings.php'){
    echo 'IT WORKED!';//header('Location: index.php');
} else {
    echo 'no work!';//header('Location: '.$http_referer);
}
 
Z

Zerker24

Enthusiast
Messages
945
Reaction score
206
Eh... I tried doing something like this, but I keep getting "no work". (Haha, I know that is improper English, but I'm lazy)

PHP:
<?php
require 'core.inc.php';
session_destroy();
if($http_referer=='http://localhost/Reminder/settings.php'){
    echo 'IT WORKED!';//header('Location: index.php');
} else {
    echo 'no work!';//header('Location: '.$http_referer);
}
Echo $http_referer and see if it is actually declared right. If it is, see where it is sending you.
 
Z

Zerker24

Enthusiast
Messages
945
Reaction score
206
header('Location: '.$http_referer); should work then... Does the settings.php file redirect you at all?
 
T

Triiistan

Contributor
Messages
1,951
Reaction score
496
header('Location: '.$http_referer); should work then... Does the settings.php file redirect you at all?
Yes, if you mean like this:

PHP:
if($query_run = mysql_query($query)){
                    header('Location: change_pass.php');
                } else {
                    echo 'Sorry, we couldn\'t change your password at this time. Please try again later.';
                }
 
Z

Zerker24

Enthusiast
Messages
945
Reaction score
206
Yes, if you mean like this:

PHP:
if($query_run = mysql_query($query)){
                    header('Location: change_pass.php');
                } else {
                    echo 'Sorry, we couldn\'t change your password at this time. Please try again later.';
                }

I am going to go out on a limb here, and say change_pass.php redirects to the confirmation page? I think you have a redirect loop going.

The confirmation page redirects to settings, which redirects to change_pass, which redirects to the confirmation page. Am I correct?
 
T

Triiistan

Contributor
Messages
1,951
Reaction score
496
Yeah, change_pass.php redirects to logout.php

change_pass.php
PHP:
<?php
require 'core.inc.php';
echo $http_referer;
header("refresh:3;url=logout.php");
echo 'Your password was changed successfully, you will now be logged out.';
?>

logout.php
PHP:
<?php
require 'core.inc.php';
session_destroy();
header('Location: '.$http_referer);
?>

settings.php
PHP:
<?php
require 'connect.inc.php';
require 'core.inc.php';
 
if(loggedin()){
 
$firstname = getuserfield('firstname');
$lastname = getuserfield('lastname');
$email = getuserfield('email');
echo 'You\'re logged in, '.$firstname.' '.$lastname.'. <a href="logout.php">Log out</a> <a href="index.php">Back</a><br><h1>Settings</h1><br>';
 
if(isset($_POST['current_pass']) && isset($_POST['new_pass']) && isset($_POST['confirm_pass'])){
    $current_pass = $_POST['current_pass'];
    $new_pass = $_POST['new_pass'];
    $confirm_pass = $_POST['confirm_pass'];
    $old_pass_hash = getuserfield('password');
    $current_pass_hash = md5($current_pass);
    $new_pass_hash = md5($new_pass);
    $user_id = getuserfield('id');
   
    if($old_pass_hash!=$current_pass_hash){
        echo 'Your current password that you gave is incorrect.';
    } else {
        if($new_pass!=$confirm_pass){
            echo 'Passwords do not match.';
        } else {
            if($current_pass==$new_pass){
                echo 'Choose a password you haven\'t previously used with this account.';
            } else {
                $query = "UPDATE `users` SET `password` = '".mysql_real_escape_string($new_pass_hash)."' WHERE `id`='".mysql_real_escape_string($user_id)."' ;";
                if($query_run = mysql_query($query)){
                    header('Location: change_pass.php');
                } else {
                    echo 'Sorry, we couldn\'t change your password at this time. Please try again later.';
                }
               
            }
        }
    }
   
}
 
 
?>
 
<!DOCTYPE html>
<html>
 
<head>
<title>Settings | <?php if(isset($email)) { echo $email; } ?></title>
</head>
 
<body>
<br>
<h3>Change Password</h3>
 
<form action="#" method="POST">
Current password:<br><input type="password" name="current_pass"><br><br>
New password:<br><input type="password" name="new_pass"><br><br>
Confirm new password:<br><input type="password" name="confirm_pass"><br><br>
<input type="submit" value="Change Password">
</form>
 
</body>
 
</html>
<?php   
} else if(!loggedin()) {
    echo 'You must be logged in to view this page.'.' <a href="index.php">Back</a>';
}
?>

I added some of my code, in case you had a question about anything. :smile:
 
Z

Zerker24

Enthusiast
Messages
945
Reaction score
206
PHP:
if($query_run = mysql_query($query)){
                    header('Location: change_pass.php');
                } else {
                    echo 'Sorry, we couldn\'t change your password at this time. Please try again later.';
                }

well, this will never be false. I would change this to

PHP:
mysql_query($query) or die("Sorry, we couldn\'t change your password at this time. Please try again later.");
header('Location: change_pass.php');

But other than that, I do not see anything that would cause this loop... I will read through it more later, I need to get ready for work.
 
T

Triiistan

Contributor
Messages
1,951
Reaction score
496
PHP:
if($query_run = mysql_query($query)){
                    header('Location: change_pass.php');
                } else {
                    echo 'Sorry, we couldn\'t change your password at this time. Please try again later.';
                }

well, this will never be false. I would change this to

PHP:
mysql_query($query) or die("Sorry, we couldn\'t change your password at this time. Please try again later.");
header('Location: change_pass.php');

But other than that, I do not see anything that would cause this loop... I will read through it more later, I need to get ready for work.
Oh, thanks a lot, I'm in no rush, this is my own personal project. Thanks a lot. :smile:
 
Top Bottom