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.
我有一个网页,我要求用户输入,当他点击添加按钮时,它应该将用户的输入插入数据库。我一直以为你不能同时使用POST和GET,但根据这个和这个答案它应该是可能的。然而,我似乎无法让他们的解决方案有效。
The script inserts something in the db (an id is generated) but the seriesName field remains empty.
该脚本在db中插入一些内容(生成一个id),但seriesName字段仍为空。
The PHP script:
PHP脚本:
<?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 :)
似乎有一些关于HTML的混淆,所以我添加了完整的HTML代码。请记住:这纯粹是为了我自己,我并不真正关心那些表格过时的东西:)
4 个解决方案
#1
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.
在HTTP中,您不能同时使用GET和POST,但您可以发出一个POST请求,该请求具有查询字符串作为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.
PHP将使用请求的主体填充$ _POST,并使用查询字符串填充$ _GET,即使请求是POST请求也是如此。
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
).
由于表单是POST,来自输入的数据将被放入正文而不是查询字符串(因此将出现在$ _POST而不是$ _GET中)。
If the value of addSeries
was fixed then you could use it with $_GET
like so:
如果addSeries的值是固定的,那么你可以像$ _GET那样使用它:
<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.
...但是由于您正在接受用户输入,因此在将输入放入表单后使用$ _POST读取值。
#2
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).
然后,您需要将$ _GET ['addSeries']更改为$ _POST ['addSeries'],因为您的表单设置为POST(或更改您的表单以通过GET提交)。
#3
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
而且由于你在php端发出了一个post请求,因此引用该输入的值将是$ _POST
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
此外,有时最好使用$ _REQUEST,其中包含来自GET和POST的数据,因此您每次都可以获得适当的数据
$issueSeries = mysqli_real_escape_string($con, $_REQUEST['addSeries']);
#4
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)
也可以使用$ _POST,或者你甚至可以使用$ _REQUEST(你可以使用$ _REQUEST获取get和post变量)
#1
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.
在HTTP中,您不能同时使用GET和POST,但您可以发出一个POST请求,该请求具有查询字符串作为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.
PHP将使用请求的主体填充$ _POST,并使用查询字符串填充$ _GET,即使请求是POST请求也是如此。
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
).
由于表单是POST,来自输入的数据将被放入正文而不是查询字符串(因此将出现在$ _POST而不是$ _GET中)。
If the value of addSeries
was fixed then you could use it with $_GET
like so:
如果addSeries的值是固定的,那么你可以像$ _GET那样使用它:
<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.
...但是由于您正在接受用户输入,因此在将输入放入表单后使用$ _POST读取值。
#2
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).
然后,您需要将$ _GET ['addSeries']更改为$ _POST ['addSeries'],因为您的表单设置为POST(或更改您的表单以通过GET提交)。
#3
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
而且由于你在php端发出了一个post请求,因此引用该输入的值将是$ _POST
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
此外,有时最好使用$ _REQUEST,其中包含来自GET和POST的数据,因此您每次都可以获得适当的数据
$issueSeries = mysqli_real_escape_string($con, $_REQUEST['addSeries']);
#4
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)
也可以使用$ _POST,或者你甚至可以使用$ _REQUEST(你可以使用$ _REQUEST获取get和post变量)