0

I have a webpage where I ask input from the user and when he clicks the add button, it should insert the input from the user into the database. I always thought that you couldn't use POST and GET at the same time but according to this and this answers it should be possible. Yet, I can't seem to get their solutions working.

The script inserts something in the db (an id is generated) but the seriesName field remains empty.

The PHP script:

<?php
$con=mysqli_connect("localhost","user","passwd","database");

// Check connection

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$issueSeries = mysqli_real_escape_string($con, $_GET['addSeries']);

mysqli_query($con,"INSERT INTO series (seriesName) VALUES ('$issueSeries')");

mysqli_close($con);
?>

The HTML:

<div id="issueAddInformationLayout">
    <div id="issueAddCredits">
        <form action="addIssue.php" method="post">
            <table>
                <tr id="lblAddCreator">
                    <td><p>NR</p></td>
                    <td><p>Series</p></td>
                    <td><p>Volume</p></td>
                    <td><p>Title</p></td>
                    <td><p>Publisher</p></td>
                </tr>
                <tr>
                    <td><input type="text" id="addNR" size="3%"/></td>
                    <td><input type="text" id="addSeries" size="25%" /></td>
                    <td><input type="text" id="addVolume" size="25%" /></td>
                    <td><input type="text" id="addTitle" size="30%" /></td>
                    <td><input type="text" id="addPublisher" size="30%" /></td>
                </tr>
            </table>
            <table>
                <tr id="lblAddCreator">
                    <td><p>Writer</p></td>
                    <td></td>
                    <td></td>
                </tr>
                <tr id="txtAddCreator">
                    <td><input type="text" id="addWriter1" size="30%" /></td>
                    <td><input type="text" id="addWriter2" size="30%" /></td>
                    <td><input type="text" id="addWriter3" size="30%"/></td>
                    <td><input type="text" id="addWriter4" size="30%"/></td>
                </tr>
                <tr id="lblAddCreator">
                    <td><p>Editor</p></td>
                    <td></td>
                    <td></td>
                </tr>
                <tr id="txtAddEditor">
                    <td><input type="text" id="addEditor1" size="30%" /></td>
                    <td><input type="text" id="addEditor2" size="30%"/></td>
                    <td><input type="text" id="addEditor3" size="30%"/></td>
                    <td><input type="text" id="addEditor4" size="30%"/></td>
                </tr>
                <tr id="lblAddCreator"> 
                    <td><p>Letterer</p></td>
                    <td></td>
                    <td></td>
                </tr>
                <tr id="txtAddLetterer">
                    <td><input type="text" id="addLetterer1" size="30%"/></td>
                    <td><input type="text" id="addLetterer2" size="30%"/></td>
                    <td><input type="text" id="addLetterer3" size="30%"/></td>
                    <td><input type="text" id="addLetterer4" size="30%"/></td>
                </tr>
                <tr id="lblAddCreator">
                    <td><p>Colourist</p></td>
                    <td></td>
                    <td></td>
                </tr>
                <tr id="txtAddColourist">
                    <td><input type="text" id="addColourist1" size="30%"/></td>
                    <td><input type="text" id="addColourist2" size="30%"/></td>
                    <td><input type="text" id="addColourist3" size="30%"/></td>
                    <td><input type="text" id="addColourist4" size="30%"/></td>
                </tr>
                <tr id="lblAddCreator">
                    <td><p>Cover Artist</p></td>
                    <td></td>
                    <td></td>
                </tr>
                <tr id="txtAddCoverArtist">
                    <td><input type="text" id="addCoverArtist1" size="30%"/></td>
                    <td><input type="text" id="addCoverArtist2" size="30%"/></td>
                    <td><input type="text" id="addCoverArtist3" size="30%"/></td>
                    <td><input type="text" id="addCoverArtist4" size="30%"/></td>
                </tr>
                <tr id="lblAddCreator">
                    <td><p>Inker</p></td>
                    <td></td>
                    <td></td>
                </tr>
                <tr id="txtAddInker">
                    <td><input type="text" id="addInker1" size="30%"/></td>
                    <td><input type="text" id="addInker2" size="30%"/></td>
                    <td><input type="text" id="addInker3" size="30%"/></td>
                    <td><input type="text" id="addInker4" size="30%"/></td>
                </tr>
                <tr id="lblAddCreator">
                    <td><p>Penciler</p></td>
                    <td></td>
                    <td></td>
                    </tr>
                <tr id="txtAddPenciler">
                    <td><input type="text" id="addPenciler1" size="30%"/></td>
                    <td><input type="text" id="addPenciler2" size="30%"/></td>
                    <td><input type="text" id="addPenciler3" size="30%"/></td>
                    <td><input type="text" id="addPenciler4" size="30%"/></td>
                </tr>
                <tr id="lblAddCreator">
                    <td><p>Trade Paper Back</p></td>
                    <td><p id="lblAddCover">Cover</p></td>
                </tr>
            </table>
            <table>
                <tr id="txtAddTPB">
                    <td><input type="text" id="addTPB" size="30%"/></td>
                    <td>
                        <p id="btnAddCover" enctype="multipart/form-data" action="parse_file.php" method="post"> 
                            <input type="file" name="uploaded_file">
                        </p>
                    </td>
                    <td>
                        <id="btnAddIssue"><input type="submit" value="Add">
                    </td>
                </tr>
            </table>
        </form>
    </div>
