Welcome to Dream.In.Code
Become a PHP Expert!

Join 150,170 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,297 people online right now. Registration is fast and FREE... Join Now!




Changing password script with mysql and php

 
Reply to this topicStart new topic

Changing password script with mysql and php

scottyadam
23 Aug, 2008 - 11:50 AM
Post #1

New D.I.C Head
*

Joined: 3 Dec, 2007
Posts: 49


My Contributions
Ok...so I have a partial CMS programmed but for some reason I cannot update the database with the password...

To start off simple...I was trying to set some variables.

CODE

$member_id=($_SESSION['SESS_MEMBER_ID']);
$username = ($_SESSION['SESS_MEMBER_USERNAME']);
$passwd = ($_SESSION['SESS_MEMBER_PASSWORD']);
$oldpasswd = $_POST['foldpasswd'];
$confirmpasswd = $_POST['fconfirmpasswd'];


I know that doing...

<?php echo $_SESSION['SESS_MEMBER_ID'] ?>

will print the current member's username!

but when I put $_SESSION['SESS_MEMBER_ID'] into a var $member_id and do...

<?php echo $member_id ?>

there is nothing printed...what is wrong??
User is offlineProfile CardPM
+Quote Post

Moonbat
RE: Changing Password Script With Mysql And Php
23 Aug, 2008 - 11:55 AM
Post #2

D.I.C Regular
Group Icon

Joined: 30 Jun, 2008
Posts: 399



Thanked: 23 times
Dream Kudos: 600
My Contributions
Why do you use parentheses around the variables?
User is offlineProfile CardPM
+Quote Post

JBrace1990
RE: Changing Password Script With Mysql And Php
23 Aug, 2008 - 12:05 PM
Post #3

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 479



Thanked: 24 times
Dream Kudos: 350
My Contributions
using parentheses may mess it up (though i doubt it)... the most likely problem is that you're not using a ; after the echo $member_id
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Changing Password Script With Mysql And Php
23 Aug, 2008 - 12:25 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Include the semicolon and also double quotes around the variable.

php

<?php echo "$member_id"; ?>


The double quotes forces the variable to evaluated and then it will echo the value. The semicolon is needed to end any statement in PHP and is required.

See if that works for you. smile.gif
User is offlineProfile CardPM
+Quote Post

scottyadam
RE: Changing Password Script With Mysql And Php
23 Aug, 2008 - 03:41 PM
Post #5

New D.I.C Head
*

Joined: 3 Dec, 2007
Posts: 49


My Contributions
So now that I have that sorted out fine...here is my code for the project that I am putting together:

This is basically a form that will change a user's password my updating the mysql database and will validate fields

CODE
<?php
    require_once('auth.php');

    // Get global variable values if there are any
    if (isset($_POST['submit']))
    {
        $usrname = ($_SESSION['SESS_MEMBER_USERNAME']);
        $passwd = ($_SESSION['SESS_MEMBER_PASSWORD']);
        $oldpasswd = $_POST['oldpasswd'];
        $newpasswd = $_POST['newpasswd'];
        $cfmpasswd = $_POST['cfmpasswd'];
    }
    if ($submit)
    {
        // Check if Old password is the correct
        if ($oldpasswd != $passwd)
        { echo "Old password is wrong!"; exit; }
        
        // Check if New password if blank
        if (trim($newpasswd) == "")
        { echo "New password cannot be blank!"; exit; }
                
        // Check if New password is confirmed
        if ($newpasswd != $cfmpasswd)
        { print "New password was not confirmed!"; exit; }
         }
        // If everything is ok, use auth class to modify the record
        $query = "UPDATE smw_members ".
        "SET usrname = '$usrname', newpasswd = '".md5($_POST['newpasswd'])."' ".
        "WHERE member_id = '$member_id'";
        mysql_query($query) or die('Error : ' . mysql_error());
?>
<meta http-equiv="REFRESH" content="0;url=http://www.google.ca">
<html>
<head>
  <title>Scotty Media Content Management System</title>
  <?php include("scripts.php"); ?>
