Spartan 117 Posted December 4, 2006 Share Posted December 4, 2006 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 editmysql_select_db("database"); //If cmd has not been initializedif(!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>"; }}?><?phpif($_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;">NewPassword:</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 Quote Link to comment Share on other sites More sharing options...
chiprivers Posted December 4, 2006 Share Posted December 4, 2006 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 Link to comment Share on other sites More sharing options...
Spartan 117 Posted December 4, 2006 Author Share Posted December 4, 2006 [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'. Quote Link to comment Share on other sites More sharing options...
chiprivers Posted December 4, 2006 Share Posted December 4, 2006 So you are happy that it is connects to the database?What is happening then when you run this script? Quote Link to comment Share on other sites More sharing options...
Spartan 117 Posted December 4, 2006 Author Share Posted December 4, 2006 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. Quote Link to comment Share on other sites More sharing options...
chiprivers Posted December 4, 2006 Share Posted December 4, 2006 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. Quote Link to comment Share on other sites More sharing options...
keeB Posted December 4, 2006 Share Posted December 4, 2006 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.. Quote Link to comment Share on other sites More sharing options...
Spartan 117 Posted December 5, 2006 Author Share Posted December 5, 2006 [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 Quote Link to comment Share on other sites More sharing options...
mansuang Posted December 5, 2006 Share Posted December 5, 2006 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 Quote Link to comment Share on other sites More sharing options...
Spartan 117 Posted December 5, 2006 Author Share Posted December 5, 2006 I did that and still nothing.That php works when I press submit, right? Or is there a different way I have to call it, because it is almost like it isn't even going to that source code when I click submit.Does it have to be within <form...> </form> tags? Quote Link to comment Share on other sites More sharing options...
mansuang Posted December 5, 2006 Share Posted December 5, 2006 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 Link to comment Share on other sites More sharing options...
Spartan 117 Posted December 5, 2006 Author Share Posted December 5, 2006 [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. Quote Link to comment Share on other sites More sharing options...
mansuang Posted December 5, 2006 Share Posted December 5, 2006 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] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.