【问题标题】:HTML form input errors PHPHTML 表单输入错误 PHP
【发布时间】:2016-01-26 06:38:40
【问题描述】:
            <form action="index.php" name="Submit_Form" method="post" enctype="multipart/form-data" id="upload" class="upload">
        <fieldset>
            <legend>Upload</legend><br/>
            Title:  <input type="text" name="title" id="name" class="name" required> <br/><br/>
            <textarea name="description" rows="6" cols="35" maxlength="120"></textarea><br/>
            <input type="file" id="file" name="file[]" required multiple><br/>
            <input type="submit" id="submit" name="submit" value="Upload">
        </fieldset> 


        <div class="bar">
            <span class="bar-fill" id="pb"><span class="bar-fill-text" id="pt"></span></span>
        </div>

        <div id="uploads" class="uploads"> 
        Uploaded file links will appear here.   
        </div>

<?php

if (isset($_POST['title'])) {
$title = $_POST['title'];
}

if (isset($_POST['description'])) {
$description = $_POST['description'];
}

$dbhost     = "localhost";

$dbname     = "blog";

$dbuser     = "root";

$dbpass     = "";



// database connection

$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);


// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// query

$stmt = $conn->prepare("INSERT INTO videos (title,description) VALUES (?,?)");

$stmt->bind_param("ss", $title, $description);

$stmt->execute();

$stmt->close();
$conn->close();
 ?> 

 <script src="upload.js"></script>
        <script>

            document.getElementById('submit').addEventListener('click', function(e) {
                e.preventDefault();

                var f = document.getElementById('file'),
                    pb = document.getElementById('pb'),
                    pt = document.getElementById('pt');


                    blog.uploader({
                    files: f,
                    progressBar: pb,
                    progressText: pt,
                    processor: 'uploads.php',

                    finished: function(data) {
                        var uploads = document.getElementById('uploads'),
                        succeeded = document.createElement('div'),
                        failed = document.createElement('div'),

                        anchor,
                        span,
                        x;

                    if(data.failed.length) {
                        failed.innerHTML = '<p>This item failed to upload:</p>';
                    }

                    uploads.textContent = '';

                    for(x = 0; x < data.succeeded.length; x = x + 1) {
                        anchor = document.createElement('a');
                        anchor.href = 'uploads/' + data.succeeded[x].file;
                        anchor.textContent = data.succeeded[x].name;
                        anchor.target = '_blank';

                        succeeded.appendChild(anchor);  
                    }

                    for(x = 0; x < data.failed.length; x = x + 1) {
                        span = document.createElement('span');
                        span.textContent = data.failed[x].name;

                        failed.appendChild(span);

                    }
                    uploads.appendChild(succeeded);
                    upload.appendChild(failed);
                },

                    error: function() {
                        console.log('Not working'); 
                    }
                });
            });
        </script>
    </form>
</body>
</html> 

我与“PHP 专家”谈了几个小时,他似乎无法弄清楚我的问题。我的输入没有提交到我的数据库、标题/描述。我可以使用

$title = $_POST['dsfasfddsfa']; 

它会插入,但我无法将用户输入的内容插入该字段。当我添加

$title = $_POST['title']; & $description = $_POST['description']; 

我在标题和描述上获得了未识别的索引,这就是我在我的 php 代码顶部添加 if 语句的原因。

我在一个网站上看到它应该删除它们,我认为一旦它们消失它就会起作用,我错了。当我这样做时,没有任何效果。我可以上传视频,但这些字段不起作用。任何和所有的帮助都很棒!在此先感谢。如果php代码不起作用,我还在两个if语句中使用提交作为$_POST。

【问题讨论】:

  • 你的结束&lt;/form&gt;元素在哪里?
  • 尝试添加print_r($_POST); 以调试所有POST 变量。查看标题和描述是否在数组中。
  • @David 您正在尝试使用仅指定 2 个表字段的插入查询插入 3 个值。第三个值为空
  • @David 根据你的截图你不是,SQL 错误显示你正在尝试插入第三个空白值('fad','dfads',''
  • 当它不是那样的时候,我不知道如何解决这个问题。 :/ gyazo.com/7ae3e08f3df6585dfd7fafbc1a29d8aa 同上。呃呃 D:

标签: php html forms mysqli


【解决方案1】:

试着先做一个简单的版本,就像这个例子一样。

http://php.net/manual/es/tutorial.forms.php

<form action="action.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
 <p>Your age: <input type="text" name="age" /></p>
 <p><input type="submit" /></p>
</form>

.

Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.

然后你会一步一步地改变你想要的东西。

最后会工作。

【讨论】:

    猜你喜欢
    • 2013-11-28
    • 1970-01-01
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-24
    • 1970-01-01
    相关资源
    最近更新 更多