Jump to content

Is there anything wrong with my code?


jon4433

Recommended Posts

I've been reading up on how to change passwords for the past 30 minutes, and I have written a script for it, but when I go to test it...the submit button does nothing when it's pressed. Is there anything wrong with my code?

 

I haven't been able to test it cause of this problem, so i'm unsure if it works or not.

 

<?php

session_start();

if($_SESSION['username']){

if($_POST['submit']){	

$connect = mysql_connect("****","****","****") or die("Could not connect to database.");
mysql_select_db("****") or die ("Could not find database!");	

$current_password = md5 ($_POST['current_password']);
$new_password = md5 ($POST['new_password']);
$new_password1 = md5 ($_POST['new_password1']);

$user = $_SESSION['username'];

	if($current_password && $new_password && $new_password1){

	$query1 = mysql_query("SELECT password FROM login WHERE username ='$user'");
	while ($row = mysql_fetch_assoc($query1))
	{
		$dbpass = $row['password'];	
	}
	if($current_password = $dbpass)
	{
		if($new_password == $new_password1)
		{
			mysql_query("UPDATE login SET password='$new_password1' WHERE username ='$user'");	
			//die("Your password has been changed!");
		}
		else
			{
				echo "Please fill out all of the fields!";	
			}
		}
	}
}
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
body {
background-image: url(images/sky.png);
background-size:100% 100%;
background-attachment: fixed;
background-repeat: repeat-x;
background-position: left top;
background-color: #999999; 
}
body,td,th {
color: #000;
font-family: "MS Serif", "New York", serif;
}
</style>
</head>
<body>
<div id="wrap">
<!--Header-->
    <div id="header_member" align="center"> <a href="membersarea.php"><img src="images/member_header.png" width="700" height="100" /></a></div>
    
    	<!--Log out and time-->
        <div id="info">
		<div id="date"><script type="text/javascript"> 
		var currentDate = new Date()
	  	var day = currentDate.getDate()
	  	var month = currentDate.getMonth() + 1
	  	var year = currentDate.getFullYear()
	  	document.write("<b>" + day + "/" + month + "/" + year + "</b>")
	  	var currentTime = new Date()
		var hours = currentTime.getHours()
		var minutes = currentTime.getMinutes()</script>
	     </div> 
             
            
             
        		<div id="time"><script type="text/javascript"> 
			var suffix = "AM";
	  if (hours >= 12) {
	  suffix = "PM";
	  hours = hours - 12;
	  }
	  if (hours == 0) {
	  hours = 12;
	  }

	  if (minutes < 10)
	  minutes = "0" + minutes

	  document.write("<b>" + hours + ":" + minutes + " " + suffix + "</b>")</script>
          		</div>
          </div>
          
         <div id="logout"><center><?php echo "<a href='logout.php'>Log out.</a>";?></center></div> 
         
  <!--Main section which will contain everything else-->	
			 <div id="member_main">
             <!--Display user information from the database-->
       		   <div id="member_right"><center>
               		<div id="change_password" align="right">
                	<form method="POST">
                    	Current Password: <input type="password" name="current_password"/><br />
                        New Password: <input type="password" name="new_password"/><br />
                        Re-Type Password: <input type="password" name="new_password1"/><br />
                    </form>
                    	<div id="submit_button">
                        	<input type="submit" name="submit" value="Submit" />
                        </div>
                    	</div>
                    </div>
       		   </div>
               
               			
                
                <!--Welcome message-->
		   <div id="member_top" align="center"><?php echo "Welcome, ".$_SESSION['username'];?></div>
                    
                   <div id="member_left" align="center"><img src="images/navigation.png" width="105" height="30" /><a href="membersarea.php"><img src="images/home_member.png" width="105" height="30" /></a><a href="account.php"><img src="images/account.png" width="105" height="30" /></a><a href="chat.php"><img src="images/chat.png" width="105" height="30" /></a></div>
                 </div> 
                 
             			<!--About us-->
                        <div id="about" align="center"><a href="membersarea.php">Home</a> | <a href="">About Us</a> | <a href="">Contact Us</a> | <a href="">FeedBack</a></div>
             
<!--Footer-->          
<div align="center" id="footer_member" style="color: #FFF">Dawncraftmc© 2011 - 2012</div>   
        
</div>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/258330-is-there-anything-wrong-with-my-code/
Share on other sites

Oops, silly mistake there! It does submit now, but nothing works? I made a few changes to the code...

 

<?php

session_start();

if($_SESSION['username']){

if($_POST['submit']){	

//Set the variables
$current_password = md5 ($_POST['current_password']);
$new_password = md5 ($POST['new_password']);
$new_password1 = md5 ($_POST['new_password1']);	

$user = $_SESSION['username'];

//Connect to the db and select the db
$connect = mysql_connect("****","****","****") or die("Could not connect to database.");
mysql_select_db("****") or die ("Could not find database!");

//Select the password column from the db
$queryget = mysql_query("SELECT password FROM login WHERE username ='$user'") or die(mysql_error);
$row = mysql_fetch_assoc($queryget);

$oldpassworddb = $row['password'];

//Check if the current password matches the password in the db
if($current_password==$oldpassworddb)
{
		//Check if both of the new passwords match
		if($new_password==$new_password1)
		{
			//Change passwords in db
			$querychange = mysql_query("UPDATE login SET password = '$new_password' WHERE username ='$user'");

			//Password has been changed
			die("Your password has been changed!");
		}
		else
		{
			//New passwords do not match
			die("New passwords don't match!");	
		}
}
else
{
	//Old password does not match
	die("Old password doesn't match!");	
}

}
else
{
	//Relocate the user to the starting page
	header("location: suggestion.html");	
}
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
body {
background-image: url(images/sky.png);
background-size:100% 100%;
background-attachment: fixed;
background-repeat: repeat-x;
background-position: left top;
background-color: #999999; 
}
body,td,th {
color: #000;
font-family: "MS Serif", "New York", serif;
}
</style>
</head>
<body>
<div id="wrap">
<!--Header-->
    <div id="header_member" align="center"> <a href="membersarea.php"><img src="images/member_header.png" width="700" height="100" /></a></div>
    
    	<!--Log out and date/time-->
        <div id="info">
		<div id="date"><script type="text/javascript"> 
		var currentDate = new Date()
	  	var day = currentDate.getDate()
	  	var month = currentDate.getMonth() + 1
	  	var year = currentDate.getFullYear()
	  	document.write("<b>" + day + "/" + month + "/" + year + "</b>")
	  	var currentTime = new Date()
		var hours = currentTime.getHours()
		var minutes = currentTime.getMinutes()</script>
	     </div> 
             
            
             
        		<div id="time"><script type="text/javascript"> 
			var suffix = "AM";
	  if (hours >= 12) {
	  suffix = "PM";
	  hours = hours - 12;
	  }
	  if (hours == 0) {
	  hours = 12;
	  }

	  if (minutes < 10)
	  minutes = "0" + minutes

	  document.write("<b>" + hours + ":" + minutes + " " + suffix + "</b>")</script>
          		</div>
          </div>
          
         <div id="logout"><center><?php echo "<a href='logout.php'>Log out.</a>";?></center></div> 
         
  <!--Main section which will contain everything else-->	
			 <div id="member_main">
             <!--Display user information from the database-->
       		   <div id="member_right">
               		<div id="change_password" align="right">
                	<form method="POST">
                    	Current Password: <input type="password" name="current_password"/><br />
                        New Password: <input type="password" name="new_password"/><br />
                        Re-Type Password: <input type="password" name="new_password1"/><br /><br />
                        <center><input type="submit" name="submit" value="Change Password" /></center>
                    </form>
                    	</div>
                    </div>

                <!--Welcome message-->
		   <div id="member_top" align="center"><?php echo "Welcome, ".$_SESSION['username'];?></div>
                    
                    <!--Navigation bar-->
                   <div id="member_left" align="center"><img src="images/navigation.png" width="105" height="30" /><a href="membersarea.php"><img src="images/home_member.png" width="105" height="30" /></a><a href="account.php"><img src="images/account.png" width="105" height="30" /></a><a href="chat.php"><img src="images/chat.png" width="105" height="30" /></a></div>
                 </div> 
                 
             			<!--About us-->
                        <div id="about" align="center"><a href="membersarea.php">Home</a> | <a href="">About Us</a> | <a href="">Contact Us</a> | <a href="">FeedBack</a></div>
                   </div>
             
<!--Footer-->          
<div align="center" id="footer_member" style="color: #FFF">Dawncraftmc© 2011 - 2012</div>   
        
</div>

</body>
</html>

Place 'echo's in your code at strategic positions to verify your code is doing what you expect.

 

In this example, we have lots of nested if/else's. If things aren't working right, we have no way to check at what point our code isn't doing what we expect.

<?php

$foo = TRUE;
$bar = FALSE;
$number = 10;

if( $foo ) {
// Do something
if( $bar ) {
	// Do something else
} else {
	// Do something MORE
	if( $number > 20 ) {
		// more code
	} else {
		// alternate code
	}
}
} else {
// code code code
}

?>

 

So we add some echos to help trace our issue.

 

<?php

$foo = TRUE;
$bar = FALSE;
$number = 10;

if( $foo ) {
echo '$foo was true.';
// Do something
if( $bar ) {
	echo '$bar was true.';
	// Do something else
} else {
	echo '$bar was false.';
	// Do something MORE
	if( $number > 20 ) {
		echo '$number was more than 20.';
		// more code
	} else {
		echo '$number was less than 20.';
		// alternate code
	}
}
} else {
echo '$foo was false.';
// code code code
}

?>

 

Now, when we run the script, we know exactly what has/hasn't executed. Using this, we can isolate the exact spot that isn't functioning as we expect.

 

$foo was true.$bar was false.$number was less than 20.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.