1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Form Input Data</title> </head> <body> <table border="1"> <tr> <td align="center">Gebruikersinformatie</td> </tr> <tr> <td> <table> <form method="post" action="input.php"> <tr> <td>Voornaam</td> <td><input type="text" name="voornaam" size="40"> </td> </tr> <tr> <td>Achternaam</td> <td><input type="text" name="lastname" size="40"> </td> </tr> <tr> <td></td> <td align="right"><input type="submit" name="submit" value="Sent"></td> </tr> </table> </td> </tr> </table> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?php $servername = "localhost"; $username = "root"; $password = "***"; $dbname = "myDB"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "INSERT INTO gebruikers (voornaam, achternaam) VALUES ('$voornaam', '$achternaam')"; if ($conn->query($sql) === TRUE) { echo "Succesvol toegevoegd"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <?php $servername = "localhost"; $username = "root"; $password = "***"; $dbname = "myDB"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $voornaam=$_POST['voornaam']; $lastname=$_POST['lastname']; $sql = "INSERT INTO gebruikers (voornaam, achternaam) VALUES (".$voornaam.", ".$lastname.")"; if ($conn->query($sql) === TRUE) { echo "Succesvol toegevoegd"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); ?> |
1 2 3 4 | $voornaam=$_POST['voornaam']; $lastname=$_POST['lastname']; $sql = "INSERT INTO gebruikers (voornaam, achternaam) VALUES (".$voornaam.", ".$lastname.")"; |
1 | Notice: Undefined index: achternaam in C:\wamp\www\input.php on line 17 |
Schijnbaar staat de kolom 'achternaam' niet (meer) in de database...quote:Op zaterdag 1 augustus 2015 14:02 schreef jodelahity het volgende:
Bedankt beide De voornaam komt nu goed in de database, de achternaam nog niet.
1 Notice: Undefined index: achternaam in C:\wamp\www\input.php on line 17
quote:Op zaterdag 1 augustus 2015 14:14 schreef embedguy het volgende:
[..]
Schijnbaar staat de kolom 'achternaam' niet (meer) in de database...
De database is nog steeds zoals in de openingspost?
Verder zie ik zo 123 niet wat er fout zou kunnen zijn...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?php $servername = "localhost"; $username = "root"; $password = "***"; $dbname = "myDB"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $voornaam=$_POST['voornaam']; $achternaam=$_POST['achternaam']; $sql = "INSERT INTO gebruikers (voornaam, achternaam) VALUES ('$voornaam', '$achternaam')"; if ($conn->query($sql) === TRUE) { echo "Succesvol toegevoegd"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); ?> |
Wat ben ik ook een druif weerquote:Op zaterdag 1 augustus 2015 15:07 schreef ClapClapYourHands het volgende:
In de code van embedguy staat een foutje
ipv achternaam staat er lastname
-edit-
Ah jij hebt het al aangepast zie ik
Heb je de html file ook aangepast?
Ja dat is bij mij standaardquote:Op zaterdag 1 augustus 2015 15:11 schreef ClapClapYourHands het volgende:
Altijd leuk om foutjes te zoeken. Soms uren zoeken naar een ;
quote:Daar wordt direct al info gestuurd naar de database (een lege rij is daar te zien). Vervolgens kan je het formulier invullen en wordt de ingevulde gegevens getoond, en op de juiste manier in de database geplaatst
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <!DOCTYPE HTML> <html> <head> <style> .error {color: #FF0000;} </style> </head> <body> <?php include 'input.php';?> <?php include 'toevoegen.php';?> <h2>Testformulier</h2> <p>Hieronder kunt u uw (of fictieve) gebruikersgegevens invullen. <br ?> De verstuurde gegevens ziet u onderaan en worden in een mysql database opgeslagen.<br /> <br /> <p><span class="error">* Benodigde velden.</span></p> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> Naam: <input type="text" name="naam" value="<?php echo $naam;?>"> <span class="error">* <?php echo $naamErr;?></span> <br /><br /> E-mail: <input type="text" name="email" value="<?php echo $email;?>"> <span class="error">* <?php echo $emailErr;?></span> <br /><br /> Website: <input type="text" name="website" value="<?php echo $website;?>"> <span class="error"><?php echo $websiteErr;?></span> <br /><br /> Comment: <br /> <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea> <br /><br /> <input type="submit" name="submit" value="Submit"> </form> <?php echo "<h2>De door u ingevulde gegevens:</h2>"; echo $naam; echo "<br />"; echo $email; echo "<br />"; echo $website; echo "<br />"; echo $comment; ?> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | <?php // define variables and set to empty values $naamErr = $emailErr = $websiteErr = ""; $naam = $email = $comment = $website = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["naam"])) { $naamErr = "Het invullen van een naam is noodzakelijk"; } else { $naam = test_input($_POST["naam"]); // check if naam only contains letters and whitespace if (!preg_match("/^[a-zA-Z ]*$/",$naam)) { $naamErr = "Alleen letters en spaties zijn toegestaan"; } } if (empty($_POST["email"])) { $emailErr = "Een e-mail adres is noodzakelijk"; } else { $email = test_input($_POST["email"]); // check if email address is well-formed if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $emailErr = "Geen kloppend e-mail formaat"; } } if (empty($_POST["website"])) { $website = ""; } else { $website = test_input($_POST["website"]); // check if URL address syntax is valid (this regular expression also allows dashes in the URL) if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) { $websiteErr = "Ongeldig websiteformaat"; } } if (empty($_POST["comment"])) { $comment = ""; } else { $comment = test_input($_POST["comment"]); } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <?php $servername = "localhost"; $username = "root"; $password = "***"; $dbname = "myDB"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $naam=$_POST['naam']; $email=$_POST['email']; $website=$_POST['website']; $comment=$_POST['comment']; $sql = "INSERT INTO gebruikerscompleet (naam, email, website, comment) VALUES ('$naam', '$email', '$website', '$comment')"; if ($conn->query($sql) === TRUE) { echo "Succesvol toegevoegd"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); ?> |
1 2 3 | if(empty($_POST['foo'])) { echo "a sentence".$_POST['foo']." with something in the middle."; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | $naam=$_POST['naam']; $email=$_POST['email']; $website=$_POST['website']; $comment=$_POST['comment']; $sql = "INSERT INTO gebruikerscompleet (naam, email, website, comment) VALUES ('$naam', '$email', '$website', '$comment')"; if ($conn->query($sql) === TRUE) { echo "Succesvol toegevoegd"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $naam=$_POST['naam']; $email=$_POST['email']; $website=$_POST['website']; $comment=$_POST['comment']; $sql = $conn->prepare("INSERT INTO gebruikerscompleet (naam, email, website, comment) VALUES (?, ?, ?, ?)"); $sql->bind_param('ssss', $naam, $email, $website, $comment); if (!$sql->execute()) { echo "Error: " . $sql->error; } else { echo "Succesvol toegevoegd"; } |
Misschien kun je er zowiets van maken:quote:Op zaterdag 1 augustus 2015 18:00 schreef jodelahity het volgende:
Edit: die vraag is beantwoord
Ik probeer nu het voor elkaar te krijgen dat de informatie pas naar de database wordt gestuurd als de validatie ook klopt. Dus de naam geen nummer heeft, het e-mail adres klopt enzovoort. Die validatie heb ik in orde, maar nu moet ik er nog voor zorgen dat het eerst gevalideerd wordt voordat het verzonden wordt naar de database (wat het nu wel wordt, ook dus als het e-mail adres: niksniks is)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | $Valid=TRUE; if(empty($_POST['foo1'])) { echo "Error sentence 1."; $Valid=FALSE; } if(empty($_POST['foo2'])) { echo "Error sentence 2."; $Valid=FALSE; } if($Valid) { $naam=$_POST['naam']; $email=$_POST['email']; $website=$_POST['website']; $comment=$_POST['comment']; $sql = $conn->prepare("INSERT INTO gebruikerscompleet (naam, email, website, comment) VALUES (?, ?, ?, ?)"); $sql->bind_param('ssss', $naam, $email, $website, $comment); if (!$sql->execute()) { echo "Error: " . $sql->error; } else { echo "Succesvol toegevoegd"; } } |
quote:Op zaterdag 1 augustus 2015 20:35 schreef pussywillow het volgende:
Tip: je maakt nog een vrij klassieke beginnersfout,
...
En voila, veilige code
|
Forum Opties | |
---|---|
Forumhop: | |
Hop naar: |