phreak3r Posted January 5, 2019 Share Posted January 5, 2019 If the user signs up and does not have an avatar, a default will be given to them. I am checking for the avatar/image file, however, that is not working. Here is the messy code below: if ($_SERVER['REQUEST_METHOD'] == 'POST') { // $username = $_POST['username']; // adds user info submitted upon registration to database function addUser($pdo) { $username = $_POST['username']; $password = password_hash($_POST['password'], PASSWORD_DEFAULT); $bio = $_POST['bio']; $email = $_POST['email']; $c_status = 0; $query = $pdo->prepare("INSERT into profiles001 (username, password, email, c_status, bio) VALUES (:username, :password, :email, :cstat, :bio)"); $query->bindValue(':username', $username); $query->bindValue(':password', $password); $query->bindValue(':email', $email); $query->bindValue(':cstat', $c_status); $query->bindValue(':bio', $bio); $file = $_FILES['userfile']; $file_name = $file['name']; $file_type = $file['type']; $file_size = $file['size']; $file_tmp_name = $file['tmp_name']; $file_error = $file['error']; if (!isset($_FILES['userfile'])) { $avatar = "assets/soap.jpg"; $avatar_present_query = $pdo->prepare("INSERT into profiles001 (avatar) VALUES (:avatar) WHERE username = ':username'"); $avatar_present_query->bindValue(':avatar', $avatar); $avatar_present_query->bindValue(':username', $username); $avatar_present_query->execute(); $query->execute(); } // $query->execute(); } addUser($pdo); Quote Link to comment Share on other sites More sharing options...
benanamen Posted January 5, 2019 Share Posted January 5, 2019 For starters, you have thrown a function right in the middle of where code should be processed. If you moved the function somewhere else you are now faced with a missing closing curly bracket and a call to the function floating in space. Start with NOT having all your code in a function. Saying something "is not working" is not going to help anyone help you. You would do well to read through this page. Quote Link to comment Share on other sites More sharing options...
phreak3r Posted January 5, 2019 Author Share Posted January 5, 2019 17 minutes ago, benanamen said: For starters, you have thrown a function right in the middle of where code should be processed. If you moved the function somewhere else you are now faced with a missing closing curly bracket and a call to the function floating in space. Start with NOT having all your code in a function. Saying something "is not working" is not going to help anyone help you. You would do well to read through this page. Sorry that I missed the last brace Kevin. As for putting the code in the function, I am going for a 'functional programming' type of format. I did not copy the entire thing as what is past the brace is commented code and THEN the final brace. Would you like to see that? Quote Link to comment Share on other sites More sharing options...
benanamen Posted January 5, 2019 Share Posted January 5, 2019 What would be more helpful is an explanation of what " not working " means. Quote Link to comment Share on other sites More sharing options...
phreak3r Posted January 5, 2019 Author Share Posted January 5, 2019 I am checking for the avatar/image file, however, that is not working. if (!isset($_FILES['userfile'])) { $avatar = "assets/soap.jpg"; $avatar_present_query = $pdo->prepare("INSERT into profiles001 (avatar) VALUES (:avatar) WHERE username = ':username'"); $avatar_present_query->bindValue(':avatar', $avatar); $avatar_present_query->bindValue(':username', $username); $avatar_present_query->execute(); $query->execute(); } Specifically, the query in the if-statement is not doing its job. I tried using UPDATE previously and that too would fail to work. I know PHP after a certain version (can't quite recall which) is more so made for object-oriented programming style. But I am wanting to go with the functional programming or function-based programming format. That is why I am using functions. Quote Link to comment Share on other sites More sharing options...
benanamen Posted January 5, 2019 Share Posted January 5, 2019 What is the result of var_dump($_FILES); Post your form as well. Quote Link to comment Share on other sites More sharing options...
benanamen Posted January 5, 2019 Share Posted January 5, 2019 By the way, username in the WHERE should not be quoted. I don't used named parameters but that may be the problem. Quote Link to comment Share on other sites More sharing options...
phreak3r Posted January 5, 2019 Author Share Posted January 5, 2019 From the var_dump: array(1) { ["userfile"]=> array(5) { ["name"]=> string(0) "" ["type"]=> string(0) "" ["tmp_name"]=> string(0) "" ["error"]=> int(4) ["size"]=> int(0) } } <?php include('header.php'); ?> <!DOCTYPE html> <html> <head> <title>soapbox - sign up</title> </head> <body> <form action="confirmation.php" method="POST" enctype="multipart/form-data"> <br> Avatar <br> <input type="file" name="userfile" id="fileToUpload"> <br> Username: <br> <input type="text" name="username" maxlength="26" placeholder="Username"> <br> Password: <br> <input type="password" name="password" maxlength="26" placeholder="Password"> <br> Email Address: <br> <input type="email" name="email" placeholder="Email Address"> <p>Bio: </p><textarea name="bio" placeholder="Bio..." rows="5" style="resize: none;"></textarea> <br> <input type="submit" value="Submit"> </form> </body> <!--Include footer later on --> </html> form from the signup.php file Quote Link to comment Share on other sites More sharing options...
benanamen Posted January 5, 2019 Share Posted January 5, 2019 (edited) Ok, I missed it. ? The !isset logic is wrong. The $_FILES['userfile'] is always going to be set on POST so that block will never run. You need to check if the userfile name is empty like so... if (empty($_FILES['userfile']['name'])) { Run this with no upload <?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (empty($_FILES['userfile']['name'])) { echo 'No File Uploaded'; } if (!isset($_FILES['userfile'])) { echo 'I will never run'; } } ?> <html> <body> <form action="" method="POST" enctype="multipart/form-data"> <input type="file" name="userfile" /> <br> Username: <br> <input type="text" name="username" maxlength="26" placeholder="Username"> <input type="submit"/> </form> </body> </html> Edited January 5, 2019 by benanamen Quote Link to comment Share on other sites More sharing options...
phreak3r Posted January 5, 2019 Author Share Posted January 5, 2019 4 minutes ago, benanamen said: Ok, I missed it. ? The !isset logic is wrong. The $_FILES['userfile'] is always going to be set on POST so that block will never run. You need to check if the userfile name is empty like so... if (empty($_FILES['userfile']['name'])) { Okay, data is being inserted into the database again, except for the avatar path. I think I will have to write out $_FILES['userfile']['name'] instead of doing what I had done before. ($file['name'] and $file being equal to $_FILES['userfile']) Quote Link to comment Share on other sites More sharing options...
Barand Posted January 5, 2019 Share Posted January 5, 2019 5 hours ago, benanamen said: By the way, username in the WHERE should not be quoted. I don't used named parameters but that may be the problem. Or it could be that INSERT queries do not have a WHERE clause 1 Quote Link to comment Share on other sites More sharing options...
benanamen Posted January 5, 2019 Share Posted January 5, 2019 Oh my. I have been really off my game lately. I got hit by a car this past November and got a concussion. It has been showing in many of my posts . Thanks @Barand. Quote Link to comment Share on other sites More sharing options...
phreak3r Posted January 6, 2019 Author Share Posted January 6, 2019 14 hours ago, Barand said: Or it could be that INSERT queries do not have a WHERE clause Dammit! Well, you are correct Barand. See this stack overflow post. I might need to try UPDATE instead. 7 hours ago, benanamen said: Oh my. I have been really off my game lately. I got hit by a car this past November and got a concussion. It has been showing in many of my posts . Thanks @Barand. Ah, I am sorry about that Kevin. I cannot stand cars, sadly in my case they are necessary as my locale requires them to a certain extent. Watch out for the SUVs and Pickup Truck drivers, those are the worst. Thank you benanamen and barand. Quote Link to comment Share on other sites More sharing options...
benanamen Posted January 6, 2019 Share Posted January 6, 2019 5 hours ago, phreak3r said: Watch out for the SUVs and Pickup Truck drivers, those are the worst. Now you tell me! It was an SUV that hit me. 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.