Jump to content

I keep getting error while the php code tries to read the POST array i think


Recommended Posts

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

 

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?

  • Chronos313 changed the title to I keep getting error while the php code tries to read the POST array i think

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'.

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.  

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.