Jump to content
phreak3r

Data is not inserted into db

Recommended Posts

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); 

 

Share this post


Link to post
Share on other sites

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.

 

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

What would be more helpful is an explanation of what " not working " means.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

What is the result of var_dump($_FILES);

Post your form as well.

Share this post


Link to post
Share on other sites

By the way, username in the WHERE should not be quoted. I don't used named parameters but that may be the problem.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Posted (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 by benanamen

Share this post


Link to post
Share on other sites
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'])

Share this post


Link to post
Share on other sites
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

  • Like 1

Share this post


Link to post
Share on other sites

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 :facewall:.

Thanks @Barand.

Share this post


Link to post
Share on other sites
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 :facewall:.

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.