LostAndConfused Posted January 9, 2009 Share Posted January 9, 2009 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); } Quote Link to comment Share on other sites More sharing options...
gevans Posted January 9, 2009 Share Posted January 9, 2009 $q = "UPDATE `TBL_USERS` SET `$field` = '$value' WHERE username = '$username'"; Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 9, 2009 Share Posted January 9, 2009 The first step would be to add some debugging to the query: return mysql_query($q, $this->connection) or trigger_error(mysql_error(),E_USER_ERROR); Other than that, we'd need to see a bit more of your code. Where is the function called? Quote Link to comment Share on other sites More sharing options...
LostAndConfused Posted January 9, 2009 Author Share Posted January 9, 2009 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. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 9, 2009 Share Posted January 9, 2009 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); } Quote Link to comment Share on other sites More sharing options...
LostAndConfused Posted January 9, 2009 Author Share Posted January 9, 2009 Ok updated it with your code and still no change I can see... Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 9, 2009 Share Posted January 9, 2009 Can you post the current select statement to see how the database got the info please... if $value and $filed are valid then it should work, unless you got the database setup wrong or the form names posting incorrectly. Quote Link to comment Share on other sites More sharing options...
LostAndConfused Posted January 9, 2009 Author Share Posted January 9, 2009 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>]"; } ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 9, 2009 Share Posted January 9, 2009 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); } ?> Quote Link to comment Share on other sites More sharing options...
LostAndConfused Posted January 9, 2009 Author Share Posted January 9, 2009 you mean what GingerRobot recommended? I changed to that code and nothing happened any differently when I uploaded the file. Was it suppose to show something? EDIT : ah, let me try that real quick EDIT2: yeah nothing different... Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 9, 2009 Share Posted January 9, 2009 yes stick a exit; at the end of the function should show the select statement echoed. Quote Link to comment Share on other sites More sharing options...
LostAndConfused Posted January 9, 2009 Author Share Posted January 9, 2009 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. Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 9, 2009 Share Posted January 9, 2009 Are you calling the function where it needs to be? because the code should show the select statement it raw data. That way we can see if the select statement getting it values. should off worked with out the exit. Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 9, 2009 Share Posted January 9, 2009 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 ... Quote Link to comment Share on other sites More sharing options...
LostAndConfused Posted January 9, 2009 Author Share Posted January 9, 2009 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? Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 9, 2009 Share Posted January 9, 2009 If the database is being used via any page it needs a include i thort... try does not heart. and like to say nice coding mate ..... differently a connection problem i recon.. Quote Link to comment Share on other sites More sharing options...
LostAndConfused Posted January 9, 2009 Author Share Posted January 9, 2009 ok if i include it on the upload page, it gives me this error so i'm assuming that means it's being ran twice thus not needed on the upload page. Fatal error: Cannot redeclare class mysqldb in /hsphere/local/home/newppg/pdo.newppg.com/include/database.php on line 14 Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 9, 2009 Share Posted January 9, 2009 so is the function in the correct place and called properly... show me... very weird issue here. make sure you are spelling the function correctly mate. Quote Link to comment Share on other sites More sharing options...
LostAndConfused Posted January 9, 2009 Author Share Posted January 9, 2009 what do you mean show you? you want me to send you the php pages involved? EDIT: Just double checked all names and everything. spelling is all correct. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 9, 2009 Share Posted January 9, 2009 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). Quote Link to comment Share on other sites More sharing options...
LostAndConfused Posted January 9, 2009 Author Share Posted January 9, 2009 // 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); } Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 9, 2009 Share Posted January 9, 2009 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. Quote Link to comment Share on other sites More sharing options...
LostAndConfused Posted January 9, 2009 Author Share Posted January 9, 2009 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 Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 9, 2009 Share Posted January 9, 2009 Well sorry leave it to a expert so sorry, I tried all the things i no, what is not much but i tried.... ANYONE COME ON. Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 9, 2009 Share Posted January 9, 2009 1 min back lol, Are you using the same function name somewhere else last comment. can only put that remark down to repeating the same function name by mistake. or the code is not written in it correct order. example if and else Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.