PHP CRUD和MySQL在两个文件上

时间:2019-05-19 13:31:26

标签: php sql pdo

我对php和数据库非常了解...

我有一个文件,其中显示注释, contact.php here来说明它和我的CSS,因为 codepen 仅适用于JS,不显示注释表单,注释代码在PHP中称为 classprove.php ,它是here,人们可以帮助我,以构建这些代码...

比起文件 kommenter_verwalter.php ,我将在其中在Tabelle上显示我的评论输出。

因此,我有两个文件用于注释, classprove.php 用于管理我的注释,以及 kommenter_verwalter.php ,我将在其中显示它们。

我尝试使用这些代码来构建要删除的文件,每行旁边都有一个按钮,因此,如果要删除该行,请单击删除按钮。

编辑我的新代码

现在要搜索我的代码而无需,请连接到我的 classprove.php 文件。

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();
?>

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

现在不会出现错误,但是什么也不会显示...

我注意到我还没有,

  

$ _ POST [“删除”]

  

$ _ POST [“ id”];

我仅使用(姓名,电子邮件和消息)来构建我的表单,而要使用phpmyadmin来构建 id 。 。这些代码已在互联网上找到...

请任何人告诉我,如何更改代码的这些部分。

如何在phpmyadmin上show我的Tabelle

我希望文件 kommenter_verwalter.php (在此提供),输出显示 only 注释(名称,电子邮件,消息等)。在带有按钮删除作为此Screeshot到show ...的表上,并保留文件 classprove.php 作为评论管理人。

请任何人帮助我解决这个问题,谢谢!

2 个答案:

答案 0 :(得分:0)

以上错误表明'"中存在错误,您没有正确放置它们,这就是complier无法parse的那部分代码的原因。像下面这样更改该行应该起作用:

 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>"; 

答案 1 :(得分:0)

因为 stackoverflow 上的人不让我的another问题回答,所以将其隐藏起来.....标题中的标题“如何在SQL上使用CRUD” ,因为它与这些非常相似,所以我在这里回答...。

现在可以显示文件 kommenter_verwalter.php ,而无需连接到我的 classprove.php 文件。

我有一些错误,因此无法显示。...

我的 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现在如何显示我的文件 kommenter_verwalter.php how to show it

相关问题