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 5 of 5

Thread: Database Update

  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    68
    Thanks
    1
    Thanked 1 Time in 1 Post

    Database Update

    PHP Code:
       <?php
                    
    require('connect.php');
          if(!isset(
    $_POST['submit'])){
          
    $cresult mysql_query("SELECT * FROM users");
          
    $rusers mysql_fetch_array($cresult);
          
    $id $_SESSION['id'];
       
    ?>                                       
          <h5>Edit Account Profile</h5>
             <form method="post" action="">
                Username: <?php echo $rusers['username']; ?><br/>
                First Name: <input type="text" name="forename"  class="form" value="<?php echo $rusers['forename']; ?>"><br/>
                Surname: <input type="text" name="surname"  class="form" value="<?php echo $rusers['surname']; ?>"><br/>
                E-mail: <input type="text" name="email"  class="form" value="<?php echo $rusers['email']; ?>"><br/>
                Address: <input type="text" name="address1"  class="form" value="<?php echo $rusers['address1']; ?>"><br/>
                City: <input type="text" name="address2"  class="form" value="<?php echo $rusers['address2']; ?>"><br/>
                Postcode: <input type="text" name="postcode"  class="form" value="<?php echo $rusers['postcode']; ?>"><br/>
                Country: <input type="text" name="country"  class="form" value="<?php echo $rusers['country']; ?>"><br/>
             <input type="submit" name="submit" value="Modify">
             </form>
       <?php
          
    }else{
             
    $id $_SESSION['id'];
             
    $forename mysql_real_escape_string($_POST['forename']);
             
    $surname mysql_real_escape_string($_POST['surname']);
             
    $email mysql_real_escape_string($_POST['email']);
             
    $address1 mysql_real_escape_string($_POST['address1']);
             
    $address2 mysql_real_escape_string($_POST['address2']);
             
    $postcode mysql_real_escape_string($_POST['postcode']);
             
    $country mysql_real_escape_string($_POST['country']);
             
          
    mysql_query("UPDATE users SET forename='$forename', surname='$surname', email='$email', address1 = '$address1', address2 = '$address2', postcode = '$postcode', country='$country' WHERE id='$id'");
          echo 
    "Your Account Profile has been updated.";
          }
       
    ?>
    I'm trying to update the forename, surname, email, address1, address2, postcode and country fields in a table in my database. However I can't seem to get it to update.
    What am I doing wrong?

    Thanks.

  • #2
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Change to this

    PHP Code:
     <?php
                    
    require('connect.php');
          if(!isset(
    $_POST['submit'])){
          
    $cresult mysql_query("SELECT * FROM users");
          
    $rusers mysql_fetch_array($cresult);
          
    $id $_SESSION['id'];

             
    $forename mysql_real_escape_string($_POST['forename']);
             
    $surname mysql_real_escape_string($_POST['surname']);
             
    $email mysql_real_escape_string($_POST['email']);
             
    $address1 mysql_real_escape_string($_POST['address1']);
             
    $address2 mysql_real_escape_string($_POST['address2']);
             
    $postcode mysql_real_escape_string($_POST['postcode']);
             
    $country mysql_real_escape_string($_POST['country']);
             
          
    mysql_query("UPDATE users SET forename='$forename', surname='$surname', email='$email', address1 = '$address1', address2 = '$address2', postcode = '$postcode', country='$country' WHERE id='$id'");
          echo 
    "Your Account Profile has been updated.";
                                        
          
     
          }
    else {
    ?>
           <h5>Edit Account Profile</h5>
             <form method="post" action="">
                Username: <?php echo $rusers['username']; ?><br/>
                First Name: <input type="text" name="forename"  class="form" value="<?php echo $rusers['forename']; ?>"><br/>
                Surname: <input type="text" name="surname"  class="form" value="<?php echo $rusers['surname']; ?>"><br/>
                E-mail: <input type="text" name="email"  class="form" value="<?php echo $rusers['email']; ?>"><br/>
                Address: <input type="text" name="address1"  class="form" value="<?php echo $rusers['address1']; ?>"><br/>
                City: <input type="text" name="address2"  class="form" value="<?php echo $rusers['address2']; ?>"><br/>
                Postcode: <input type="text" name="postcode"  class="form" value="<?php echo $rusers['postcode']; ?>"><br/>
                Country: <input type="text" name="country"  class="form" value="<?php echo $rusers['country']; ?>"><br/>
             <input type="submit" name="submit" value="Modify">
             </form> 
    <?php 
          
    }
       
    ?>
    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • #3
    New Coder
    Join Date
    Jul 2009
    Posts
    68
    Thanks
    1
    Thanked 1 Time in 1 Post
    I've updated my code to this but I still can't get it to update.

    PHP Code:
    <?php
    if(isset($_POST['submit'])){
        
               
    $id $_SESSION['id'];
             
    $forename mysql_real_escape_string($_POST['forename']);
             
    $surname mysql_real_escape_string($_POST['surname']);
             
    $email mysql_real_escape_string($_POST['email']);
             
    $address1 mysql_real_escape_string($_POST['address1']);
             
    $address2 mysql_real_escape_string($_POST['address2']);
             
    $postcode mysql_real_escape_string($_POST['postcode']);
             
    $country mysql_real_escape_string($_POST['country']);

        if(isset(
    $error)){
        echo 
    "$error";
        }else{
          
    mysql_query("UPDATE users SET forename='$forename', surname='$surname', email='$email', address1 = '$address1', address2 = '$address2', postcode = '$postcode', country='$country' WHERE id='$id'");
        
        echo 
    "Done.";
            }
            }else{        
    ?>           

    <?php 
        $cresult 
    mysql_query("SELECT * FROM users");
        
    $rusers mysql_fetch_array($cresult);
        
    $id $_SESSION['id'];
    ?>
        <h5>Edit Account Profile</h5>
             <form method="post" action="">
                Username: <?php echo $rusers['username']; ?><br/>
                First Name: <input type="text" name="forename"  class="form" value="<?php echo $rusers['forename']; ?>"><br/>
                Surname: <input type="text" name="surname"  class="form" value="<?php echo $rusers['surname']; ?>"><br/>
                E-mail: <input type="text" name="email"  class="form" value="<?php echo $rusers['email']; ?>"><br/>
                Address: <input type="text" name="address1"  class="form" value="<?php echo $rusers['address1']; ?>"><br/>
                City: <input type="text" name="address2"  class="form" value="<?php echo $rusers['address2']; ?>"><br/>
                Postcode: <input type="text" name="postcode"  class="form" value="<?php echo $rusers['postcode']; ?>"><br/>
                Country: <input type="text" name="country"  class="form" value="<?php echo $rusers['country']; ?>"><br/>
             <input type="submit" name="submit" value="Modify">
             </form> 
    <?php }
    include(
    "template/footer.php");
    ?>

  • #4
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    You're not testing whether the query is successful. A quick test would be to add or die(mysql_error()) after the mysql_query, like this:
    PHP Code:
    mysql_query("your query here") or die(mysql_error()); 
    It shouldn't be used like that in production, but since you have no error checking at all, it can't be any worse.
    Are you a Help Vampire?

  • #5
    New Coder
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Try replacing:
    PHP Code:
    mysql_query("UPDATE users SET forename='$forename', surname='$surname', email='$email', address1 = '$address1', address2 = '$address2', postcode = '$postcode', country='$country' WHERE id='$id'"); 
         
        echo 
    "Done."
    With:
    PHP Code:
    $query "UPDATE users SET forename='$forename', surname='$surname', email='$email', address1 = '$address1', address2 = '$address2', postcode = '$postcode', country='$country' WHERE id='$id'"
    echo 
    $query;
    mysql_query($query) or die(mysql_error()); 
    And tell me what gets displayed...
    -Sam


  •  

    Posting Permissions

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