Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Aug 2011
    Location
    London
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form to update MYSQL database does not work.

    I have no idea why this is not working, I want it to update the `name` database located in PHPMyAdmin, with a user defined name.

    Code:
    PHP Code:
    <?php
    include 'config.php';
    ?>
    <div align="left">Welcome too the profile page, here you can change your email and password and other features!</div>
    <br />
    <br />
    <br />
    <div align="left"><strong>Change Name:</strong>
    <br /><input type="text" name="userchange" value=""><input type="submit" name="changename" value="Change">
    <?php
    if (ISSET($_POST['changename'])){
        
    $cusername $_POST['userchange'];
    function 
    change_username($cusername){
        
    $cusername mysql_real_escape_string(htmlentities($cusername));
        
    $cname mysql_real_escape_string($cusername);
        
    mysql_query("UPDATE users SET name = $cname ");
        }
    }
    ?>

  • #2
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,307
    Thanks
    4
    Thanked 205 Times in 202 Posts
    Where is your <form> tag? Where is the function change_username being called?

    Even if you had a form tag right now and the post value named changename were set the only thing that would happen is that the value of $_POST['userchange'] would end up being put into the variable named $cusername. Nothing else will happen.
    Dave .... HostMonster for all of your hosting needs

  • #3
    New Coder
    Join Date
    Aug 2011
    Location
    London
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've changed the code, but it still don't work

    PHP Code:
    <?php
    include 'config.php';
    ?>
    <div align="left">Welcome too the profile page, here you can change your email and password and other features!</div>
    <br />
    <br />
    <br />
    <div align="left"><strong>Change Name:</strong>
    <br /><form><input type="text" name="userchange" value=""><input type="submit" name="changename" value="Change">
    </form>
    <?php
    if (ISSET($_POST['changename'])){
        
    $cusername $_POST['userchange'];
    function 
    change_username($cusername){
        
    $cusername mysql_real_escape_string(htmlentities($cusername));
        
    $cname mysql_real_escape_string($cusername);
        
    mysql_query("UPDATE `users` SET `name` = '{$cusername}' WHERE `name`='OLDUSERNAME'");
        }
    }
    ?>

  • #4
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post
    You are not putting enough information into your form tag, it needs at least an action (what/where the form does/goes) and method (get or post), and is common practice to name them (for grabbing elements by id if you want to use javascript of any kind)

    Here is my revised code:
    Code:
    <?php
    include 'config.php';
    ?>
    <div align="left">Welcome too the profile page, here you can change your email and password and other features!</div>
    <br />
    <br />
    <br />
    <div align="left"><strong>Change Name:</strong>
    <br /><form action = '<?php echo $_SERVER['PHP_SELF']; ?>' method = 'POST' name = 'usernamechange'><input type="text" name="userchange" value=""><input type="submit" name="changename" value="Change">
    </form>
    <?php
    if (ISSET($_POST['changename'])){
        $cusername = $_POST['userchange'];
    function change_username($cusername){
        $cusername = mysql_real_escape_string(htmlentities($cusername));
        $cname = mysql_real_escape_string($cusername);
        mysql_query("UPDATE `users` SET `name` = '{$cusername}' WHERE `name`='OLDUSERNAME'");
        }
    }
    ?>
    I'm assuming you are omitting your database connection? If you aren't, you are going to need to connect to the database before attempting to update any values.

    Also, if you ever want to control what is displayed, I suggest you grab all of your session, cookie, get, post variables first, so you can manipulate what is displayed afterwards.
    Last edited by perpl3x3d; 09-11-2011 at 07:39 PM.

  • #5
    New Coder
    Join Date
    Aug 2011
    Location
    London
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This still does not work, and I've included my config.php file.

  • #6
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post
    Can you provide the errors it is returning?

  • #7
    New Coder
    Join Date
    Aug 2011
    Location
    London
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It does not return the error, it just does not change/update the database. No errors recorded.

  • #8
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,307
    Thanks
    4
    Thanked 205 Times in 202 Posts
    As I said in my first post, the change_username function is never being called so the fact that nothing is being updated is not a surprise. I don't suspect he's getting any errors either based on what's been posted.
    Dave .... HostMonster for all of your hosting needs

  • #9
    New Coder
    Join Date
    Aug 2011
    Location
    London
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Djm, what do you mean. I've named it change_username as a function?

  • #10
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by djm0219 View Post
    As I said in my first post, the change_username function is never being called so the fact that nothing is being updated is not a surprise. I don't suspect he's getting any errors either based on what's been posted.
    DOH, didn't even see he defined a function.

    Quote Originally Posted by DanielWatts View Post
    Djm, what do you mean. I've named it change_username as a function?
    Yes, you have have defined it, but you have not called it.

    This should AT LEAST produce an error, at most update your database:
    PHP Code:
    <?php
    include 'config.php';
    ?>
    <div align="left">Welcome too the profile page, here you can change your email and password and other features!</div>
    <br />
    <br />
    <br />
    <div align="left"><strong>Change Name:</strong>
    <br /><form action = '<?php echo $_SERVER['PHP_SELF']; ?>' method = 'POST' name = 'usernamechange'><input type="text" name="userchange" value=""><input type="submit" name="changename" value="Change">
    </form>
    <?php
    if (ISSET($_POST['changename'])){ 
    function 
    change_username($cusername){
        
    $cusername mysql_real_escape_string(htmlentities($cusername));
        
    $cname mysql_real_escape_string($cusername);
        
    mysql_query("UPDATE `users` SET `name` = '{$cusername}' WHERE `name`='OLDUSERNAME'");
        }
        
    change_username($_POST['userchange']); 
    }
    ?>

  • #11
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,439
    Thanks
    62
    Thanked 537 Times in 524 Posts
    Any particular reason why htmlentities is also being used inside mysql_real_escape_string?

    It doesn't really do anything for security.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #12
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,307
    Thanks
    4
    Thanked 205 Times in 202 Posts
    In addition to what tangoforce said, why do you have change_username as a function? Functions are normally used when something needs to be done more than once and used in several places. For what you are trying to do the code in that function is just as easily done "in line".

    The only other question here is where is OLDUSERNAME coming from?
    PHP Code:
    <?php include 'config.php'?>
    <div align="left">
    Welcome too the profile page, here you can change your email and password and other features!
    </div>
    <br />
    <br />
    <br />
    <div align="left">
    <strong>Change Name:</strong>
    </div>
    <form action = '<?php echo $_SERVER['PHP_SELF']; ?>' method = 'POST' name = 'usernamechange'>
    <input type="text" name="userchange" value="">
    <input type="submit" name="changename" value="Change">
    </form>
    <?php
    if (ISSET($_POST['changename'])){ 
        
    $cusername mysql_real_escape_string($_POST['userchange']); 
        
    mysql_query("UPDATE `users` SET `name` = '{$cusername}' WHERE `name`='OLDUSERNAME'");
    }
    ?>
    Dave .... HostMonster for all of your hosting needs


  •  

    Posting Permissions

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