在数据库中存储数据时出错

时间:2018-05-30 12:14:27

标签: php html forms mysqli

我创建了一个php编辑表单,我想编辑我的记录;所有记录都在更新,但是当我尝试更新附件文件时,它们没有得到更新,而是当我尝试更新文件时,文件存储在上传文件夹中。我已尝试使用以下代码,但它无法正常工作。谁能告诉我哪里出错了?

<?php
$nameErr = $emailErr = $fileToUploadErr = $fileToUploadErrr = $fileToUploadErrrr = $fileToUploadErrrrr = $fileToUploadErrrrrr = "";
$file = $name = $email = $filename = "";
include_once 'connect.php';

$id = $_GET['id'];
$query = mysqli_query($mysqli, "select * from `users` where userid ='$id'");
$row = mysqli_fetch_array($query);

// set a default variable to hold the original value if $_POST is not triggered

$name = $row['name'];
$email = $row['email'];
$filename = $row['filename'];

// checking weather the form has been submitted before performing the update query

if (isset($_POST['update'])) {

    // Name Section

    if (empty($_POST["name"])) {
        $nameErr = "Name is required";
    } else {
        $name = $_POST["name"];

        // check if name only contains letters and whitespace

        if (!preg_match("/^[a-zA-Z ]*$/", $name)) {
            $nameErr = "Only letters allowed no blank space";
        }
    }

    // E-mail Section

    if (empty($_POST["email"])) {
        $emailErr = "Email is required";
    } else {
        $email = $_POST["email"];

        // check if e-mail address is well-formed

        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $emailErr = "Invalid email format";
        }
    }

    // UPLOAD FILE

    $uploaddir = 'upload/';
    $uploadfile = $uploaddir.basename($_FILES['filename']['name']);
    $imageFileType = pathinfo($uploadfile, PATHINFO_EXTENSION);
    if ($_FILES['filename']['name'] != '') {

        // Check if file already exists

        if (file_exists($uploadfile)) {
            $fileToUploadErrrrrr = "Sorry, file already exists.";
        }

        if ($imageFileType != "dotx" && $imageFileType != "pdf" && $imageFileType != "txt") {
            $fileToUploadErr = "Sorry, only Word,pdf,text files are allowed.";
        } else {
            $uploaded = move_uploaded_file($_FILES['filename']['tmp_name'], $uploadfile);
            if ($uploaded) {
                $file = $_FILES['filename']['name'];
            } else {
                $file = $filename;
            }
        }
    }

    if ($nameErr == "" &&
        $emailErr == "" &&
        $fileToUploadErr == "" &&
        $fileToUploadErrr == "" &&
        $fileToUploadErrrr == "" &&
        $fileToUploadErrrrr == "" &&
        $fileToUploadErrrrrr == "") {
        $check = "SELECT * FROM users WHERE email = '$email' and userid !='$id'";
        $rs = mysqli_query($mysqli, $check);
        $da = mysqli_fetch_array($rs, MYSQLI_NUM);
        if ($da > 0) {
            echo "email Already in Exists";
        } else {
            if ($file == "" || $filename != null) {
                $sql = "update `users` set name='$name', email='$email' where userid ='$id'";
                if (mysqli_query($mysqli, $sql)) {
                    echo "Record updated successfully";
                } else {
                    echo "Error updating record: ".mysqli_error(mysqli_query);
                }

                mysqli_close($mysqli);
            } else {
                $sql = "update `users` set name='$name',  email='$email', filename='$file' where userid ='$id'";
                if ($filename != null):
                    $target = "upload/".$filename;
                    unlink($target);
                endif;
                if (mysqli_query($mysqli, $sql)) {
                    echo "Record updated successfully";
                } else {
                    echo "Error updating record: ".mysqli_error(mysqli_query);
                }

                mysqli_close($mysqli);
            }
        }
    }
}

?>


<!DOCTYPE html>
<html>
<head>
    <style>
        .error {
            color: red;
        }

        .button {
            background-color: #4CAF50;
            border: none;
            color: white;
            padding: 10px 25px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            margin: 4px 2px;
            cursor: pointer;
        }

        .div1 {
            /* background-color: #ff80b3;*/
            margin-top: -19px;
            margin-bottom: -25px;
            margin-left: -19px;
        }

        .copy {
            border-radius: 4px;
            padding: 6px 20px;
            border-style: ridge;
        }

        .copy1 {
            border-radius: 4px;
            padding: 6px 28px;
            border-style: ridge;
        }

        .copy2 {
            border-radius: 4px;
            padding: 4px 2px;

        }
    </style>
</head>
<body style="background-color: #ff80b3">

<div style="padding-left: 250px" class="div1">
    <br><br><br><br>
    <h2 style="color:#1aff1a">EDIT :</h2>

    <form method="POST" action="" enctype="multipart/form-data">
        <input type="hidden" name="hidden_id" value="<?= $id ?>"/>
        <label style="color:000000">Name:</label>
        <input class="copy" style="margin-left: 52px" type="text" value="<?= $name ?>" name="name">
        <span class="error"> <?php echo $nameErr; ?></span>
        <br><br>


        <label style="color:000000">E-mail:</label>
        <input type="text" class="copy" style="margin-left: 48px" value="<?= $email ?>" name="email">
        <span class="error"><?php echo $emailErr; ?></span>
        <br><br>


        <label style="color:000000">UPLOAD FILE : </label>
        <input type="file" style="width:180px;height:100px;padding:10px;" name="filename">

        <br>
        <span class="error"><?php echo $fileToUploadErr; ?></span>
        <span class="error"><?php echo $fileToUploadErrrr; ?></span>
        <span class="error"><?php echo $fileToUploadErrrrr; ?></span>
        <span class="error"><br><?php echo $fileToUploadErrrrrr; ?></span>
        <br>

        <input type="submit" class="button" name="update" value="Update">
        <a href="pannel.php">Back</a>
    </form>

</div>

</body>
</html>

0 个答案:

没有答案