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
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    52
    Thanks
    24
    Thanked 0 Times in 0 Posts

    How to update a single database row

    How to update a single database row without having to update all the rows
    Please look at code and give me ideas to how i can solve this:

    PHP Code:
    <?php
      error_reporting
    (E_ALL E_NOTICE);
      
    session_start();
      
      
    $userid $_SESSION['id'];
      
    $username $_SESSION['username'];
      include(
    "../adminonly/admin.php");
      
     if (
    $_POST['updateinfo']){
             
    // get the form data
             
    $getname mysql_real_escape_string(stripslashes($_POST['name']));
             
    $getbio mysql_real_escape_string(stripslashes($_POST['bio']));
             
    $getlocation mysql_real_escape_string(stripslashes($_POST['location']));
             
    $name $_FILES['myfile']['name'];
             
    $tmp_name $_FILES['myfile']['tmp_name'];
             
             
    // update fullname
             
    if ($getname){
                 
                 
    $insert mysql_query("UPDATE members SET name='$getname' WHERE username='$username'");
                 
                 
    mysql_query($insert);
                 echo 
    "<font color='#999'>Your info has been updated!</font><br/><br/>";
             }
             
             
    //update bio
             
    if ($getbio){
                 
                 
    $insert mysql_query("UPDATE members SET  bio='$getbio' WHERE username='$username'");
                  
    mysql_query($insert);
                 echo 
    "<font color='#999'>Your info has been updated!</font><br/><br/>";
             }
             
              
    //update location
             
    if ($getlocation){
                 
                 
    $insert mysql_query("UPDATE members SET  location='$getlocation' WHERE username='$username'");
                  
    mysql_query($insert);
                 echo 
    "<font color='#999'>Your info has been updated!</font><br/><br/>";
             
        
        
        
    //update avatar
         
    if ($name){
                  
    $userimage "avatar/$name";
                  
    move_uploaded_file($tmp_name$userimage);
                  
                 
    $insert mysql_query("UPDATE members SET avatar='$userimage' WHERE username='$username'");
                 
                 
    mysql_query($insert);
                 echo 
    "<font color='#999'>Your Avatar has been updated!</font><br/><br/>";
             }
        } 
            
            
    }
     
     
    ?>


    <html>
    <title>Members Page</title>
    <head></head>
    <body>
      <?php
      
    if ($username){
         echo 
    "Welcome <b>$username</b>, <a href='./logout.php'>Logout</a>";
         echo 
    "<br /><a href='./resetpass.php'>Reset Your Password</a>";
      }
      else
         echo 
    "Please login to access this page. <a href='./login.php'>Login Here</a>";
         
         
    $form "<form action='./member.php' method='post' enctype='multipart/form-data'>
                      <table>
                          <tr>
                            <td>Fullname:</td>
                            <td><input type='text' name='name' value='
    $getname'/></td>
                          </tr>
                          <tr>
                            <td>Bio:</td>
                            <td><textarea name='bio' value='
    $getbio'></textarea></td>
                          </tr>
                          <tr>
                            <td>Location:</td>
                            <td><input type='text' name='location' value='
    $getlocation'/></td>
                          </tr>
                          <tr>
                            <td>Avatar: </td>
                            <td><input type='file' name='myfile'/></td>
                          </tr>
                          <tr>
                            <td></td>
                            <td><input type='submit' name='updateinfo' value='Update info' /></td>
                          </tr>
                      </table>
                  </form>"
    ;
                  
      echo 
    $form;
      
      
    $get mysql_query("SELECT * FROM members WHERE username='$username'");
      
    $numrows mysql_num_rows($get);
      
     if (
    $get 1){
         echo 
    "No results found!";
     }
     else{
     
      while (
    $row mysql_fetch_array($get)){
     
     
    $dbuser $row['username'];
     
    $getname $row['name'];
     
    $getbio $row['bio'];
     
    $getlocation $row['location'];
     
    $getuserimage $row['avatar'];
     
     
     
    $user_results "<form action='./member.php'>
                      <table>
                      <tr>
                            <td><b></b></td>
                            <td><img src='
    $getuserimage' width='100' height='100'></td>
                          </tr>
                          <tr>
                            <td><b>Fullname:</b></td>
                            <td>
    $getname</td>
                          </tr>
                          <tr>
                            <td><b>Bio:</b></td>
                            <td>
    $getbio</td>
                          </tr>
                          <tr>
                            <td><b>Location:</b></td>
                            <td>
    $getlocation</td>
                          </tr>
                      </table>
                  </form>"
    ;
                  
                  echo 
    $user_results;
     
     }  

    }
      
      
      
    ?>
    </body>
    </html>

  2. #2
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,080
    Thanks
    10
    Thanked 1,158 Times in 1,149 Posts
    You'll have to explain exactly what the problem is.
    I see it already uses UPDATE for the table ... that is updating one row.

    The row which matches the username is getting updated .. no other rows.

  3. #3
    New Coder
    Join Date
    Aug 2011
    Posts
    52
    Thanks
    24
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    You'll have to explain exactly what the problem is.
    I see it already uses UPDATE for the table ... that is updating one row.

    The row which matches the username is getting updated .. no other rows.

    The problem i have is, i want to update each field without having to update all of them at once for example i want to update the name field: I can't do that unless i update the other fields as well. But i only want to update the name filed like wise if i want to update the location field i can't do that without updating the other fields.

  4. #4
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,080
    Thanks
    10
    Thanked 1,158 Times in 1,149 Posts
    Why can't you update one field?

    This line that you have in your script only updates one field called "name" ...

    $insert = mysql_query("UPDATE members SET name='$getname' WHERE username='$username'");

    I guess I'm still not understanding your question ??

  5. #5
    New Coder
    Join Date
    Aug 2011
    Posts
    52
    Thanks
    24
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    Why can't you update one field?

    This line that you have in your script only updates one field called "name" ...

    $insert = mysql_query("UPDATE members SET name='$getname' WHERE username='$username'");

    I guess I'm still not understanding your question ??

    if you have skype add me and i'll share my screen with you: gazaian1
    I really need help on this.


 

Tags for this Thread

Posting Permissions

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