ursus contionabundo: Es lag definitiv am PHP...

Beitrag lesen

Ich habe mal alle Typos korrigiert, den Code ein wenig aufgeräumt und die allergröbsten Strukturfehler beseitigt.

Ich markiere den Code als "schlecht", weil er zwar funktioniert aber als Gästebuch nicht wirklich brauchbar ist.

<?php

class ClassProveContakt3 {

	private $dbHost = 'localhost'; # Host der Datenbank
	private $dbName = 'name';      # Name der Datenbank
	private $dbUser = 'user';      # Name der Datenbank
	private $dbPass = 'pass';      # Passwort

	private $Name;
	private $Email; 
  private $Message;
	private $PostOK;
	private $DateTime;
	private $items;
	private $ip;
	private $dbh;

	function __construct() {

		 $this -> ip = $_SERVER['REMOTE_ADDR'];
		 $this -> DateTime = date('m/d/Y h:i:s a');  
		 $this -> items = ['Name', 'Email', 'Message']; 
		 $flag = true;

         foreach ( $this -> items as $key ) {  
			if ( empty ( $_POST[$key] ) )  {
				$flag = false;    
			} else {    
				#trigger_error('Codepoint __construct:1  ' . $key . ': ' . $_POST[$key]);
				$this -> $key = trim( filter_var( $_POST[$key], FILTER_SANITIZE_STRING ) ); 
				$this -> $key = trim( $_POST[$key] ); 
			}    
		}
		#trigger_error('Codepoint __construct:2  TRUE');   
        $this -> PostOk = $flag;
        #trigger_error('Codepoint __construct:3 ' . $this -> PostOk );  
	}

    private function getConnection() {
          // Establish connection with MYSQL Server
          try {
             $this -> dbh =  new PDO( 'mysql:host=' . $this -> dbHost  . ';dbname=' . $this -> dbName, $this -> dbUser, $this -> dbPass );
          } catch ( PDOException $pe ) {
             trigger_error ("Cannot connect to database: " . $pe -> getMessage() , E_USER_ERROR );
          }
      }

	private function reportPDOError( $message, $sql ) {
         $info = $this -> dbh -> errorInfo();

         echo "<div style='color:red'><b>Error in SQL Access: $message</b>";
         echo "<br>SQL-Statement: $sql";
         echo "<br>PDO SQLSTATE: $info[0]";
         echo "<br>MySQL error code: $info[1]";
         echo "<br>MySQL error message: $info[2]</div>";
	}
    
    private function unsetFormdata() {
		foreach ( $this -> items as $key ) { 
			unset( $_POST[$key] );
		}
	}
      

    function ShowForm()  {
?>
<!-- <form  method="POST" action="https://home.fastix.org/phpinfo.php">-->
<form  method="POST"">
	<label for="name"><b>Name * </b></label>
	<input type="text" id="name" name="Name" value="<?=@htmlentities( $_POST['Name'] );?>">
	<label for="email"><b>E-mail * </b></label>
	<input type="email" id="email" name="Email" value="<?=@htmlentities( $_POST['Email'] );?>">
	<br><br>
	<label><b> Message * </b><br>
		<textarea cols="45" rows="6" id="text" name="Message"><?=@htmlspecialchars( $_POST['Message'] );?></textarea>      
	</label>
	<br><br>
	<input  type="submit" name="post" value="POST COMMENT" id="comment">
</form>
<?php
    }
    
    
    function TestPostData() {
		
		if ( $this -> PostOk ) {
			$this -> writeCommentToDatabase();
		} else {
			echo '<div class="msg">*** Please enter all required fields ***</div>';  
		}
    } 

	function writeCommentToDatabase() {
	// Establish connection with MYSQL Server
	
		if ( ! $this -> dbh ) {
			$this -> getConnection();
		}
		//Prepare Query of SQL
		$statement = $this -> dbh -> prepare("INSERT INTO mela(name, email, message, datetime, ip) VALUES (:name, :email, :message, :date, :ip)");
		if ( ! $statement ) {
			trigger_error( 'prepare failed: SQLSTATE=' . $this -> dbh -> errorCode() . ', Error Info=' . print_r( $this -> dbh -> errorInfo(), true ), E_USER_ERROR ) ;
		} else {
			$ok = $statement -> bindValue( ':name',    $this -> Name,         PDO::PARAM_STR )
			   && $statement -> bindValue( ':email',   $this -> Email,        PDO::PARAM_STR )
			   && $statement -> bindValue( ':message', $this -> Message,      PDO::PARAM_STR )
			   && $statement -> bindValue( ':date',    date("Y-m-d H:i:s"), PDO::PARAM_STR )
			   && $statement -> bindValue( ':ip',      $this -> ip,          PDO::PARAM_STR );
			if ( ! $ok )  {
				echo "<br><br>bindValue failed: SQLSTATE=" . $this -> dbh -> errorCode() . ", Error Info=" . print_r( $dbh -> errorInfo(), true ) . "</p>";
			}
		}
		if ( $ok ) {
			$ok = $statement -> execute();
		} 
		
		if ( ! $ok ) {
            trigger_error( "execute failed: SQLSTATE=" . $this -> dbh -> errorCode() . ", Error Info=" . print_r($this -> dbh -> errorInfo(), true),  E_USER_ERROR );
		} else {
			echo '<div class="msg">Data Inserted successfully!</div>';
			$this ->  unsetFormdata();
			return $ok;
		}
    }    

