0

Hello guys i need some help.I connected to database from server and can insert some info like $sql = "INSERT INTO Posts (Text_Post) VALUES ('Sample Text')";. Now I want to save on click text from <input type="text" /> to database. Can you tell me what i am doing wrong.

 <?php

      $servername = "google.com";
      $username = "google";
      $password = "google";
      $dbname = "google";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    if(isset($_POST['Submit'])) {
            $sql = "INSERT INTO Posts (Text_Post) VALUES ('".$_POST['text']."')";

            if ($conn->query($sql) === TRUE) {
                echo "New record created successfully";
            } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
            }

            $conn->close();
    }
  ?>

  <!DOCTYPE html>
  <html>
  <head>
    <title>anonim</title>
  </head>
  <body>
    <form name="form" action="" method="post">
      <input type="text" name="text" id="text" value="Salut" /=>
      <input type="submit" id="Submit" /> 
    </form>
  </body>
  </html>
2
  • 1
    what error do you receive? Commented Mar 10, 2016 at 9:59
  • 1
    @dr4605 your code is open to SQL Injection, never use input values directly from the $_POST or $_GET variables. Commented Mar 10, 2016 at 10:01

2 Answers 2

4

You're missing the name tag on your submit. When data is POST'ed to the server, it uses the name tag.

<input type="submit" id="submit" name="Submit">

Remember to watch your Capitals also - (since you're checking if Submit is SET then you need to POST the submit).

You could just do:

if(isset($_POST['text'])) {

Also, going off the comments: I'd suggest taking a look at this link because you're prone to SQL Injections.

Sign up to request clarification or add additional context in comments.

Comments

0

when we are going to post a form using POST or GET. we should always give name to all our fieds so we get get them just using $_POST['name'] or $_GET['name']. In Your case just give a name to your submit tag and check whether data is submitted or not.

replace

<input type="submit" id="Submit" />  

with

<input type="submit" id="submit" name="submit">

and check it like

if(isset($_POST['submit'])) {// it will only check where form is posted or not

// your code...

}

3 Comments

replace <input type="submit" id="Submit" />
with <input type="submit" name="submit" />
sorry these lines left in my answer please check these two lines... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.