2017-03-28 1 views
-1

Ich mache einen Blog mit mysql, um Daten (Blog-Posts) mit PHP einzugeben und auszugeben. Ich habe eine Tabelle erstellt, Zeilen für die Daten eingefügt wie: ID, Titel, Inhalt und Datum in "addentry.php". Ich habe Probleme beim Ausgeben der Daten mit php auf meiner "viewblog.php". Wo es heißt "// Ausgabe von Tabelle" (Zeile 115), das ist, wo ich die Daten ausgeben möchte. Ich habe mein Bestes versucht, um eine Lösung zu finden.Wie man Daten mit PHP aus der MySQL-Datenbank ausgibt

Vielen Dank im Voraus.


<?xml version = "1.0" encoding = "utf-8"?> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
 
<!-- image.html 
 
    A trivial document 
 
    --> 
 
<html xmlns = "http://www.w3.org/1999/xhtml"> 
 

 

 
    <head><title> My Blog </title></head> 
 

 
    <style type ="text/css"> 
 

 
     body{ 
 

 
     position: fixed; 
 
     overflow:overlay; 
 
     width: 100%; 
 
     top: -20px; 
 
     left: -20px; 
 
     right: -40px; 
 
     bottom: -40px;  
 

 

 
     height: auto; 
 
     background-image:url(image.jpg); 
 
     background-size: cover; 
 

 

 

 
     } 
 
     .container{ 
 

 
      background-color: #ecdad6;   
 
      padding: 30px; 
 
      width:920px;   
 
      margin-left: 25%; 
 
      padding-bottom:1000px; 
 
      padding-left:0px; 
 
      border: 2px solid black; 
 
      } 
 

 
     .links{ 
 
      position: absolute; 
 

 
      padding-right: 135px; 
 
      padding-bottom: 800px;    
 
      margin-left: 680px; 
 
      margin-right: 100px; 
 
      font-size: 20px; 
 
      word-wrap: break-word; 
 
      top:-3px; 
 

 

 
     } 
 

 

 
     .blog{ 
 
      position: absolute; 
 
      width:678px; 
 
      padding-bottom: 920px;    
 
      margin-left: 10px; 
 
      font-size: 20px; 
 
      text-align: left;    
 
      word-wrap: break-word; 
 

 
      } 
 
     ul li { margin-top: -10px; } 
 

 
     } 
 
     
 
     iframe{ 
 
     border:2px solid black; 
 
     width:4000px; 
 
     display:block; 
 
     *} 
 

 
    } 
 
    </style> 
 
    <body> 
 

 
     <!--Logo & hyperlinked --> 
 
     <p align = "center"><a href="viewblog.php"><img src = "Logo.jpg"  alt="My logo" width="10%" height="10%"/></a></p> 
 
     <br/> 
 
     <hr width="50%"> 
 

 
     <div class="container">       
 
      <div class="blog"> 
 
       <?php 
 
         
 
\t \t \t \t $host = "dbprojects.eecs.qmul.ac.uk" ; 
 
\t \t \t \t $user = "hm315" ; 
 
\t \t \t \t $pass = "cXtXuyf2pnF4H" ; 
 
\t \t \t \t $db = "hm315" ; 
 
\t \t \t \t 
 
\t \t \t \t $link = mysql_connect ($host , $user , $pass); 
 
\t \t \t \t if (! $link) { 
 
\t \t \t \t  die('Could not connect: ' . mysql_error()); 
 
\t \t \t \t } 
 
\t \t \t \t echo 'Connected successfully' ; 
 

 
\t \t \t \t $db_selected = mysql_select_db ($db , $link); 
 
\t \t \t \t if (! $db_selected) { 
 
\t \t \t \t  die ('Can\'t use $db : ' . mysql_error()); 
 
\t \t \t \t } 
 

 
\t \t \t \t echo 'Connected successfully' ; 
 

 
\t \t \t \t //select from table 
 
\t \t \t   $sql = "SELECT * FROM post02"; 
 
\t \t \t \t $row=mysql_fetch_array($sql); 
 
\t \t \t \t 
 
\t \t \t \t echo 'selecting table works'; 
 
\t \t \t \t 
 
\t \t \t \t //output from table 
 
\t \t \t \t 
 
\t \t \t \t 
 

 
          \t 
 
\t \t \t \t //connection + database, record created and close connection. 
 

 
\t \t    if ($link->query($sql) === TRUE) { 
 
\t \t \t \t  echo "New record created successfully"; 
 
\t \t \t  } else { 
 
\t \t \t \t  echo "Error: " . $sql . "<br>" . $link->error; 
 
\t \t \t  } 
 
