skip to Main Content

I am very New with php and Database…

I have a File where Display the Comment, contact.php , here to schow it and my Css, because codepen have only for JS , the Comment form not to Display , the Code for Comment to be call classprove.php it have in PHP and it is here , people help me, for build these Code…

Than have the File kommenter_verwalter.php where i will to Display my Comments output on Tabelle .

So , i have Two Files for my Comments , classprove.php for Administrate my Comments , and kommenter_verwalter.php , where i will they to Display.

I to try with these Code to build a File for delete with a button beside each row so that if I want to delete that row I click the delete button .

Edit new my Code

Now to search my Code without connect to my classprove.php File .

The Code from kommenter_verwalter.php,

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);



class ClassProveContakt2 
{


          private $dbHost = 'localhost'; # Host der Datenbank
          private $dbName = 'meine';      # Name der Datenbank
          private $dbUser = 'root';      # Name phpmyadmin
          private $dbPass = 'pass';      # Passwort


          private $name;
          private $email;
          private $message;
          private $datetime;
          private $ip;
          private $db;
          private $connection; 
          private $id;
          private $sql; 
          private $statement;
          private $success;     




          function CommentToDatabase()
          {
              // Establish connection with MYSQL Server
            try
            {
              $db = new PDO("mysql:host=localhost;dbname=meine", "root", "pass");
            }
             catch (PDOException $pe)
            {
               echo "<br>Cannot connect to database: " . $pe->getMessage();
                return false;
            }

            if(isset($_POST["delete"])) {
             try {


                   $connection = new PDO($id, $name, $email, $message, $datetime, $ip);

                   $id = $_POST["id"];

                   $sql = "DELETE FROM mela WHERE id = :id";

                   $statement = $connection->prepare($sql);
                   $statement->bindValue(':id', $id);
                   $statement->execute();

                   $success = "User successfully deleted";
                  }catch(PDOException $error) {
                        echo $sql . "<br>" . $error->getMessage();
                   }

             } 
           }
           function tabelle() 
           {
              $db = $this ->CommentToDatabase();

              if ($db) 
              {  

                 echo "<table id='user' class='table table-bordered'>

                    <tr>
                      <th>id</th>
                      <th>name</th>
                      <th>email</th>
                      <th>message</th>
                      <th>datetime</th>
                      <th>ip</th>
                       <th>Delete User</th>
                      </tr>";

                     foreach ($db as $row){

                        echo <<<EOT
                           <tr>
                              <td>{$row['id']}</td>
                              <td>{$row['name']}</td>
                              <td>{$row['email']}</td>
                              <td>{$row['message']}</td>
                              <td>{$row['datetime']}</td>
                              <td>{$row['ip']}</td>
                              <td><button type "submit" name="delete" value="{$row['id']}">löschen</button> 
                            </tr>

EOT;
                     }

                    echo "</table>";   


                 }

           }
}

$News = new ClassProveContakt2;
$News -> CommentToDatabase();
$News -> tabelle();
?>

When write sudo tail -n0 -f /var/log/apache2/error.log /var/log/mysql/error.log

Now to come not error , but nothing Display…

I to observe that i have not,

$_POST[“delete”]

and

$_POST[“id”];

I to build my form with only (name, email and message) , id have with phpmyadmin to build . . These piece Code have in internet to found…

Can Please anyone tell me , how can to change these part the Code .

How to show my Tabelle on phpmyadmin ,

I to want that the the File kommenter_verwalter.php (there which here give) , the output Display only the comments(name, email , message,..) on a Table whith the button , delete as this Screeshot to show…,and the File classprove.php to stay as Administrate for Comment .

Can Please anyone help me with this Problem , Thanks !

2

Answers


  1. Chosen as BEST ANSWER

    Because are People on stackoverflow which not to let my another Question to answer ,they to hiding it.....Titel name from it "How to use CRUD on SQL" , and because it is very similar as these , i answer here ....

    Now can Display the File kommenter_verwalter.php without connect to my classprove.php File .

    I has some mistakes therefore can't Display it....

    How have now my kommenter_verwalter.php,

    class ClassProveContakt2 
    {  
    
          private $conn;
    
    
    
       function __construct() {
    
          $dbHost = 'localhost'; # Host der Datenbank
          $dbName = 'meine';      # Name der Datenbank
          $dbUser = 'root';      # Name der Datenbank
          $dbPass = 'pass';      # Passwort
    
          // declare the object conn, which conect to the Datebase .
    
              $this->conn = new mysqli($dbHost,$dbUser,$dbPass,$dbName);  
        }
         // mit these function delete the Comment .
    
        function deleteComment($id){
    
          /* build a Variable to delete the Comment ,on **id=?** , give **?**  
           because i will the Comment to delete on hand (with the Bottom "delete")
           give not Value to **id**  , the **?** . */
    
          $sql = "DELETE FROM mela WHERE id=?";
    
           // prepare and bind the connection to Database .
    
           $statement= $this->conn->prepare($sql);
           $statement -> bind_param('i',$id);
           $statement->execute();
        }  
        function printAdminTable() {
    
            //connect to the Table , name "mela" and the Rows which are in it .  
    
             $sql = "SELECT id, name, email, message, datetime,ip FROM mela ORDER BY datetime DESC";
    
           // prepare and bind the connection to the Table . 
    
           $statement= $this->conn->prepare($sql);
           $statement->execute();
    
           $result = $statement-> get_result();
    
             //On *if* Statement asked if all is ok on the connection with sql
            // when yes ,build the Table .
    
           if ( $result ) {
                echo "
            <form action="prove_comment.php" method="POST">n
            <table id='user' class='table table-bordered' border= '1px solid';>
                    <tr>
                     <th>ID</th>
                      <th>Name</th>
                      <th>E-mail</th>
                      <th>Message</th>
                      <th>Datetime</th>
                      <th>IP</th>
                       <th>Delete</th>
                      </tr>";
    
                  while($row = $result->fetch_assoc()){
    
                    echo <<<EOT
                      <tr>
                          <td>{$row['id']}</td>
                          <td>{$row['name']}</td>
                          <td>{$row['email']}</td>
                          <td>{$row['message']}</td>    
                          <td>{$row['datetime']}</td>
                          <td>{$row['ip']}</td>
                          <td><button type="submit" name="delete" value="{$row['id']}">löschen</button>
                      </tr>
    
    EOT;
                  }
                 echo '</table></form>';  
                // when not connection on sql .
                } else {
              echo '<p class="error">Hm. $arrRows enthält nichts oder false. '
                .'Entweder ist die Datenbank noch leer oder da ist was falsch.</p>';
         }            
        }
            // connection to sql closse .
         function __destruct(){
           $this->conn->close();    
         }
        }   // Build the call to the method .
        $News = new ClassProveContakt2();
           /* Because befor have add 'delete' on my POST Method                         
            (Linne 77 ,  "<td><button type="....) , with the **if** Statement asked if
            it is , when yes , delete it . */     
        if (isset($_POST['delete'])){
        $News -> deleteComment($_POST['delete']);
        }
        $News -> printAdminTable();
    

    Screehot how to show now my File kommenter_verwalter.php , how to show it


  2. Above error indicate that there is error in ' and " you have not place them properly that’s why complier is not able to parse that part of your code. Change that line like below this should work :

     echo "<form action='classprove.php' method='post'>
                  <tr>
                   <td> '".$row["id"]."'</td>
                    <td>'". $row["name"]."'</td>
                    <td>'". $row["email"]."'</td>
                   <td> '".$row["datetime"]."'</td>
                  <td> '".$row["ip"]."'</td>
                 </tr>
                  </form>"; 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search