</div>

There seemed to be some confusion about the HTML so I added the full HTML code. Keep in mind: this is purely for myself and I don't really care about stuff like that tables are outdated :)

Community
  • 1
  • 1
Noosrep
  • 416
  • 10
  • 24

4 Answers4

1

In HTTP, you can't use GET and POST at the same time but you can make a POST request which has a query string as part of the URL.

PHP will populate $_POST with the body of the request and $_GET with the query string, even if the request was a POST request.

Since the form is POST, the data from the inputs will be put into the body and not the query string (so will appear in $_POST not $_GET).

If the value of addSeries was fixed then you could use it with $_GET like so:

 <form id="btnAddIssue" action="addIssue.php?addSeries=someValue" method="post">

… but since you are taking user input, use $_POST to read the value after you put the input inside the form.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Your input is outside of your form so it isn't submitted.

<form id="btnAddIssue" action="addIssue.php" method="post">
    <input type="text" id="addSeries" size="25%" />
   <input type="submit" value="Add">
</form>

You then need to change $_GET['addSeries'] to $_POST['addSeries'] since your form is set to POST (or change your form to submit via GET).

John Conde
  • 217,595
  • 99
  • 455
  • 496
0

Your input is outside the form tag which won't be considered when user submits the form.

<form id="btnAddIssue" action="addIssue.php" method="post">
   <input type="text" id="addSeries" size="25%" />
   <input type="submit" value="Add">
</form>

And since you are making a post request onto php side it would be $_POST to reference that input's value

Also, sometimes it would be good to make use of $_REQUEST which contains data from both GET and POST so you have proper data from request everytime

$issueSeries = mysqli_real_escape_string($con, $_REQUEST['addSeries']);
rsakhale
  • 1,018
  • 13
  • 26
  • 2
    **Warning**: `$_REQUEST` also includes data from cookies. I'd generally avoid it in favour of knowing exactly where your data was coming from. – Quentin May 20 '14 at 12:06
0

Your text filed is placed outside the form and so no value (of that text filed) will be submitted to the action page.

Change it to

<form id="btnAddIssue" action="addIssue.php" method="post">
<input type="text" id="addSeries" size="25%" />
<input type="submit" value="Add">
</form>

Also use $_POST, or you can even use $_REQUEST (you can use $_REQUEST for both get and post variables)

AeJey
  • 1,447
  • 20
  • 40