\t \t \t \t 
 
         \t \t mysql_close ($link); 
 
       ?> 
 
       
 
       
 
       <form action='index.php' method='get'></form> 
 
       <div class="links"> 
 
        <a href="viewblog.php"> <ul><li>home</li></ul></a> 
 
        <a href="login.html"> <ul><li>logIn</li></ul></a> 
 
        <a href="entry.html"> <ul><li>add_entry</li></ul></a>     
 
       </div>     
 
      </div> 
 
       </div> 
 

 
    </body> 
 
</html>
<?php 
 

 
    $host = "xxx.ac.uk" ; 
 
\t $user = "xxx" ; 
 
\t $pass = "xxx" ; 
 
\t $db = "xxx" ; 
 
\t 
 
\t $link = mysql_connect ($host , $user , $pass, $db); 
 
\t if (! $link) { 
 
\t  die('Could not connect: ' . mysql_error()); 
 
\t } 
 
\t echo 'Connected successfully' ; 
 
\t 
 

 
\t $db_selected = mysql_select_db ($db , $link); 
 
\t if (! $db_selected) { 
 
\t  die ('Can\'t use $db : ' . mysql_error()); 
 
\t } 
 
/* 
 
\t // create a table 
 

 

 
\t $sql="create table post02(id INT(6) unsigned auto_increment primary key , title varchar(30) not null, content varchar(255) not null, date TIMESTAMP)"; 
 

 

 

 
\t $connection = mysql_query($sql); 
 

 
\t if (! $connection) { 
 

 
\t  die ('Cant create table : ' . mysql_error()); 
 

 
\t } 
 

 
\t echo 'Created a table successfuly' ; 
 
*/ \t 
 

 
//insert to table 
 
\t echo'insert table - initializing'; 
 

 
\t if($_POST['submit']){ 
 
\t $title = $_POST['title']; 
 
\t $content = $_POST['content']; 
 
\t $date = date('l jS \of F Y h:i:s A'); 
 
\t } 
 
\t 
 
\t $sql = "INSERT INTO post2('title', 'content', 'date') VALUES ($title, $content,$date))"; 
 

 
\t if($title =="" || $content==""){ 
 
\t  echo "please compelete your post!"; 
 
\t  return; 
 
\t  
 
\t } 
 
\t echo'insert table completed'; 
 

 
    
 
    \t mysql_query($db ,$sql); 
 
    \t header("location: viewblog.php"); 
 

 
\t //record created is successful 
 
\t if ($link->query($sql) === TRUE) { 
 
\t  echo "New record created successfully"; 
 
\t } else { 
 
\t  echo "Error: " . $sql . "<br>" . $link->error; 
 
\t } 
 
\t 
 
    
 
mysql_close ($link); 
 
?>

+2

Ich hoffe, das sind nicht die echten Verbindung Krediten, die Sie gebucht haben. Wenn ja, ändern Sie bitte Benutzer/Pass so schnell wie möglich. Es gibt viele Bots, die SO nach Anmeldeinformationen durchsuchen und gerne Bitcoins extrahieren und die Welt innerhalb von Minuten mit Ihren Anmeldeinformationen spammen. – Dimi

+0

Ihre Frage ist nicht klar –

+0

@lazy_coder Ich habe 2 PHP-Dateien. Eine namens "addentry.php", die eine Tabelle innerhalb der Datenbank erstellt, fügt auch Daten in die entsprechenden Felder ein. Ich habe auch eine andere PHP-Datei namens "viewblog.php", diese PHP-Datei holt die Blog-Beiträge, die in der Datenbanktabelle "post02" gespeichert ist. was ich brauche Hilfe, ist zu versuchen, die gespeicherten Blog-Beiträge aus der "post02" -Tabelle in meine "viewblog.php" zu echo. – Timmay696969

Antwort

1

Die Frage war nicht so klar, ich schrieb einen Beispielcode nach dem Titel der Frage. Oh, und vergiss nicht deine Credits zu ändern.

<table> 
<tr> 
    <th>Column 1</th> 
    <th>Column 2</th> 
    <th>Column 3</th> 
</tr> 

<?php 
$r = $link->query("SELECT * FROM table"); 
while($rf = $r->fetch_assoc()) 
{ 
    echo "<tr> 
      <td>{$rf['column_1']}</td> 
      <td>{$rf['column_2']}</td> 
      <td>{$rf['column_2']}</td> 
     </tr>"; 
} 
?> 

</table> 
+0

Ich möchte in der Lage sein, Daten von MySQL mit php mit echo zu holen. – Timmay696969

+0

Genau das habe ich hier geschrieben. Dieser Code erstellt eine HTML-Tabelle mit 3 Spalten und erstellt die Zeilen aus der Datenbank. Natürlich müssen Sie einen Teil des Codes ändern (wie Namen, SQL-Tabelle und Spaltennamen). – Hienz