    function getMessages()
    {
		if ( ! $this -> dbh ) {
			$this -> getConnection();
		}

        $sql = "SELECT name, email, message, datetime FROM mela ORDER BY datetime DESC";
        $statement = $this -> dbh -> query( $sql );

        if ( ! $statement ) {
            $this -> reportPDOError('SQL-Error:', $sql );
            return false;
        }

        $result = $statement -> fetchAll( PDO::FETCH_ASSOC );

        if ( false === $result ) {
            $this->reportPDOError( "fetchAll(ASSOC) failed", $sql );
        }
        foreach ($result as $message) {
?>
<article>
   <h2>Message from <?=htmlspecialchars( $message['name'] ) ?> &lt;<?= htmlspecialchars( $message['email'] ); ?>&gt;</h2>
   <p>Created <?=htmlspecialchars( $message['datetime'] ); ?></p>
   <p><?=htmlspecialchars( $message['message'] ); ?></p>
</article>
<?php
		}    
    }
}
            
$Newobject = new ClassProveContakt3();
$Newobject -> TestPostData();
$Newobject -> ShowForm();
$Newobject -> getMessages();
0 94

phpmyadmin nicht start

  1. 0
    1. 0
  2. 1
    1. 0
      1. 0
      2. 0
        1. 0
          1. 0
            1. 0
              1. 0
                1. 0
                  1. 0
                    1. 0
                      1. 0
                        1. 0
                          1. 0
                            1. 0
                              1. 1
                                1. 0
                                  1. 0
                                    1. 0
                                      1. 0
                                  2. 0
                                    1. 0
                                      1. 0
                                        1. 0
                                          1. 0
                                            1. 0
                                              1. 0
                                                1. 0
                                                  1. 0
                                                    1. 0
                                                      1. 0
                                                        1. 0
                                                          1. 0
                                                          2. 1

                                                            Es lag definitiv am PHP...

                                                            1. 0

                                                              Was an der Kommentarfunktion konzeptionell schlecht ist

                                                              1. 0
                                                                1. 0
                                                                  1. 0
                                                                    1. 0
                                                                    2. 0

                                                                      Altes Zeug löschen

                                                                      1. 0
                                                                        1. 0
                                                            2. 0
                                                              1. 0
                                                                1. 0

                                                                  Deine Datenbank-Tabelle ist wahrscheinlich (noch) falsch.

                                                                  1. 0
                                                                    1. 0
                                                                      1. 0
                                                                        1. 0
                                                                  2. 0
                                                                    1. 0
                                                                      1. 0
                                                                        1. 0

                                                                          Auf dem Weg zum DELETE

                                                                        2. 0

                                                                          Nein! Du machts jetzt einen Plan.

                                                                          1. 0
                                                                      2. 0
                                                                        1. 0
                                                                          1. 0
                                                                            1. 0
                                                                              1. 0
                                                                                1. 0
                                                                                  1. 0
                                                                                    1. 0
                                                                                    2. 0
                                                                                    3. 0
                                                                                      1. 0
                                                                                        1. 0
                                                                                          1. 0
                                                                                        2. 0
                                                                                      2. 0

                                                                                        Tabelle wird nicht gezeigt

                                                                2. 0
                                                                  1. 0

                                                                    Kleine Verfeinerung der reportPDOError - Methode

                                                                    1. 0
                                                                      1. 0
                                                                        1. 0
                                                                        2. 0
                                                                          1. 0
                                                                            1. 0
                                                                              1. 0
                                                                                1. 0
                                                                                  1. 0
                                                                                    1. 0
                                                                                      1. 0
                                                                                        1. 0
                                                                                          1. 0
                                                      2. 0
                                                2. 0
                                                  1. 0
                                                    1. 0
                        2. 0
                          1. 0