diegoaguilar
diegoaguilar

Reputation: 8376

Empty fields after POST with HTML / PHP?

For learning purposes I'm trying to insert data from HTML form to a MySQL database, this is the code I've got for the form:

<form action="newmovie.php" method="post">

            <p>Titulo:  <input type="text" name="movieTitle"> </p>
            <p>Año:  <input type="text" name="movieYear"> </p>
            <p>Director:  <input type="text" name="movieDirector"> </p>
            <p>Protagonista:  <input type="text" name =movieProtagonist> </p>
            <input type="submit"  class="button" value="Agregar">

</form>

So, in same webpage I check if a POST is being performed:

<?php
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        include("connectDB.php");
        $mySQL=new MySQL();

        $movieTitle=$_GET['movieTitle']; 
        $movieDirector=$_GET['movieDirector'];    
        $movieYear=$_GET['movieYear'];    
        $movieProtagonist=$_GET['movieProtagonist'];
        echo "INSERT INTO peliculas (titulo,director,year, protagonista) VALUES ('$movieTitle','$movieDirector','$movieYear', '$movieProtagonist')";
        $queryResult=$mySQL->query("INSERT INTO peliculas (titulo,director,year, protagonista) VALUES ('$movieTitle','$movieDirector','$movieYear', '$movieProtagonist')");
        echo "$queryResult";    
    }
?>

So, inserting is done BUT the fields are empty, this is what the echo prints:

INSERT INTO peliculas (titulo,director,year, protagonista) VALUES ('','','', '')

What's that I'm doing wrong with my code?

Upvotes: 1

Views: 2065

Answers (6)

ashutosh madheshiya
ashutosh madheshiya

Reputation: 11

Since method='post'

Instead of $_GET['']

use either

$_POST['']

or

$_REQUEST['']

Upvotes: 1

Yanki Twizzy
Yanki Twizzy

Reputation: 8001

<form action="newmovie.php" method="post">

Since your form method is post, you need to use

$movieTitle=$_POST['movieTitle'];

You could alternatively use

$movieTitle=$_REQUEST['movieTitle'];

for both post and get but this would cause unneccessary overhead

Upvotes: 1

Ram Sharma
Ram Sharma

Reputation: 8819

try the code below, it will fix your issue.

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $movieTitle=$_POST['movieTitle']; 
    $movieDirector=$_POST['movieDirector'];    
    $movieYear=$_POST['movieYear'];    
    $movieProtagonist=$_POST['movieProtagonist'];
    echo "INSERT INTO peliculas (titulo,director,year, protagonista) VALUES ('".$movieTitle."','".$movieDirector."','".$movieYear."', '".$movieProtagonist."')";
    $queryResult=$mySQL->query("INSERT INTO peliculas (titulo,director,year, protagonista) VALUES ('".$movieTitle."','".$movieDirector."','".$movieYear."', '".$movieProtagonist."')");
    echo "$queryResult";    
}

?>

Upvotes: 1

LondonAppDev
LondonAppDev

Reputation: 9653

Fix the typo here

<p>Protagonista:  <input type="text" name =movieProtagonist> </p>

by changing it to

<p>Protagonista:  <input type="text" name="movieProtagonist"> </p>

Change the PHP to:

<?php
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        include("connectDB.php");
        $mySQL=new MySQL();

        $movieTitle=$_POST['movieTitle']; 
        $movieDirector=$_POST['movieDirector'];    
        $movieYear=$_POST['movieYear'];    
        $movieProtagonist=$_POST['movieProtagonist'];
        echo "INSERT INTO peliculas (titulo,director,year, protagonista) VALUES ('$movieTitle','$movieDirector','$movieYear', '$movieProtagonist')";
        $queryResult=$mySQL->query("INSERT INTO peliculas (titulo,director,year, protagonista) VALUES ('$movieTitle','$movieDirector','$movieYear', '$movieProtagonist')");
        echo "$queryResult";    
    }
?>

Explanation:

You can submit a form to a PHP script by using HTTP GET or HTTP POST. You define this in the form. In your form, you set it to send it via POST but were trying to retrieve it in php using $_GET.

Get is good for if you need to send URLS and should only be used for retrieving data from the database. Get variables are passed like this

http://yourserver.com/yourscript.php?variable1=something&variable2=somethingElse

And post is hidden in the HTTP request (you don't see it in the URL). It's better to use 'post' for any request that is making changes to the DB.

You can read more about the differences here: http://blog.teamtreehouse.com/the-definitive-guide-to-get-vs-post

Upvotes: 2

deviloper
deviloper

Reputation: 7240

Use $_POST to read form data: example: $movieTitle=$_POST['movieTitle'];

Upvotes: 1

Code Lღver
Code Lღver

Reputation: 15593

In PHP file instead of the

$_GET

use the

$_REQUEST

And your problem will solve as you are using the method='post' in the form tag. And you are getting the data by $_GET which is used to get the data of method get.

Always use the $_REQUEST to get the data from form as it is common to get the both data from get and post.

Upvotes: 1

Related Questions