来自数据库的PHP sql SELECT工作正常,但是即使显示成功消息,sql INSERT INTO数据库也不工作

时间:2019-06-05 22:51:50

标签: php mysql sql

我正在尝试一些新东西,同时练习PHP。我已经检查了StackOverflow上的所有先前帖子,但找不到解决方案。我正在尝试使用PHP和PhpMyAdmin将一些数据插入数据库。现在,我面临的问题是,如果我手动输入数据,则可以显示(SELECT FROM)来自数据库的数据。当我尝试使用PHP示例将数据动态插入数据库中时:

$sql = "INSERT INTO apps (appName, appDescription, appLinkFacebook, appLinkInstagram, appLinkPlaystore, appLinkWeb,appGoogleGamesIcon, appFullImageNameBackground, appFullImageNameIcon) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);";

我没有收到任何错误,并且还收到了成功消息,该消息应该在INSERT命令完成后显示。我尝试插入的图像也已在指定文件夹中成功创建,并且它们的名称也以正确的方式显示。我已经检查了表单中的所有输入字段名称,所有链接和拼写“正义”,似乎找不到问题。我还尝试在本地主机和远程服务器上使用数据库时使用INSERT命令,但仍然一无所获。如果有人对做什么有任何想法,请告诉。谢谢

这是我的upload.php文件的完整源代码。

    <?php
    if (isset($_POST['btnUpload'])) {
    $newFileNameCardBackground = $_POST['imgNameCardBackground'];
    if (empty($newFileNameCardBackground)) {
        $newFileNameCardBackground = "card_background";
    } else {
        $newFileNameCardBackground = strtolower(str_replace(" ", "-", $newFileNameCardBackground));
    }

    $newFileNameCardIcon = $_POST['imgNameCardIcon'];
    if (empty($newFileNameCardIcon)) {
        $newFileNameCardIcon = "card_icon";
    } else {
        $newFileNameCardIcon = strtolower(str_replace(" ", "-", $newFileNameCardIcon));
    }

    $appName = $_POST['appName'];
    $appDescription = $_POST['appDescription'];

    $appLinkFacebook = $_POST['appLinkFacebook'];
    $appLinkInstagram = $_POST['appLinkInstagram'];
    $appLinkPlaystore = $_POST['appLinkPlaystore'];
    $appLinkWeb = $_POST['appLinkWeb'];

    $appGoogleGamesIcon = $_POST['appGoogleGamesIcon'];

    $fileCardBackground = $_FILES['fileCardBackground'];
    $fileNameCardBackground = $fileCardBackground["name"];
    $fileTypeCardBackground = $fileCardBackground["type"];
    $fileTempNameCardBackground = $fileCardBackground["tmp_name"];
    $fileErrorCardBackground = $fileCardBackground["error"];
    $fileSizeCardBackground = $fileCardBackground["size"];
    $fileCardBackgroundExtension = explode(".", $fileNameCardBackground);
    $fileCardBackgroundActualExtension = strtolower(end($fileCardBackgroundExtension));

    $fileCardIcon = $_FILES['fileCardIcon'];
    $fileNameCardIcon = $fileCardIcon["name"];
    $fileTypeCardIcon = $fileCardIcon["type"];
    $fileTempNameCardIcon = $fileCardIcon["tmp_name"];
    $fileErrorCardIcon = $fileCardIcon["error"];
    $fileSizeCardIcon = $fileCardIcon["size"];
    $fileCardIconExtension = explode(".", $fileNameCardIcon);
    $fileCardIconActualExtension = strtolower(end($fileCardIconExtension));

    $allowed = array("jpeg", "jpg", "png", "JPEG", "JPG", "PNG");

    if (in_array($fileCardBackgroundActualExtension, $allowed) && in_array($fileCardIconActualExtension, $allowed)) {
        if ($fileErrorCardBackground === 0 && $fileErrorCardIcon === 0) {

            $imageFullNameCardBackground = $newFileNameCardBackground . "." . uniqid("", true) . "." . $fileCardBackgroundActualExtension;
            $fileDestinationCardBackground = "../../img/card_background/" . $imageFullNameCardBackground;

            $imageFullNameCardIcon = $newFileNameCardIcon . "." . uniqid("", true) . "." . $fileCardIconActualExtension;
            $fileDestinationCardIcon = "../../img/card_logo/" . $imageFullNameCardIcon;

            include 'connection.php';

            if (empty($appName) && empty($appDescription) && empty($appGoogleGamesIcon)) {
                header("Location: ../../admin/admin-main.php?upload=SelectedFields-MUST-NOT-BeEmpty");
                exit();
            } else {
                $sql = "SELECT * FROM apps;";
                $statement = mysqli_stmt_init($conn);
                if (!mysqli_stmt_prepare($statement, $sql)) {
                    echo "SQL statment failed";
                } else {
                    mysqli_stmt_execute($statement);
                    $result = mysqli_stmt_get_result($statement);
                    $rowCount = mysqli_num_rows($result);

                    $sql = "INSERT INTO apps (appName, appDescription, appLinkFacebook, appLinkInstagram, appLinkPlaystore, appLinkWeb,
                        appGoogleGamesIcon, appFullImageNameBackground, appFullImageNameIcon) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);";

                    if (!mysqli_stmt_prepare($statement, $sql)) {
                        echo "SQL statment failed";
                    } else {
                        mysqli_stmt_bind_param(
                            $statement,
                            "sssssssss",
                            $appName,
                            $appDescription,
                            $appLinkFacebook,
                            $appLinkInstagram,
                            $appLinkPlaystore,
                            $appLinkWeb,
                            $appGoogleGamesIcon,
                            $appFullImageNameBackground,
                            $appFullImageNameIcon
                        );
                        mysqli_stmt_execute($statement);

                        move_uploaded_file($fileTempNameCardBackground, $fileDestinationCardBackground);
                        move_uploaded_file($fileTempNameCardIcon, $fileDestinationCardIcon);

                        header("Location: ../../admin/admin-main.php?upload=success");
                    }
                }
            }
        } else {
            echo "You have an error";
            exit();
        }
    } else {
        echo "Yopu need to upload a proper file type";
        exit();
    }
}

总而言之,当我手动输入数据时,sql SELECT可以正常工作,图像应该以正确的名称显示,并且没有错误。

谢谢:D

1 个答案:

答案 0 :(得分:1)

通过在我的sql语句上方使用此命令发现了问题。现在一切正常。 感谢您的帮助。

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);