注意:未定义变量:在 C:\xampp\htdocs\imp02\week5\firstPHPDatabase.php 第 25 行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20633878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Notice: Undefined variable: in C:\xampp\htdocs\imp02\week5\firstPHPDatabase.php on line 25
提问by Justin Veenis
I need to insert and extract data from a MySQL database. I'm able to extract info, but when I try to insert it, it gives me a lot of error messages. Most of them I was able to resolve, but this one I just can't seem to figure out:
我需要从 MySQL 数据库中插入和提取数据。我能够提取信息,但是当我尝试插入它时,它给了我很多错误消息。其中大多数我都能够解决,但这个我似乎无法弄清楚:
Notice: Undefined variable: emailInput in C:\xampp\htdocs\imp02\week5\firstPHPDatabase.php on line 25
Notice: Undefined variable: aanmelding in C:\xampp\htdocs\imp02\week5\firstPHPDatabase.php on line 25
Notice: Undefined variable: ipadres in C:\xampp\htdocs\imp02\week5\firstPHPDatabase.php on line 25
Notice: Undefined variable: opmerkingen in C:\xampp\htdocs\imp02\week5\firstPHPDatabase.php on line 25
Notice: Undefined variable: aantalPersonen in C:\xampp\htdocs\imp02\week5\firstPHPDatabase.php on line 25
My code:
我的代码:
<?php
$databaseLink = mysqli_connect('localhost', 'root', '', 'newyearseveparty');
if (mysqli_connect_error())
echo mysqli_connect_error();
$selectorQuery = "SELECT * FROM attendants";
echo "The query We use is $selectorQuery!";
$attendants = array();
if ($result = mysqli_query($databaseLink, $selectorQuery)) {
while ($tableRow = mysqli_fetch_assoc($result)) {
$attendants[] = $tableRow;
}
} else {
echo mysqli_error($databaseLink) . 'QUERY: ' . $$selectorQuery;
}
if (isset($_POST['value'])){
$nameInput = $_POST['nameInput'];
$emailInput = $_POST['emailInput'];
$aanmelding = $_POST['aanmelding'];
$ipadres = $_SERVER['REMOTE_ADDR'];
$opmerkingen = $_POST['opmerkingen'];
$aantalPersonen = $_POST['aantalpersonen'];
}
$sql = "INSERT INTO attendants (naam, email, komt, ipadres, opmerkingen, aantalpersonen)
VALUES('Justin', '$emailInput', '$aanmelding', '$ipadres','$opmerkingen', '$aantalPersonen')";
if (!mysqli_query($databaseLink,$sql))
{
die('Error: ' . mysqli_error($databaseLink));
}
echo "1 record added";
mysqli_close($databaseLink);
?>
<!doctype html>
<html>
<head>
<title></title>
<meta name="description" content=""/>
<meta charset="utf-8"/>
<link rel="stylesheet" href=""/>
</head>
<body>
<?php
if (!empty($attendants)) {
foreach ($attendants as $people) {
echo '<ol>';
echo 'Attendent';
echo "<li>Naam: {$people['naam']}</li>";
echo "<li>E-mail: {$people['email']}</li>";
echo "<li>Komt: {$people['komt']}</li>";
echo "<li>Datum van aanmelding: {$people['datum']}</li>";
echo "<li>Ipadres: {$people['ipadres']}</li>";
echo "<li>Eventuele opmerkingen: {$people['opmerkingen']}</li>";
echo "<li>Aantal personen: {$people['aantalpersonen']}</li>";
echo '</ol>';
}
} else {
echo "af er is iets fout gegaan, of er heeft nog niemand zich ingeschreven";
}
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<fieldset>
<label for="nameInput" class="labelstyle">Naam: </label>
<input id="nameInput" name="inputFields" type="text" autofocus="auto"><br>
<label for="emailInput" class="labelstyle">E-Mail</label>
<input id="emailInput" name="inputFields" type="text"><br>
<label for="aanmelding" class="labelstyle">Komt u, voor ja 1, voor nee 0</label>
<input id="aanmelding" name="inputFields" type="text"><br>
<label for="opmerkingen" class="labelstyle">opmerkingen</label>
<input id="opmerkingen" name="inputFields" type="text"><br>
<label for="aantalpersonen" class="labelstyle">aantal personen</label>
<input id="aantalpersonen" name="inputFields" type="number"><br>
<input type="submit" name="submit" value="submit">
</fieldset>
</form>
</body>
</html>
I hope some of you would be so nice to help me out
我希望你们中的一些人会很乐意帮助我
回答by Deepu
The name of the input element will be posted as the key of $_POSTarray.So change it like this or change your $_POSTkeys.
输入元素的名称将作为$_POST数组的$_POST键发布。所以像这样更改它或更改您的键。
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<fieldset>
<label for="nameInput" class="labelstyle">Naam: </label>
<input id="nameInput" name="nameInput" type="text" autofocus="auto"><br>
<label for="emailInput" class="labelstyle">E-Mail</label>
<input id="emailInput" name="emailInput" type="text"><br>
<label for="aanmelding" class="labelstyle">Komt u, voor ja 1, voor nee 0</label>
<input id="aanmelding" name="aanmelding" type="text"><br>
<label for="opmerkingen" class="labelstyle">opmerkingen</label>
<input id="opmerkingen" name="opmerkingen" type="text"><br>
<label for="aantalpersonen" class="labelstyle">aantal personen</label>
<input id="aantalpersonen" name="aantalpersonen" type="number"><br>
<input type="submit" name="submit" value="submit">
</fieldset>
</form>
回答by Let me see
You cant access a POST element using the idof the element.For accessing an element as $_POST['nameInput']the NAMEof the input field should be nameInputeg:-
您不能使用元素的id访问 POST 元素。为了访问元素作为输入字段$_POST['nameInput']的名称应该是nameInput例如:-
<input id="nameInput" name="inputFields" type="text" autofocus="auto"><br>
should be changed to
应该改为
<input id="nameInput" name="nameInput" type="text" autofocus="auto"><br>
AND
If you want the form to be submitted to the same page. you should leave the action attributeof the form blankinstead like action=''
AND
The name of your submit button is 'submit'. so you should check like this if the form has been submitted or not isset($_POST['submit'])
并且
如果您希望将表单提交到同一页面。您应该将表单的action 属性留空,而不是像action=''
AND
您提交按钮的名称是'submit'。所以你应该像这样检查表单是否已经提交isset($_POST['submit'])
回答by user3079312
Use isset($_POST['value']) to handle the situation
使用 isset($_POST['value']) 处理情况
回答by g a naidu
use like this: "$_POST[variableName]";Dont't give single quotes to the variable inside post.
像这样使用:“$_ POST[variableName]”;不要给 post 中的变量加上单引号。
回答by ghgh
You cant access a POSTelement using the idof the element.For accessing an element as $_POST['nameInput']the nameof the input field should be nameInput
你不能访问POST使用所述元件id的element.For的访问的元素作为$_POST['nameInput']所述name输入字段的应nameInput
In example:
例如:
<input id="nameInput" name="inputFields" type="text" autofocus="auto"><br>
should be changed to
应该改为
<input id="nameInput" name="nameInput" type="text" autofocus="auto"><br>
If you want the form to be submitted to the same page. you should leave the actionattribute of the form blank instead. (like action='')
如果您希望将表单提交到同一页面。您应该将action表单的属性留空。(喜欢action='')
The nameof your submit button is 'submit'. so you should check like this if the form has been submitted or not isset($_POST['submit'])
在name您的提交按钮'submit'。所以你应该像这样检查表单是否已经提交isset($_POST['submit'])

![php SQLSTATE[HY093]:无效的参数号:绑定变量的数量与第 102 行的标记数量不匹配](/res/img/loading.gif)