Chronos313 Posted December 16, 2020 Share Posted December 16, 2020 Hello together, I'm still relatively new with PHP and I'm encountering an error with the error code (Warning: Undefined array key "title" in I:\xampp\htdocs\test\Datenspeichern.php on line 13 to 17.). Normally I wanted that when I enter the data in the fields from the CD_Website page, that it is inserted in my created database and displayed in the table on the page. Does anyone know where my error is? Thanks a lot main Code= <!doctype html> <html> <head> <meta charset="utf-8"> <title>Unbenanntes Dokument</title> <style type="text/css"> table { border-collaps: collapse; width: 100%; font-family: serif; font-size: 35px; text-align: center; } td { font-size: 25px; text-align: center; font-family: serif; } </style> </head> <body> <table> <tr> <th>CD-Titel</th> <th>Artist</th> <th>Songtitel</th> <th>Musiklänge</th> <th>Lied Nummer</th> </tr> <?php $host = "localhost"; $user = "root"; $password = ""; $db_name = "cdaufgabe"; $con = mysqli_connect($host, $user, $password, $db_name); if(mysqli_connect_error()) { die("Verbindungsabbruch mit der Datenbank: ". mysqli_connect_error()); }; $check = "SELECT * FROM `cd-titel`"; $result = mysqli_query($con, $check); if ($result > null) { while ($row = $result->fetch_assoc()){ echo "<tr><td>" . $row['CD-Titel'] . "</td> <td>" . $row['Artist'] . "</td> <td>" . $row['Songtitel'] . "</td> <td>" . $row['Musiklänge'] . "</td> <td>" . $row['Lied-Nummer'] . "</td></tr>"; }; }; /*if (isset($_POST['submitted'])) { $titel = $_POST['titel']; $artist = $_POST['artist']; $songtitel = $_POST['songtitel']; $musicle = $_POST['musicle']; $liednr = $_POST['liednr']; $data_add = "INSERT INTO cd-titel (CD-Titel, Artist, Songtitel, Musiklänge, Lied-Nummer) VALUES ('$titel', '$artist', '$songtitel', '$musicle', '$liednr')"; if (!mysqli_query($db_name, $data_add)) { die('Fehler beim einfügen von Daten'); } } */ // $data_add = "INSERT INTO `cd-titel` (`CD-Titel`, `Artist`, `Songtitel`, `Musiklänge`, `Lied-Nummer`) VALUES ('$titel', '$artist', '$songtitel', '$musicle', '$liednr')"; // mysqli_query($con, $data_add); /*mysqli_query($con, $data_add);*/ ?> <form methode="post" action="Datenspeichern.php"> <input type="text" name="titel" placeholder="CD-Titel"/> <input type="text" name="artist" placeholder="Artist"/> <input type="text" name="songtitel" placeholder="Songtitel"/> <input type="text" name="musicle" placeholder="Musiklänge"/> <input type="text" name="liednr" placeholder="Lied-Nummer"/> <input type="submit" name="submitted" value="speichern"/> </form> </br></br></br> </table> </body> </html> second code(Datenspeichern.php)= <?php $con = mysqli_connect('localhost', 'root', ''); if (!$con) { echo'Nicht verfügbar'; } if (!mysqli_select_db($con, 'cdaufgabe')){ echo 'Datenbank nicht ausgewählt'; }; $titel = $_POST['titel']; $artist = $_POST['artist']; $songtitel = $_POST['songtitel']; $musicle = $_POST['musicle']; $liednr = $_POST['liednr']; $data_add = "INSERT INTO cd-titel (CD-Titel, Artist, Songtitel, Musiklänge, Lied-Nummer) VALUES ('$titel', '$artist', '$songtitel', '$musicle', '$liednr')"; if (!mysqli_query($con, $data_add)){ echo 'Fehler'; } else { echo'Eingefügt'; }; header("url=CD_Webseite.php"); ?> Does anyone knows the mistake i keep doing? Quote Link to comment https://forums.phpfreaks.com/topic/311874-i-keep-getting-error-while-the-php-code-tries-to-read-the-post-array-i-think/ Share on other sites More sharing options...
gw1500se Posted December 16, 2020 Share Posted December 16, 2020 Because you did not use the code icon (<>) for your code it is very difficult to read. Which are lines 13-17? Although I can't find it, somewhere there you probably have 'titel' misspelled as 'title'. Quote Link to comment https://forums.phpfreaks.com/topic/311874-i-keep-getting-error-while-the-php-code-tries-to-read-the-post-array-i-think/#findComment-1583132 Share on other sites More sharing options...
gizmola Posted December 16, 2020 Share Posted December 16, 2020 It looks to me like you have some sort of sync issue with your environment, because there is no reference to a key of 'title' in the script you posted. There could be all sorts of reasons depending on how you are testing or revising your code. This could also be because you have a cached version of your code. Did you try to restart your apache/php? If that doesn't fix the issue, I'm not sure what to tell you, as the code you provided does not include 'title', but does include 'titel' in a number of places. If you changed the spelling of 'title' to 'titel' recently that would also suggest a caching issue. If you are interested in where this type of caching might exist in a development environment, reference OPCache. OPCache is a performance enhancement that caches source code in memory to vastly improve runtime production. It might be installed in your xampp. In a development scenario, it is probably advisable to disable it just to remove the possibility it might interfere with your development cycle. Again this is just an educated guess based on what you provided. Quote Link to comment https://forums.phpfreaks.com/topic/311874-i-keep-getting-error-while-the-php-code-tries-to-read-the-post-array-i-think/#findComment-1583147 Share on other sites More sharing options...
mac_gyver Posted December 16, 2020 Share Posted December 16, 2020 4 hours ago, Chronos313 said: methode="post" method is misspelt in the form tag, so this is actually a get method form and none of the $_POST data exists. 1 Quote Link to comment https://forums.phpfreaks.com/topic/311874-i-keep-getting-error-while-the-php-code-tries-to-read-the-post-array-i-think/#findComment-1583148 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.