Jump to content

Quick Password Changing Script - RESOLVED


Spartan 117

Recommended Posts

Hello, I have found this code that changes a filed in mysql, but it doesn't seem to work. I do it and nothing happens. Could someone maybe look it over for me and help me make it work?

[code="php"]
Change Administrator Password: </h3>
                <div align="center">
<?php
mysql_connect("domain","username","password");
//select which database you want to edit
mysql_select_db("database");

//If cmd has not been initialized
if(!isset($cmd))
{
  //display all the links
  $result = mysql_query("select * from members order by id desc");
 
  //run the while loop that grabs all the news scripts
  while($r=mysql_fetch_array($result))
  {
      //grab the info
      $username=$r["username"];//take out the title
      $id=$r["id"];//take out the id
   
//make the title a link
      echo "<a href='password.php?cmd=edit&id=$id'>$username - Edit</a>";
      echo "<br>";
    }
}
?>
<?php
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
  if (!isset($_POST["submit"]))
  {
      $id = $_GET["id"];
      $sql = "SELECT * FROM members WHERE id=$id";
      $result = mysql_query($sql);       
      $myrow = mysql_fetch_array($result);
      ?>
 
<form action="password.php" method="post">
      <input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
      <input type=hidden name="name" id="username" value="<?php echo $myrow["username"] ?>">
  <table style="text-align: left; width: 428px;" border="0"
cellpadding="0" cellspacing="0">
    <tbody>
      <tr>
        <td style="width: 215px;">New
Password:</td>
        <td style="width: 209px;"><input name="password" type="password" id="password" value="" size="30"></td>
      </tr>
      <tr>
        <td style="width: 215px;"></td>
        <td
style="text-align: left; vertical-align: top; width: 209px;"><input type="hidden" name="cmd" value="edit"> 
      <input type="submit" name="submit" value="Change Password"></td>
      </tr>
    </tbody>
  </table>
</form>
<?php } ?>
<?php
  if ($_POST["$submit"])
  {
          $name = $_POST["username"];
  $password = $_POST["password"];  
  $sql = "UPDATE members SET username='$name',password='$password' WHERE id=$id";
      //replace link with your table name above
      $result = mysql_query($sql);
      echo "Login Info Updated!";
}
}
?>
[/code]

I was hoping to use it as a fast password changing script...

If someone can help me it would be great.

Thanks
Link to comment
Share on other sites

Can you confirm that you have a database called 'database' with a table called 'members' with columns 'username', 'password' and 'id'?

Also where you connect to your database:

[code]
mysql_connect("domain","username","password");
[/code]

are these literal values? or should they be strings in which case you missed out the $.  Also if they are strings, where are they set?
Link to comment
Share on other sites

[quote author=chiprivers link=topic=117295.msg478426#msg478426 date=1165243176]
Can you confirm that you have a database called 'database' with a table called 'members' with columns 'username', 'password' and 'id'?

Also where you connect to your database:

[code]
mysql_connect("domain","username","password");
[/code]

are these literal values? or should they be strings in which case you missed out the $.  Also if they are strings, where are they set?
[/quote]

I used those just so I wouldn't have to give everyone on these forums the info needed to connect to my database...

I do have a table called members with columns 'username', 'password', and 'id'.
Link to comment
Share on other sites

When I type into the inputbox the new password and click submit the page reloads and it doesn't show anything except for the header. It is suppose to echo "Login Info Updated" when it is finished. So then I checked in phpMyAdmin and the password field was the same as it was before, so it didn't even change it.
Link to comment
Share on other sites

What I do when I have trouble with a long script that is not working and I can not trace the fault, I put some echo"TEST"; statements in my script at different places to see how far the script is running. If the TEST is echoed to the browser then the script had got that far.
Link to comment
Share on other sites

Replace
[code]
<?php
  if ($_POST["$submit"])
  {
          $name = $_POST["username"];
  $password = $_POST["password"];  
  $sql = "UPDATE members SET username='$name',password='$password' WHERE id=$id";
      //replace link with your table name above
      $result = mysql_query($sql);
      echo "Login Info Updated!";
}
?>
[/code]

with:

[code]
<?php
  if ($_POST["$submit"])
  {
          $name = $_POST["username"];
  $password = $_POST["password"];  
  $sql = "UPDATE members SET username='$name',password='$password' WHERE id=$id";
      //replace link with your table name above
      $result = mysql_query($sql) or die("There was an error: " . mysql_error() . "<br>Here is the query: " . $sql;
      echo "Login Info Updated!";
}
?>
[/code]

Also, please note: [code=php:0]if ($_POST["$submit"])[/code] probably dont want the $ there..
Link to comment
Share on other sites

[quote author=keeB link=topic=117295.msg478527#msg478527 date=1165251347]
Also, please note: [code=php:0]if ($_POST["$submit"])[/code] probably dont want the $ there..
[/quote]

Where wouldn't I want the $? In the submit right?

I have been screwing around with this for like 30 minutes, I have even tried echoing test and stuff. I cannot get it to work. I am getting a little annoyed  :'(...

Nobody sees anything wrong with it at all? Could someone please head me in the direction of some good tutorials on what I would need to figure this out please? Or maybe can someone post the most basic way to change a field (password) in a table of mysql with step by step "this does this, this does that"? Either way it would be great.

Thanks
Link to comment
Share on other sites

kindly check your php.ini setting.
if "register_global" is "Off" you may need to add [code=php:0] $id = $_POST["id"] [/code] in your script.

To make sure that your query run correctly.
just add [code=php:0]echo $sql; exit; [/code] to check your query.

-----------------------
<?php
   if ($_POST["$submit"])
   {
          $name = $_POST["username"];
  $password = $_POST["password"];
               [code=php:0]$id = $_POST["id"];[/code] 
  $sql = "UPDATE members SET username='$name',password='$password' WHERE id=$id";
[code=php:0]
       //check the query, Remove it when you make sure that the query is correct
       echo $sql;
       exit;
[/code]

      //replace link with your table name above
      $result = mysql_query($sql);
      echo "Login Info Updated!";
}
}
?>
-----------------------------

Hope it helps
Link to comment
Share on other sites

[quote author=mansuang link=topic=117295.msg478910#msg478910 date=1165294133]
Did you see some query like "[color=orange]UPDATE members SET username='some_username',password='some_password' WHERE id=id_number[/color]" ?
or nothing happen.



[/quote]

I didn't get that until I removed the $ from $submit like someone suggested earlier, I made that change earlier with another and I thought it was wrong because I got an error, it must have been whatever the other thing was I changed. I got it working now though.

Thank you all for helping me, I am sorry for being a noob  ;D I have learned a little more php thanks to you.
Link to comment
Share on other sites

Try this

[code]
<?php
//$_POST["$submit"] -> $_POST["id"] cuz' when you press enter to submit form the "submit" value is null
  if ($_POST["id"])
  {
         $name = $_POST["name"]; //Change $_POST["username"] -> $_POST["name"] (same as form field)
 $password = $_POST["password"];  
              $id = $_POST["id"]; //Add This line
 $sql = "UPDATE members SET username='$name',password='$password' WHERE id=$id";
     //replace link with your table name above
     $result = mysql_query($sql);
     echo "Login Info Updated!";
}
}
?>
[/code]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.