(PHP)使用已登录用户的用户名将其添加到另一个数据库中

时间:2018-10-16 07:11:52

标签: php html sql bootstrap-4

因此,对于我的任务,我需要使用登录用户的用户名,并将其放入同一数据库中的另一个表中,当该用户上传图像时,每当我尝试上传图像时,都会出现该数据库仅有ID,其余为空。

处理脚本:

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

登录脚本:

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

上传脚本

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

希望获得帮助,我觉得我错过了一些小细节

2 个答案:

答案 0 :(得分:1)

当您在表单中输入字段时,将使用您没有的字段的name=值来提交字段。

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

对所有字段重复此操作。我还删除了值和属性标签之间的空格。

在提交表单时,还要检查是否设置了一个值,然后设置了要使用的变量,但是您没有检查是否未设置它们并停止提交表单。这意味着,如果未设置该字段,则代码将继续以一个空值(如您所找到的)继续,它还将具有某种形式的消息(如果您添加了报告),表明该字段不存在。 ..

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'];

最好写出是否设置了所有字段...

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

    // Process insert
}

答案 1 :(得分:0)

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

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