Jump to content

using php to update mysql string


LostAndConfused

Recommended Posts

My site has a login system I've added which uses mysql databases. I'm trying to make it so people can upload avatars (the filename is saved in the database) and ive also found a system for uploading. Now I just need to tell the mysql database to update with the new filename somehow.

 

I'm taking a shot in the dark and doing this but it's doing everything except updating in mysql. Can somebody tell me what i'm doing wrong?

 

 

Upload.php

$session->updateAvatar[$_FILES['file']['name']];

 

 

session.php

function updateAvatar($subavatar){
      global $database;  //The database connection
      $database->updateUserField($this->username,"avatar",$subavatar);
   }

 

 

database.php

function updateUserField($username, $field, $value){
      $q = "UPDATE ".TBL_USERS." SET ".$field." = '$value' WHERE username = '$username'";
      return mysql_query($q, $this->connection);
   }

Link to comment
Share on other sites

well my sessions.php file already had something similar to what i'm using to send the data to database.php and the database.php function i'm using I haven't messed with at all so it should be working.

 

I'm pretty sure everything is correct but I just am totally confused at why it's not working. gevans, I tried your edit of the database.php line and it doesn't seem to change anything. GingerRobot, you'll have to forgive me i'm a little new to php so how would I go about using that to debug? I changed the last line in the database.php file to the line you gave me and nothing changed with that either. Says uploaded successful but never updated the mysql string.

Link to comment
Share on other sites

Well assuming you do have error_reporting and display_errors on, that would suggest to me that the query either isn't ever being executed or there are no matching rows. Try echoing that query to make sure:

 

function updateUserField($username, $field, $value){
      $q = "UPDATE ".TBL_USERS." SET ".$field." = '$value' WHERE username = '$username'";
      echo 'Query was: '.$q.'<br />';
      return mysql_query($q, $this->connection) or trigger_error(mysql_error(),E_USER_ERROR);
   }

Link to comment
Share on other sites

Sorry I'm not sure what you mean by the current select statement but when the file is uploaded (and that part works) this page is loaded and the following is what i'm using... when it says the filename and tag, it does show correctly.

 

<?php
// ==============
// Configuration
// ==============
$uploaddir = "uploads"; // Where you want the files to upload to - Important: Make sure this folders permissions is 0777!
$allowed_ext = "jpg, gif, png, jpeg"; // These are the allowed extensions of the files that are uploaded
$max_size = "300000"; // 50000 is the same as 50kb
$max_height = "300"; // This is in pixels - Leave this field empty if you don't want to upload images
$max_width = "300"; // This is in pixels - Leave this field empty if you don't want to upload images

// Check Entension
$extension = pathinfo($_FILES['file']['name']);
$extension = $extension[extension];
$allowed_paths = explode(", ", $allowed_ext);
for($i = 0; $i < count($allowed_paths); $i++) {
if ($allowed_paths[$i] == "$extension") {
$ok = "1";
}
}

// Check File Size
if ($ok == "1") {
if($_FILES['file']['size'] > $max_size)
{
print "File size is over the currently set limit.";
exit;
}

// Check Height & Width
if ($max_width && $max_height) {
list($width, $height, $type, $w) =
getimagesize($_FILES['file']['tmp_name']);
if($width > $max_width || $height > $max_height)
{
print "File height and/or width are over the currently set limit.";
exit;
}
}

// The Upload Part
if(is_uploaded_file($_FILES['file']['tmp_name']))
{
  $session->updateAvatar[$_FILES['file']['name']];
  move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
}
print "Your file   [b]".$_FILES['file']['name']."[/b]   has been uploaded successfully. [<a href=\"useredit.php\">My Account</a>]";
} else {
print "Your file   [b]".$_FILES['file']['name']."[/b]  is not one of the aloud filetypes. [<a href=\"Upload.php\">Back</a>]";
}
?>

Link to comment
Share on other sites

The last user's query,  echoed the select statement where is it?

 

is it correct?

 

show us it?

 

i want to see that field and value and username are valued.

 

<?php
function updateUserField($username, $field, $value){
      $q = "UPDATE ".TBL_USERS." SET ".$field." = '$value' WHERE username = '$username'";
      echo 'Query was: '.$q.'<br />'; //<<< show us this?
      return mysql_query($q, $this->connection) or trigger_error(mysql_error(),E_USER_ERROR);
   }
?>

Link to comment
Share on other sites

function updateUserField($username, $field, $value){
      $q = "UPDATE ".TBL_USERS." SET ".$field." = '$value' WHERE username = '$username'";
      echo 'Query was: '.$q.'<br />'; //<<< show us this?
      return mysql_query($q, $this->connection) or trigger_error(mysql_error(),E_USER_ERROR);
      exit;
   }

 

Still no change, just saying file was uploaded successfully.

Link to comment
Share on other sites

should be able to see this on the browser

 

example

 

"UPDATE ".TBL_USERS." SET the field name = 'the value' WHERE username = 'the users name'"

 

if this info not showing then it you not adding the function correctly ..

 

or the function page not included to the page.

 

what your saying is the database query not showing then think what can it be ...

Link to comment
Share on other sites

yeah it doesn't show any text that didn't used to be there...

 

 

if it helps any, this is using the same database function to edit other areas of the mysql database and the edit form does work.

(sorry it's a little long)

 

function editAccount($subcurpass, $subnewpass, $subemail, $subclass, $submajor, $subminor, $subavatar){
      global $database, $form;  //The database and form object
      /* New password entered */
      if($subnewpass){
         /* Current Password error checking */
         $field = "curpass";  //Use field name for current password
         if(!$subcurpass){
            $form->setError($field, "* Current Password not entered");
         }
         else{
            /* Check if password too short or is not alphanumeric */
            $subcurpass = stripslashes($subcurpass);
            if(strlen($subcurpass) < 4 ||
               !eregi("^([0-9a-z])+$", ($subcurpass = trim($subcurpass)))){
               $form->setError($field, "* Current Password incorrect");
            }
            /* Password entered is incorrect */
            if($database->confirmUserPass($this->username,md5($subcurpass)) != 0){
               $form->setError($field, "* Current Password incorrect");
            }
         }
         
         /* New Password error checking */
         $field = "newpass";  //Use field name for new password
         /* Spruce up password and check length*/
         $subpass = stripslashes($subnewpass);
         if(strlen($subnewpass) < 4){
            $form->setError($field, "* New Password too short");
         }
         /* Check if password is not alphanumeric */
         else if(!eregi("^([0-9a-z])+$", ($subnewpass = trim($subnewpass)))){
            $form->setError($field, "* New Password not alphanumeric");
         }
      }
      /* Change password attempted */
      else if($subcurpass){
         /* New Password error reporting */
         $field = "newpass";  //Use field name for new password
         $form->setError($field, "* New Password not entered");
      }
      
      /* Email error checking */
      $field = "email";  //Use field name for email
      if($subemail && strlen($subemail = trim($subemail)) > 0){
         /* Check if valid email address */
         $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"
                 ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"
                 ."\.([a-z]{2,}){1}$";
         if(!eregi($regex,$subemail)){
            $form->setError($field, "* Email invalid");
         }
         $subemail = stripslashes($subemail);
      }
      
      $field = "class";  //Use field name for class
      if(!$subclass || strlen($subclass = trim($subclass)) == 0){
         $form->setError($field, "* class not entered");
      }
      else{
         /* check class names*/
         $subclass = stripslashes($subclass);
         $subclass = strtolower($subclass);
         if(($subclass != "paladin") && ($subclass != "wizard") && ($subclass != "assassin") && ($subclass != "priest")){
            $form->setError($field, "* Must be paladin, wizard, assassin, or priest");
         }
         else
     {
	   if($subclass == "paladin") $subclass = "Paladin";
	   if($subclass == "wizard") $subclass = "Wizard";
	   if($subclass == "assassin") $subclass = "Assassin";
	   if($subclass == "priest") $subclass = "Priest";
     }
      }

      $field = "major";  //Use field name for major element
      if(!$submajor || strlen($submajor = trim($submajor)) == 0){
         $form->setError($field, "* major element not entered. use none if you have none.");
      }
      else{
         /* check element names*/
         $submajor = stripslashes($submajor);
         $submajor = strtolower($submajor);
         if(($submajor != "metal") && ($submajor != "wood") && ($submajor != "water") && ($submajor != "fire") && ($submajor != "earth") && ($submajor != "none") && ($subminor != "-none-")){
            $form->setError($field, "* Must be metal, wood, water, fire, earth, or none");
         }
     else
     {
	  if($submajor == "metal") $submajor = "Metal";
	  if($submajor == "wood") $submajor = "Wood";
	  if($submajor == "water") $submajor = "Water";
	  if($submajor == "fire") $submajor = "Fire";
	  if($submajor == "earth") $submajor = "Earth";
          if($submajor == "none") $submajor = "-None-";
         }
      }

      $field = "minor";  //Use field name for minor element
      if(!$subminor || strlen($subminor = trim($subminor)) == 0){
         $form->setError($field, "* minor element not entered. use none if you have none.");
      }
      else{
         /* check element names*/
         $subminor = stripslashes($subminor);
         $subminor = strtolower($subminor);
         if(($subminor != "metal") && ($subminor != "wood") && ($subminor != "water") && ($subminor != "fire") && ($subminor != "earth") && ($subminor != "none") && ($subminor != "-none-")){
            $form->setError($field, "* Must be metal, wood, water, fire, earth, or none");
         }
         else
     {
	  if($subminor == "metal") $subminor = "Metal";
	  if($subminor == "wood") $subminor = "Wood";
	  if($subminor == "water") $subminor = "Water";
	  if($subminor == "fire") $subminor = "Fire";
	  if($subminor == "earth") $subminor = "Earth";
          if($subminor == "none") $subminor = "-None-";
         }
      }
      if(($submajor == "-None-") && ($subminor != "-None-")){
         $form->setError($field, "* you cannot have a minor without a major");}
      
      /* Errors exist, have user correct them */
      if($form->num_errors > 0){
         return false;  //Errors with form
      }
      
      /* Update password since there were no errors */
      if($subcurpass && $subnewpass){
         $database->updateUserField($this->username,"password",md5($subnewpass));
      }
      
      /* Change Email */
      if($subemail){
         $database->updateUserField($this->username,"email",$subemail);
      }
      
      /* Change Class */
      if($subclass){
         $database->updateUserField($this->username,"class",$subclass);
      }
      
      /* Change Major */
      if($submajor){
         $database->updateUserField($this->username,"major",$submajor);
      }
      
      /* Change Minor */
      if($subminor){
         $database->updateUserField($this->username,"minor",$subminor);
      }
      
      /* Change Avatar link
      if($subminor){
         $database->updateUserField($this->username,"minor",$subminor);
      }*/
      
      /* Success! */
      return true;
   }

 

on the upload page, it includes sessions.php and on the sessions.php page it includes database.php so is that correct or should I be calling database.php on the upload page as well?

Link to comment
Share on other sites

Ok updated it with your code and still no change I can see...

 

Seeing as we've moved on a bit, the change in question was:

 

function updateUserField($username, $field, $value){
      $q = "UPDATE ".TBL_USERS." SET ".$field." = '$value' WHERE username = '$username'";
      echo 'Query was: '.$q.'<br />';
      return mysql_query($q, $this->connection) or trigger_error(mysql_error(),E_USER_ERROR);
   }

 

If you're saying you get no output from this function to the screen, then i can only assume it isn't being called anywhere. I can't see anywhere in the other code that you have posted where you actually call this function. Defining a function doesn't execute the code inside; you must call it with those parameters (username, field and value).

Link to comment
Share on other sites

// The Upload Part
if(is_uploaded_file($_FILES['file']['tmp_name']))
{
  $session->updateAvatar[$_FILES['file']['name']];
  move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
}
print "Your file   <b>".$_FILES['file']['name']."</b>   has been uploaded successfully. [<a href=\"useredit.php\">My Account</a>]";
} else {
print "Your file   <b>".$_FILES['file']['name']."</b>  is not one of the aloud filetypes. [<a href=\"Upload.php\">Back</a>]";
}
?>

 

 