</head>
<body>
  <table border="1" width="1000" align="center" cellpadding="0" cellspacing="0">
   <tr valign="top">
    <?php include("itop_bar.php"); ?>
   </tr>
   <tr valign="top">
    <td width="213" bgcolor="#404040">
     <?php include("iside_bar.php"); ?>
    </td>
    <td width="787">
     <table border="0" width="100%">
      <tr>
       <?php include("iquick_bar.php"); ?>
      </tr>
      <tr>
       <td valign="top" colspan="2">
        <hr align="left" width="770" align="center">
       </td>
      </tr>
      <tr>
       <td valign="top" class="txt_main" colspan="2">
        <form method="POST" action="chgpwd.php">
        <table>
         <tr>
          <td width="30%">Old Password:</td>
          <td width="70%"><input type="password" name="oldpasswd"></td>
         </tr>
         <tr>
          <td width="30%">New Password:</td>
          <td width="70%"><input type="password" name="newpasswd" size="25"></td>
         </tr>
         <tr>
          <td width="34%">Confirm:</td>
          <td width="66%"><input type="password" name="cfmpasswd" size="25"></td>
         </tr>
         <tr>
          <td width="100%" colspan="2" align="center"><input type="submit" value="Change Password" name="submit"></td>
         </tr>
        </table>
        </form>
       </td>
      </tr>
     </table>
    </td>
   </tr>
   <tr>
    <?php include("ibottom_bar.php"); ?>
   </tr>
  </table>
</body>
</html>


When I run the code the password is not changed in the database...what is wrong!
User is offlineProfile CardPM
+Quote Post

JBrace1990
RE: Changing Password Script With Mysql And Php
23 Aug, 2008 - 04:00 PM
Post #6

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 479



Thanked: 24 times
Dream Kudos: 350
My Contributions
$submit is not set... neither is $member_id
User is offlineProfile CardPM
+Quote Post

CTphpnwb
RE: Changing Password Script With Mysql And Php
23 Aug, 2008 - 04:03 PM
Post #7

D.I.C Regular
Group Icon

Joined: 8 Aug, 2008
Posts: 481



Thanked: 32 times
My Contributions
I think it's failing on the query. Try it without quotes around $memberid:

CODE
$query = "UPDATE smw_members SET usrname = '$usrname', newpasswd = '".md5($_POST['newpasswd'])."' WHERE member_id = ".$member_id;


This post has been edited by CTphpnwb: 23 Aug, 2008 - 04:04 PM
User is offlineProfile CardPM
+Quote Post

JBrace1990
RE: Changing Password Script With Mysql And Php
23 Aug, 2008 - 04:06 PM
Post #8

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 479



Thanked: 24 times
Dream Kudos: 350
My Contributions
if he didn't concatenate the strings mid-query, it'd be easier to see >_>> but you need single quotes around the variable, otherwise it looks for a column with that name...
User is offlineProfile CardPM
+Quote Post

CTphpnwb
RE: Changing Password Script With Mysql And Php
23 Aug, 2008 - 04:14 PM
Post #9

D.I.C Regular
Group Icon

Joined: 8 Aug, 2008
Posts: 481



Thanked: 32 times
My Contributions
I see what you mean.

I just looked at a script of mine that works though, and I have instances where it works both with and without the single quotes:
CODE
$classdata = mysql_query("SELECT * FROM Classes where Classid=$newclass") or die(mysql_error());
$result = mysql_query("SELECT * FROM Teachers WHERE Teacherid = '$x'");// or die(mysql_error());

blink.gif
User is offlineProfile CardPM
+Quote Post

scottyadam
RE: Changing Password Script With Mysql And Php
24 Aug, 2008 - 09:25 AM
Post #10

New D.I.C Head
*

Joined: 3 Dec, 2007
Posts: 49


My Contributions
QUOTE(JBrace1990 @ 23 Aug, 2008 - 05:00 PM) *

$submit is not set... neither is $member_id



I understand the $member_id situation it is stored in the session but how do I set $submit...I just confused they heck out of myself here....could you give a little help with that...
User is offlineProfile CardPM
+Quote Post

JBrace1990
RE: Changing Password Script With Mysql And Php
24 Aug, 2008 - 10:59 AM
Post #11

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 479



Thanked: 24 times
Dream Kudos: 350
My Contributions
change it to if($_POST){}
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:10AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month