krizpers
krizpers

Reputation: 107

HTML form not populating MySQL DB as expected

When I save a form from html to php and finally store it in MySQL somewhere in that line it save the var= including what comes after the =

Here is my html:

   <form action="searchResultsSave.php" method="POST">
     What are we looking for? <input type="text" name="searchVar" />
     <input type="submit" value="Submit">
   </form>

Php:

    $searchVar = file_get_contents('php://input');

    $sql = "INSERT INTO g_information(searchVar) VALUES ('$searchVar')";

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

Finally my output in mysql is: "searchVar=cars" when it should just be "cars".

Where do you think I went wrong?

Upvotes: 0

Views: 46

Answers (2)

Tadasbub
Tadasbub

Reputation: 196

You should read input variable from the form

    <?php
    $_POST["searchVar"];
    ?>

Then do some validation on the input, making sure no illegal characters are entered and data is safe to store in MySQL database

    <?php
    $_POST['searchVar'] = filter_var($_POST['searchVar'], FILTER_SANITIZE_STRING);
    $sql = "INSERT INTO g_information(searchVar) VALUES ("'.$_POST['searchVar'].'")"; 
    ?>

Upvotes: 1

Suman B
Suman B

Reputation: 84

$searchVar = file_get_contents('php://input'); 

should be

$searchVar = $_POST['searchVar'];

This way you get the value of the search term.

Upvotes: 1

Related Questions