Jump to content

Data is not inserted into db


phreak3r

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

 

Link to comment
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.

 

Link to comment
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?

Link to comment
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.

Link to comment
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

Link to comment
Share on other sites

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
Link to comment
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'])

Link to comment
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.

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.