Results 1 to 2 of 2

Thread: MySQL, Changing a value of a field[RESOLVED]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2005
    Posts
    131

    Resolved MySQL, Changing a value of a field[RESOLVED]

    I am writing a web program and I need to have a way for the admin to change either the admin password or the standard password (user name view)

    I need to know what I have to do with my code to get it working properly.

    Or, maybe someone has code already where I just have to change the names of the fields? Im sure it is simple code, it just has to determine which submit button was clicked and then use the proper query to update the password field.

    Please help

    VB Code:
    1. <?php
    2. include("cn.php");
    3. include("common.php");
    4. checklogin();
    5.  
    6. $msg = "";
    7. $password = "";
    8.  
    9.  
    10.  
    11.  
    12. $_POST['Submit']
    13. {
    14.  
    15.     $password = $_POST['password'];
    16.    
    17.    
    18.    
    19.         $result = mysql_query("Update admin set password='$password');
    20.         $msg="Admin Password Changed"
    21.    
    22. }
    23. ?>
    24. <html>
    25. <head>
    26. <title>Administration</title>
    27. </head>
    28.  
    29. <body bgcolor="#BCCDCC">
    30.  
    31. <table width="76%" border="1" align="center" cellpadding="1" cellspacing="1" bgcolor="#FFFFFF">
    32.   <tr>
    33.     <td width="3%" bgcolor="#CCCCCC"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">&nbsp;</font></td>
    34.     <td width="97%" bgcolor="#CCCCCC"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Records</strong></font></td>
    35.   </tr>
    36.   <tr>
    37.     <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif">&nbsp;</font></td>
    38.     <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif">&nbsp;</font></td>
    39.   </tr>
    40.   <tr>
    41.     <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif">&nbsp;</font></td>
    42.     <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif"><a href="newrecord.php">Addnew
    43.       Record </a></font></td>
    44.   </tr>
    45.   <tr>
    46.     <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif">&nbsp;</font></td>
    47.     <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif"><a href="listrecord.php">Edit/View
    48.       Records</a></font></td>
    49.   </tr>
    50.   <tr>
    51.     <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">&nbsp;</font></td>
    52.     <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">&nbsp;</font></td>
    53.   </tr>
    54.  
    55. </table>
    56. <form name="form1" method="post" action="">
    57.  
    58.     <table width="48%" border="1" align="center" cellpadding="1" cellspacing="1" bordercolor="#000000">
    59.    <tr><td colspan="2">Change Admin Password</td></tr>
    60.     <tr>
    61.       <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">New Password</font></td>
    62.       <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">
    63.         <input name="password" type="password" id="password">
    64.         </font></td>
    65.     </tr>
    66.     <tr>
    67.       <td>&nbsp;</td>
    68.       <td><input type="submit" name="Submit" value="Submit"></td>
    69.     </tr>
    70.   </table>
    71. <p>&nbsp;</p></form>
    72. <form name="form1" method="post" action="">
    73.  
    74.     <table width="48%" border="1" align="center" cellpadding="1" cellspacing="1" bordercolor="#000000">
    75.    <tr><td colspan="2">Change View Password</td></tr>
    76.     <tr>
    77.       <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">New Password</font></td>
    78.       <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">
    79.         <input name="password" type="password" id="password">
    80.         </font></td>
    81.     </tr>
    82.     <tr>
    83.       <td>&nbsp;</td>
    84.       <td><input type="submit" name="Submit" value="Submit"></td>
    85.     </tr>
    86.   </table>
    87. <p>&nbsp;</p></form>
    88. </body>
    89. </html>
    Last edited by espylacopa; May 21st, 2005 at 07:34 PM.
    Things fall apart which the center cannot hold...

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: MySQL, Changing a value of a field[RESOLVED]

    Your code seems to be somewhat incomplete. You are missing an if statment and you are also not ensuring that meta characters in the password have been escaped and that the mysql query was successful.

    Try this code:
    PHP Code:
    if(isset($_POST['Submit'])) // test if the variable exists - it won't if the form wasn't submitted
    {
        
    /* check if magic quotes is on and undo its actions if it is, then
           escape meta characters in the password string
         */
        
    if (get_magic_quotes_gpc()) {
            
    $password stripslashes($_POST['password']);
        } else {
            
    $password $_POST['password'];
        }

        
    $password mysql_escape_string($password);

        
    $query "UPDATE admin SET `password`='$password'";
        
        if (
    mysql_query($query)) {
        
    $msg'Admin Password Changed';
        } else {
            
    $msg 'Failed to change admin password: ' mysql_error();
        }

    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width