dtvpl739577 2015-12-12 21:37
浏览 25
已采纳

调整我的php留言簿

I want it to show the newest post first then second and so on, how do i manage such thing? and it doesnt show the email address for some reason... :( and last but not least: how do i add like timestamp like yyyy/mm/ddd hh:mm ?

<form method="post" onsubmit="#"  action="gpost.php">
<fieldset>
 Namn:<br><input name="gnamn" id="gnamn" type="text" size="20">
 <br>E-post:<br><input name="gemail" id="gemail" type="text" size="20">
<br>Meddelande:<br>
<textarea name="gmeddelande" id="gmeddelande" cols="20" rows="10">  
</textarea>
<br><br>
<input name="submit" type="submit" value="Sänd">
</fieldset>
</form>

<?php
$host="localhost"; 
$user="my user";
$pass="mypasswor :D";
$dbname="pelle";
$con=mysqli_connect($host,$user,$pass,$dbname);
if (mysqli_connect_errno($con))
{
echo "Misslyckades att ansluta till mysql: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT name,message FROM guestbook");
while($row = mysqli_fetch_array($result))
{ ?>
<div id="post">
<header><h3><?php echo $row['name']; ?></h3></header>
<p><?php echo $row['email']; ?></p>
<p><?php echo $row['message']; ?></p>
 </div>

   <?php }
   mysqli_close($con);
  ?>

and heres my gpost.php

 <?php
 $host="localhost"; 
$user="my usernamen"; 
$pass="my password"; 
 dbname="pelle"; 
 $con=mysqli_connect($host,$user,$pass,$dbname);
 if (mysqli_connect_errno($con))
 {
 echo "<h1>Failed to connect to MySQL: " . mysqli_connect_error() ."</h1>";
 }
 $name=$_POST['gnamn'];
 $email=$_POST['gemail'];
 $message=$_POST['gmeddelande'];
 $sql="INSERT INTO guestbook(name,email,message)  VALUES('$name','$email','$message')";
 if (!mysqli_query($con,$sql))
 {
 die('Error: ' . mysqli_error($con));
 }
 else
 echo "Values Stored in our Database!";
 mysqli_close($con);
 ?>
  • 写回答

3条回答 默认 最新

  • dongpo1203 2015-12-12 21:47
    关注

    Add a field called "time" from Type INT to Database, with the value of "time()" php function.

    And change your SQL from

     mysqli_query($con,"SELECT name,message FROM guestbook");
    

    to

    mysqli_query($con,"SELECT name,message,time FROM guestbook ORDER BY time DESC");
    

    time(): Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). time on php.net

    date(): string date ( string $format [, int $timestamp = time() ] ) Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time(). date on php.net

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用