Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-07-2012, 08:32 PM   PM User | #1
gazaian1
New Coder

 
Join Date: Aug 2011
Posts: 52
Thanks: 24
Thanked 0 Times in 0 Posts
gazaian1 is an unknown quantity at this point
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>
gazaian1 is offline   Reply With Quote
Old 05-07-2012, 08:48 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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.
mlseim is offline   Reply With Quote
Old 05-07-2012, 08:58 PM   PM User | #3
gazaian1
New Coder

 
Join Date: Aug 2011
Posts: 52
Thanks: 24
Thanked 0 Times in 0 Posts
gazaian1 is an unknown quantity at this point
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.
gazaian1 is offline   Reply With Quote
Old 05-08-2012, 02:05 AM   PM User | #4
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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 ??
mlseim is offline   Reply With Quote
Old 05-08-2012, 02:35 AM   PM User | #5
gazaian1
New Coder

 
Join Date: Aug 2011
Posts: 52
Thanks: 24
Thanked 0 Times in 0 Posts
gazaian1 is an unknown quantity at this point
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.
gazaian1 is offline   Reply With Quote
Reply

Bookmarks

Tags
html, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:04 AM.


Advertisement
Log in to turn off these ads.