简体   繁体   中英

(PHP) Using the username of a logged in user to add it into another database

So for my assignment, I need to use the username of a logged in user, and put that into another table in the same database, when the said user uploads an image, and whenever I try to upload, the database comes up with only the id, the rest is empty.

Processing Script:

<?php
if(isset($_POST['username'])) $username = $_POST['username'];    
if(isset($_POST['title'])) $title = $_POST['title'];
if(isset($_POST['category'])) $category = $_POST['category'];
if(isset($_POST['description'])) $description = $_POST['description'];
if(isset($_POST['tags'])) $tags = $_POST['tags'];

$filename = $_FILES['image']['name'];
$location = $_FILES['image']['tmp_name'];

move_uploaded_file($location, "uploads/$filename");

$db = mysqli_connect("localhost", "root","", "artworks") or die(mysqli_error($db));
$q = "insert into artwork values(null, '$username', '$title', '$category', '$description', '$tags', '$filename')";
mysqli_query($db, $q) or die(mysqli_error($db));

header("Location:index.php");
exit(0); ?>

Login Script:

    <?php
    if(isset($_POST['username'])) $username = $_POST['username'];
    if(isset($_POST['password'])) $password = $_POST['password'];

    $db = mysqli_connect("localhost", "root","", "artworks")  or die(mysqli_error($db));
    $q = "select * from member where username='$username' and password=SHA('$password')";
    $results = mysqli_query($db, $q) or die(mysqli_error($db));

    if(mysqli_num_rows($results) > 0)
    {
        session_start();
        $_SESSION['username'] = $username;
        header("Location:index.php");
        exit(0);
    }

    header("Location:register.php");?>

Upload Script

<?php
session_start();

    if(!isset($_SESSION['username']))
    {
        header("Location:login.php");
        exit(0);
    }
?>

<?php include("header.inc");
        include("nav.inc"); ?>

<div class = "container">
<form role = "form-horizontal" method = "post" action = "add_ps.php" enctype="multipart/form-data">
        <div>
            <div class = "form-group">
            <label for = "title" class="control-label col-xs-6">Title</label>
            <input type = "text" class = "form-control" id = "title" placeholder = "Title of Image">
            </div>
        </div>

        <div>
            <div class = "form-group">
            <label for = "category" class="control-label col-xs-6">Category</label>
            <input type = "text" class = "form-control" id = "category" placeholder = "Category of Image">
            </div>
        </div>

        <div>
            <div class = "form-group">
            <label for = "description" class="control-label col-xs-6">Description</label>
            <textarea class="form-control" rows="5" id="description" placeholder = "Description of Image"></textarea>
            </div>
        </div>

        <div>
            <div class = "form-group">
            <label for = "tags" class="control-label col-xs-6">Tags (Seperate with comma)</label>
            <input type = "text" class = "form-control" id = "tag" placeholder = "Tag of Image">
            </div>
        </div>

        <div class = "form-group">
            <label for = "inputfile">File input</label>
            <input type = "file" id = "uploads">
            <p class = "help-block">Upload image here</p>
         </div>

        <div class = "form-group">
            <div class="col-xs-2">
                <button type = "submit" class = "btn btn-primary">Submit</button>
            </div>
        </div>
</form>
</div>

<?php include("footer.inc"); ?>

Hope this help, I feel like I am missing out on some tiny detail

When you have your fields in the form, your fields will be submitted with the name= value of the field which you don't have...

    <input type="text" class="form-control" id="title" placeholder="Title of Image" name="title">

Repeat this for all of your fields. I've also removed the spaces between the value and the attribute label.

Also when you submit the form, you check if a value is set and then set the variable you use, but you don't check if they aren't set and stop the form being submitted. This means that if the field isn't set, the code will just continue with an empty value (as you've found) it will also have some form of message(if you added reporting) to say the field doesn't exist...

if(isset($_POST['username'])) $username = $_POST['username'];    
if(isset($_POST['title'])) $title = $_POST['title'];
if(isset($_POST['category'])) $category = $_POST['category'];
if(isset($_POST['description'])) $description = $_POST['description'];
if(isset($_POST['tags'])) $tags = $_POST['tags'];

Would be better written to say if all the fields are set...

if (isset($_POST['username']) && isset($_POST['title']) ... ) { // Add rest of fields.
    $username = $_POST['username'];
    $title = $_POST['title'];
    // Add rest of fields.

    // Process insert
}

只需添加name =“ image”您就会丢失它

 <input type = "file" id = "uploads" name="image">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM