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:
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