简体   繁体   English

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

[英]PHP won't insert data record on my Database

I was trying to insert a messages feature that will insert record inside my database. 我试图插入一个消息功能,该功能将在数据库中插入记录。 But for some reason it won't insert my messages and its redirecting me to home.php which contains the login.php file. 但是由于某种原因,它不会插入我的消息,它不会将我重定向到包含login.php文件的home.php

I have three files here: 我在这里有三个文件:

  1. home.php home.php
  2. process.php process.php
  3. dashboard.php dashboard.php

Now given the three database users, messages, and comment, I need to insert to the database the records of the user who login. 现在给出了三个数据库用户,消息和注释,我需要将登录用户的记录插入数据库。 For some reason when click on the "Post Message" button it wont insert my message to my database. 由于某些原因,当单击“发布消息”按钮时,它不会将我的消息插入到我的数据库中。 Any idea? 任何想法?

Here's my dashboard.php : 这是我的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>

And here's my process.php : 这是我的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();
    }
}

Any idea what went wrong? 知道出了什么问题吗?

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

the action is set to "process-homepage.php" and your code is in process.php 该操作设置为“ process-homepage.php”,并且您的代码在process.php中

Can you please change action to process.php and see if it works. 您能否将操作更改为process.php并查看其是否有效。 If not then set log level to E_ALL and provide error details. 如果不是,则将日志级别设置为E_ALL并提供错误详细信息。

If you can change the original code of yours to this; 如果您可以将自己的原始代码更改为此;

<?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 know if this solve the issue, if not we move to chat. Lemme知道这是否可以解决问题,否则我们将开始聊天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM