PHP不会在我的数据库中插入数据记录

时间:2015-07-10 08:13:37

标签: php

我试图插入一条消息功能,该功能将在我的数据库中插入记录。但由于某种原因,它不会插入我的消息并将其重定向到包含home.php文件的login.php

我这里有三个文件:

  
      
  1. home.php
  2.   
  3. process.php
  4.   
  5. dashboard.php
  6.   

现在给出了三个数据库用户,消息和注释,我需要向数据库插入登录用户的记录。出于某种原因,当点击“发布消息”按钮时,它不会将我的消息插入我的数据库。有什么想法吗?

这是我的dashboard.php

<form action="process.php" method="post">
    <input type="hidden" name="action" value="message">
    <textarea rows="4" cols="70" name="message"></textarea>
    <p><input type="submit" value="Post"/></p>
</form>

这是我的process.php

if (isset($_POST['action']) && ($_POST['action']) == 'message') {
    //call to function
    post_message(); //use the ACTUAL POST
}

function post_message() { //just a parameter called post
    $_SESSION['errors'] = array();

    if (empty($_POST['message'])) {
        $_SESSION['errors'][] = "Post Message Can't Be Blank!";
    }

    //now count errors
    if (count($_SESSION['errors']) > 0) {
        header('Location: dashboard.php');
        die();
    } else {
        $query = "INSERT INTO messages(message, created_at, updated_at)
                  VALUES ('{$_POST['message']}', NOW(), NOW())";

        $result = run_mysql_query($query); 
        $_SESSION['message'] = "message successfully posted";
        header('Location: dashboard.php');
        die();
    }
}

知道出了什么问题吗?

2 个答案:

答案 0 :(得分:1)

<form action="process-homepage.php" method="post">

操作设置为“process-homepage.php”,您的代码在process.php

请您将操作更改为process.php并查看其是否有效。如果没有,则将日志级别设置为E_ALL并提供错误详细信息。

答案 1 :(得分:0)

如果您可以将您的原始代码更改为此;

<?php session_start();  
    error_reporting(E_ALL);
    ini_set('display_errors',1);

    if (isset($_POST['action']) && ($_POST['action']) == 'message') {
        $Message = mysql_real_escape_string($_POST[message]);
        if (empty($Message)) {
            $_SESSION['errors'] = "Post Message Can't Be Blank!";
            header('Location: dashboard.php');
            die();
        } else {
            $query = "INSERT INTO messages (message, created_at, updated_at)
                      VALUES ('$Message', NOW(), NOW())";
            $result = mysql_query($query); //your run query function changed here but you can also try with run_mysql_query($query);
            $_SESSION['success'] = "message successfully posted"; //made change here from message to success
            header('Location: dashboard.php');
            die();
        }
    }
?>

Lemme知道这是否解决了这个问题,如果不是我们转移聊天。