-3

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

Angel.King.47
  • 7,922
  • 14
  • 60
  • 85
Justin Veenis
  • 37
  • 1
  • 1
  • 6
  • 1
    You shouldn't even think of building SQL like that to begin with; you're going to get SQL injections. – Wooble Dec 17 '13 at 12:19

5 Answers5

0

The name of the input element will be posted as the key of $_POST array.So change it like this or change your $_POST keys.

<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>
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
  • Please note that using php_self will open them up to xss attacks. Please read here http://stackoverflow.com/questions/6080022/php-self-and-xss p.s i didn't downvote for this – Liam Sorsby Dec 17 '13 at 12:21
0

You cant access a POST element using the id of the element.For accessing an element as $_POST['nameInput'] the NAME of the input field should be nameInput eg:-

<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 attribute of the form blank instead 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'])

Let me see
  • 5,063
  • 9
  • 34
  • 47
0

Use isset($_POST['value']) to handle the situation

0

use like this: "$_POST[variableName]"; Dont't give single quotes to the variable inside post.

ganaidu
  • 96
  • 1
  • 3
-1

You cant access a POST element using the id of the element.For accessing an element as $_POST['nameInput'] the name of the input field should be 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 action attribute of the form blank instead. (like action='')

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'])

goto
  • 7,908
  • 10
  • 48
  • 58
ghgh
  • 1