簡體   English   中英

PHP不會在我的數據庫中插入數據記錄

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

我試圖插入一個消息功能,該功能將在數據庫中插入記錄。 但是由於某種原因,它不會插入我的消息,它不會將我重定向到包含login.php文件的home.php

我在這里有三個文件:

  1. home.php
  2. process.php
  3. 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>

這是我的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();
    }
}

知道出了什么問題嗎?

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

該操作設置為“ process-homepage.php”,並且您的代碼在process.php中

您能否將操作更改為process.php並查看其是否有效。 如果不是,則將日志級別設置為E_ALL並提供錯誤詳細信息。

如果您可以將自己的原始代碼更改為此;

<?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知道這是否可以解決問題,否則我們將開始聊天。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM