我需要从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
我的代码:
<?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>
我希望你们中的一些人能够很好地帮助我
答案 0 :(得分:0)
输入元素的名称将作为$_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>
答案 1 :(得分:0)
您无法使用元素的 id 访问POST元素。要将元素作为$_POST['nameInput']
访问,输入字段的 NAME 应为 nameInput 强>
例如: -
<input id="nameInput" name="inputFields" type="text" autofocus="auto"><br>
应改为
<input id="nameInput" name="nameInput" type="text" autofocus="auto"><br>
和强>
如果您希望将表单提交到同一页面。您应该保留空白形式的操作属性,而不是action=''
和强>
提交按钮的名称为'submit'。所以如果表单已经提交,你应该像这样检查isset($_POST['submit'])
答案 2 :(得分:0)
使用isset($ _ POST ['value'])来处理这种情况
答案 3 :(得分:0)
使用如下:&#34; $ _ POST [variableName]&#34;; 不要在帖子内给变量单引号。
答案 4 :(得分:-1)
您无法使用元素的POST
访问id
元素。要将元素作为$_POST['nameInput']
访问,输入字段的name
应为nameInput
}
例如:
<input id="nameInput" name="inputFields" type="text" autofocus="auto"><br>
应改为
<input id="nameInput" name="nameInput" type="text" autofocus="auto"><br>
如果您希望将表单提交到同一页面。您应该将表单的action
属性留空。 (如action=''
)
提交按钮的name
为'submit'
。所以如果表单已经提交,你应该像这样检查isset($_POST['submit'])