PHPStorm表单处理:PHP变量不回显,也不发布到sql

时间:2017-03-18 21:41:25

标签: php html mysql forms

我有一个带有五个输入的html表单,名为title,dateposted,content,tags和submit。我想将输入存储在php文件中的变量中,然后将它们发布到mysql数据库中。我正在使用PHPstorm,当我将鼠标悬停在$ _POST上时,它会显示引用错误:$ _POST未定义。如何修复此引用错误?这就是为什么它没有提交到数据库?

我的HTML格式:

<div class="section" id="home">
        <form action="insert.php" method="post">
            <fieldset>
                <legend>Title</legend>
                <input id="title" name="title" placeholder="Title" type="text"/>
                <input id="dateposted" type="text" placeholder="mm-dd-yyyy" name="dateposted"/>
            </fieldset>

            <fieldset>
                <label>Content</label>
                <textarea id="content" placeholder="content" name="content"> </textarea>
            </fieldset>

            <fieldset>
                <label>Tags</label>
                <input id="tags" type="text" placeholder="tags" name="tags"/>
            </fieldset>

            <fieldset>
                <input id="submit" type="submit" value="Submit" name="Submit"/>
            </fieldset>
        </form>
    </div>

PHP代码:

    <?php
error_reporting(E_ALL);ini_set("display_error",true);
define('DB_NAME', 'blog_posts');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');

$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {
    die('Could not connect: ' . mysqli_error($link));
}

$db_selected = mysqli_select_db($link, DB_NAME);

if (!$db_selected) {
    die('Can\'t use ' . DB_NAME . ': ' . mysqli_error($link));
}

//echo 'Connected successfully';

if(isset($_POST['Submit'])) {
    $title = $_POST['title'];
    $dateposted = $_POST['dateposted'];
    $content = $_POST['content'];
    $tags = $_POST['tags'];


    $sqli = "INSERT INTO posts 
    ("title", "dateposted", "content", "tags") 
          VALUES 
    ('$title','$dateposted', '$content', '$tags')";

    if (!mysqli_query($link, $sqli)) {
        die('Error: ' . mysqli_error($link));
    }
}

mysqli_close($link);

1 个答案:

答案 0 :(得分:0)

下面:

$sqli = "INSERT INTO posts 
    ("title", "dateposted", "content", "tags") 
          VALUES 
    ('$title','$dateposted', '$content', '$tags')";

必须写不带引号:

$sqli = "INSERT INTO posts 
    (title, dateposted, content, tags) 
          VALUES 
    ('$title','$dateposted', '$content', '$tags')";

我测试了你的代码,当删除引用时它可以正常工作,如下所示:

CREATE TABLE `posts` (
  `id` int(11) NOT NULL,
  `title` varchar(100) DEFAULT NULL,
  `dateposted` date DEFAULT NULL,
  `content` text,
  `tags` varchar(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;