-1

I have an html form with five inputs, named title, dateposted, content, tags and submit. I want to store the inputs in variables in a php file, then post them to a mysql database. I'm using PHPstorm and when I hover over $_POST it says reference error: $_POST is not defined. How do I fix this reference error? Is that why it's not submitting to the database?

My html form:

<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 Code:

    <?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);
ethacker
  • 131
  • 1
  • 4
  • 17
  • The notices are on `insert.php` and the code here is `insert.php`? You are open to SQL injections with this code and are using the wrong encapsulation on the columns, it should be backticks. – chris85 Mar 18 '17 at 21:44
  • 1
    Okay, so I added the backticks. The $_POST is still showing ReferenceError – ethacker Mar 18 '17 at 21:55
  • `ReferenceError` sounds like a browser/console notice. Can you post the literal error? I thought you were getting PHP notices about the indices. – chris85 Mar 18 '17 at 21:58
  • 1
    It's in phpstorm when you hover over "$_POST". I can't seem to get a screenshot of it though. – ethacker Mar 19 '17 at 18:00
  • Here is about tick and backtick https://superuser.com/questions/254076/how-do-i-type-the-tick-and-backtick-characters-on-windows/254077 – b2ok Mar 19 '17 at 18:56

1 Answers1

-1

Here:

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

must write without quote:

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

I tested your code and it works ok when remove quote, with table like this:

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;
b2ok
  • 544
  • 6
  • 13
  • 1
    I did that originally but did as Chris85 said. It still doesn't work though, I think it's something to do with the $_POST – ethacker Mar 19 '17 at 04:37
  • @ethacker I'm still awaiting second comment response. Also double quotes are not backticks.. `"` != `\`` – chris85 Mar 19 '17 at 05:25
  • Your published script is ok when you remove quote. Problem is with table.Export SQL of table and copy here. – b2ok Mar 19 '17 at 12:34
  • I create your database and table and complete your code, removed quote, and all works excelent! – b2ok Mar 19 '17 at 12:52
  • 1
    @b2ok my table is on the phpmyadmin page, with the wamp server. how do I create the table in phpstorm? – ethacker Mar 19 '17 at 18:03
  • backtics in croatian translated by Google means single closed quote. Thanks @chris85 for that new word for me. – b2ok Mar 19 '17 at 18:36
  • In phpMyAdmin click left up on New (database) and write name od database in fild Database name. Enter. Click on SQL and copy my SQL for table and Go. – b2ok Mar 19 '17 at 18:40
  • I now look at with more focus on @chris85 comment about backtics = I can't find where it is on my keyboard. Single quote = ' and Double quote = ". Backstick there is not any influence on code, only for better view for developers. – b2ok Mar 19 '17 at 18:52
  • Here's a good explanation/and display of them. http://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-backticks-in-mysql Single quotes vs. backticks are very different. Nothing vs backticks isn't much of a difference unless the term is reserved. – chris85 Mar 19 '17 at 19:08