【问题标题】:Blank page when submitting form data to mysql with PHP [duplicate]使用PHP将表单数据提交到mysql时出现空白页面[重复]
【发布时间】:2016-12-10 21:19:54
【问题描述】:

我正在尝试将表单数据提交到我的 MySQL 数据库,但是当我按下提交时,浏览器页面保持完全白色(错误报告已打开)并且没有数据提交到数据库,我的代码低于任何帮助将不胜感激: )

<?php
    session_start();

    include_once("db.php");

    if(isset($_POST['widget_name']) && isset ($_POST['widget_type']) && isset ($_POST['language_support']) && isset ($_POST['widget_description']) && isset ($_POST['widget_dependencies'])){   
        if($_POST['widget_name'] != "" && $_POST['widget_type'] != "" && $_POST['language_support'] != "" && $_POST['widget_description'] != "" && $_POST['widget_dependencies'] != ""){
            $name = $_POST['widget_name'];
            $type = $_POST['widget_type'];
            $language = $_POST['language_support'];
            $description = $_POST['widget_description'];
            $dependencies = $_POST['widget_dependencies'];

            $sql_store = "INSERT INTO 'submissions' (ID, Author, Widget_Name, Widget_Type, Language_Support, Description, Dependencies, Icon) VALUES ('', '', '$name', '$type', '$language', '$description', '$dependencies', '')";
            $sql = mysqli_query($db, $sql_store) or die(mysql_error());

        } else {echo "<span class='enter-data' id='enter-data'>You need to enter data in all fields.</span>";}

    } else {echo "<span></span>";}

    /* if(isset($_POST['submit'])){

            $iconName = mysql_real_escape_string($_FILES["image"]["name"]);
            $iconData = mysql_real_escape_string(file_get_contents ($_FILES["image"]["tmp_name"]));
            $imageType = mysql_real_escape_string($_FILES["image"]["type"]);

            if(substr($imageType,0,5) == "image"){
                mysqli_query("INSERT INTO 'submissions' VALUES('','$ImageName','$imageData') ")
            } else {echo "Only images allowed.";}

    } */

?>

<!DOCTYPE html>
<html>
</head>
    <title>Test</title>
    <style>
    body{font-family:Helvetica;}

    span{margin-left:10; width:30%; display:inline-block;} 

    input{margin-top:10; width: 60%;}

    #submission-form{float:left;}
    #text-inputs{width:500px; background:red; border-radius:15px; display:inline-block;}
    #image-upload{width:500px; background:blue; border-radius:15px; display:inline-block; vertical-align:top; height: 250; width: 250px;}

    .description-box{margin-top:10; min-width:60%; max-width:60%; height:100;}
    .dependencies-box{margin-bottom:10;}
    .submit-button{float:right; width:20%; margin-top:-10; margin-right:10;}

    input[type='file'] {
  color: transparent;
}
    </style>    
<head>

<body>

<div id="submission-form">
    <form action="index.php" method="POST" enctype="multipart/form-data">
                <div id="image-upload">
            <span style="width:auto; margin-top:10;">Widget Icon:</span>
            <input style="float:right; margin-right: -15;" type="file" name="widget_icon" onchange="loadFile(event)">
            <center><img style="width: 80%; height: auto;  margin-top:5;" src="http://placehold.it/500x500" id="output"></center>
                <script>
                    var loadFile = function(event) {
                        var output = document.getElementById('output');
                    output.src = URL.createObjectURL(event.target.files[0]);
                    };
                </script>
        </div>


        <div id="text-inputs">
            <span>Widget Name: </span><input type="text" name="widget_name" value="">
            <br>
            <span>Widget Type: </span><input type="text" name="widget_type" value="">
            <br>
            <span>Language Support: </span><input type="text" name="language_support" value="">
            <br>
            <span style="vertical-align:top; padding-top:10;}">Description: </span><textarea rows="1" cols="26" name="widget_description" value="" class="description-box"></textarea>
            <br>
            <span>Dependencies: </span><input type="text" name="widget_dependencies" value="" class="dependencies-box">
            <br>
        </div>

        <br>

        <br>

        <input type="submit" name="submit_data" value="Submit" class="submit-button">   
    </form>
</div>


</body>
</html>

【问题讨论】:

  • 空白页表示您有错误。查找您的错误日志/启用错误报告并阅读它们。
  • 另外,您正在为您的 id 列添加 empty('') 。你确定这是对的吗?
  • or die(mysql_error()) 错误有两个原因。

标签: php html mysql database forms


【解决方案1】:

您的 INSERT 语句是错误的。表名不需要单引号。改为:

$sql_store = "INSERT INTO submissions (ID, Author, Widget_Name, Widget_Type, Language_Support, Description, Dependencies, Icon) VALUES ('', '', '$name', '$type', '$language', '$description', '$dependencies', '')";

请注意,此代码允许 sql 注入。看看:How can I prevent SQL injection in PHP?

【讨论】:

  • 解决了,谢谢! :)
  • 很高兴我能帮上忙! =]
  • 您错过了 OP 在查询中没有收到任何错误的事实,因为他们没有使用正确的功能
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多