That's the part on my upload page where it uploads it. You can see where i'm trying to update the mysql string.

 

Then sessions reads that function which is where this part comes in...

 

function updateAvatar($subavatar){
      global $database;  //The database connection
      $database->updateUserField($this->username,"avatar",$subavatar);
   }

Link to comment
Share on other sites

are we trying to update this.

 

    /* Change Email */
      if($subemail){
         $database->updateUserField($this->username,"email",$subemail);
      }
     
      /* Change Class */
      if($subclass){
         $database->updateUserField($this->username,"class",$subclass);
      }
     
      /* Change Major */
      if($submajor){
         $database->updateUserField($this->username,"major",$submajor);
      }
     
      /* Change Minor */
      if($subminor){
         $database->updateUserField($this->username,"minor",$subminor);
      }
     
      /* Change Avatar link
      if($subminor){
         $database->updateUserField($this->username,"minor",$subminor);
      }*/
     
      /* Success! */
      return true;
   }

 

if so is $subminor correct.

 

Link to comment
Share on other sites

yes that is part of the account edit page which allows members to edit their account info. all of that stuff works perfectly which uses the same basic functions. I just showed it to show everything from there on works so when I made the updateAvatar function, I just used the same basic setup from that page. Thats why i'm confused at why it's not working the same way.

 

 

Oh by the way using that database echo edit you guys recommended, when updating my account info, it shows this instead now...

Query was: UPDATE users SET email = 'OutlawCecil@gmail.com' WHERE username = 'General_Leo'
Query was: UPDATE users SET class = 'Paladin' WHERE username = 'General_Leo'
Query was: UPDATE users SET major = 'Earth' WHERE username = 'General_Leo'
Query was: UPDATE users SET minor = 'Wood' WHERE username = 'General_Leo'

Warning: Cannot modify header information - headers already sent by (output started at /hsphere/local/home/newppg/pdo.newppg.com/include/database.php:158) in /hsphere/local/home/newppg/pdo.newppg.com/process.php on line 186

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.