dsljpwi494719 2013-10-04 08:07
浏览 24
已采纳

如何将日期和user_id插入mysql

I'm quite new in PHP and MySQL. I tried to make message board where user can post some message on wall and the every logged user can read it.

Now when someone add message doesn't write in table author_id and date_added. I need them when results are displayed.

Here is new.php

if(isset($_POST['formSubmit']))
{
    $errorMessage = "";

    if(empty($_POST['formTitle'])) 
    {
        $errorMessage .= "<li>Doesn't have title!</li>";
    }
    if(empty($_POST['formContent'])) 
    {
        $errorMessage .= "<li>The field for content is empty!</li>";
    }

    if(empty($errorMessage)) 
    {
        $db = mysql_connect("localhost","root","");
        if(!$db) die("Error connecting to MySQL database.");
        mysql_select_db("homework3" ,$db);

        $sql = "INSERT INTO massages (author_id, date_added, title, content) VALUES ('$_POST[author_id]', '$_POST[date_added]', '$_POST[formTitle]', '$_POST[formContent]')";
        mysql_query($sql);

        header("Location: index.php");
        exit();
    }
}

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<div><label for='formTitle'>Title<input type="text" name="formTitle" value=""   style="width: 350px;"></label></div></br>

<div><label for='formContent'>Content</div><textarea name="formContent" style="width: 344px; height: 100px;"></textarea>

<input type="submit" class="formbutton" name="formSubmit" value="Send"/>

</form>

Edit: I don't know if you need this but this is how I display massages:

$sql = 'SELECT username, msg_id, title, content, date_added FROM massages as m, users  as u WHERE author_id = user_id ORDER BY m.date_added DESC';

  $result = mysqli_query($link, $sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
    $real_date = date('d.m.Y', $row['date_added']);
    echo '<table>
   <tr>
   <td>' . $row['msg_id'] . '. ' . $row['title'] . '</td>
   </tr>
   <tr>
     <td>' . $row['content'] . '</td>
   </tr>
   <tr>
<td>By<span style="color: #CC0033;">' . $row['username'] . '</span> on   <span style="color: #CC0033;">' . $real_date . '</span></td></br>
</tr>
</table>';
}

}

  • 写回答

4条回答 默认 最新

  • dongsu4345 2013-10-04 08:11
    关注

    You can use hidden attributes i.e. type = 'hidden' for auther_id

    For example in your form

    <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
    <div><label for='formTitle'>Title<input type="text" name="formTitle" value=""   style="width: 350px;"></label></div></br>
    
    <div><label for='formContent'>Content</div><textarea name="formContent" style="width: 344px; height: 100px;"></textarea>
    
    <input type="hidden" name="author_id" value="<?php echo $_SESSION['what_ever']; ?>"/>
    <input type="submit" class="formbutton" name="formSubmit" value="Send"/>
    
    </form>
    

    NOTE: <?php echo $_SESSION['what_ever']; ?> is just an assumption of how your author_id could be

    and for date_added you can create add this directly in the query no need to post it via form

    $sql = "INSERT INTO massages (author_id, date_added, title, content) VALUES ('$_POST[author_id]', NOW(), '$_POST[formTitle]', '$_POST[formContent]')";
    

    You should also avoid sending author_id via post and add it rather in this manner

    $auther_id = $_SESSION['username'];
    $sql = "INSERT INTO massages (author_id, date_added, title, content) VALUES ('$auther_id', NOW(), '$_POST[formTitle]', '$_POST[formContent]')";
    

    IMPORTANT

    PHP is deprecating the mysql functions you must need to use mysqli Why shouldn't I use mysql_* functions in PHP?

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

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据