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>
strdr4605
  • 3,796
  • 2
  • 16
  • 26

2 Answers2

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.

Community
  • 1
  • 1
Jaquarh
  • 6,493
  • 7
  • 34
  • 86
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...

}
Jaquarh
  • 6,493
  • 7
  • 34
  • 86
Passionate Coder
  • 7,154
  • 2
  • 19
  • 44