PHP $_POST为空 -- 在使用MAMP本地服务器时无法正常工作

3

目前,这是我所拥有的代码

index.php:

        <form action="insert.php" method="post">
            Comments:
            <input type="text" name="comment">
            <input type="submit">
        </form>

insert.php:

<?php

include ('index.php');

$user = 'x';
$password = '';
$db = 'comment_schema';
$host = 'localhost';
$port = 3306;

/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */

$link = mysqli_connect("localhost", "x", "", "comment_schema");

// Check connection
if($link === false){
   die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Escape user inputs for security
$comment = mysqli_real_escape_string($link, $_POST["comment"]);
$sql = "INSERT INTO parentComment(ID, Comment) VALUES('','$comment')";

// attempt insert query execution
if(mysqli_query($link, $sql)){
   echo $comment;
} else{
 echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// close connection
mysqli_close($link);

当我执行"echo $comment"时,没有任何输出。然而,如果我像这样执行"echo "hi""就能工作。我认为某种原因导致了$_POST无法被识别。有任何建议可以使其正常工作或者我做错了什么。
我的目标是将用户输入插入到phpmyadmin数据库中。目前,它能够插入,但是插入了一个空值。我的数据库只有两列: ID和评论。ID是自动递增的。评论是从用户那里得到的。

1
你为什么要再次包含index.php文件? - Sudhakar Annadurai
查看 var_dump() 打印的内容 - Sagar Guhe
因为那里是我的HTML代码所在的地方。 - Oviya Arasu
@user2468160,$_POST为空。当我插入数据时,我的数据库得到了更新,但是它没有插入值。它将$comment插入为空字符串。我不认为问题还是评论名称,因为我已经将其更改为其他内容。现在我称之为COMMENTS,但仍然无法工作。 - Oviya Arasu
显示剩余3条评论
4个回答

0

先检查一下你在数据库中的评论数据类型。(我建议使用Varchar())。 尝试按照以下方式进行:

<form action="insert.php" method="POST">
                Comments:
                <input type="text" name="comment"/>
                <input type="submit" name="submit" value="submit">
            </form>

<?php

include ('index.php');

$user = 'x';
$password = '';
$db = 'comment_schema';
$host = 'localhost';
$port = 3306;


$link = mysqli_connect("localhost", "x", "", "comment_schema");


if($link === false){
   die("ERROR: Could not connect. " . mysqli_connect_error());
}


$comment = mysqli_real_escape_string($link, $_POST["comment"]);
$sql = "INSERT INTO parentComment(ID, Comment) VALUES('','$comment')";


if(mysqli_query($link, $sql)){
   echo $comment;
} else{
 echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// close connection
mysqli_close($link);

你的代码和原问题(OP)的代码没有任何区别。 - Billal Begueradj
嘿,感谢您的建议,但不幸的是它仍然无法正常工作。没有插入任何值。评论的数据类型是varchar。 - Oviya Arasu

0

试一下

<?php
    $user = 'x';
    $password = '';
    $db = 'comment_schema';
    $host = 'localhost';

    $link = mysqli_connect($host, $user, $password, $db);

    if($link === false){
       die("ERROR: Could not connect. " . mysqli_connect_error());
    }

    // Escape user inputs for security
    $comment = mysqli_real_escape_string($link, $_POST["comment"]);
    $sql = "INSERT INTO parentComment(ID, Comment) VALUES(NULL,'$comment')";

// attempt insert query execution
if(mysqli_query($link, $sql)){
   echo $comment;
} else{
 echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// close connection
mysqli_close($link);

0
请使用以下代码:-
include ('index.php');

$user = 'x';
$password = '';
$db = 'comment_schema';
$host = 'localhost';
$port = 3306;    

$link = mysqli_connect("localhost", "x", "", "comment_schema");

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

if(!empty($_POST["comment"])){
    $comment = mysqli_real_escape_string($link, $_POST["comment"]);    
    // Escape user inputs for security
    $sql = "INSERT INTO parentComment(ID, Comment) VALUES('','$comment')";
    $result = mysqli_query($link, $sql);
    // attempt insert query execution
    if($result){
       echo $comment;
    } else{
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }
    // close connection
    mysqli_close($link);

}else{
    die('comment is not set or not containing valid value');
}

希望它能对你有所帮助 :-)

:( 它不起作用。它直接进入else语句并打印出“评论未设置或不包含有效值”,感谢您的帮助,但仍然不确定问题在哪里。 - Oviya Arasu
您在评论字段中传递了有效值吗? - Ravi Hirani
是的,我刚试着在我的表单中注释了单词“hi”。那应该是有效的。我已经将我的数据库中的注释设置为varchar。 - Oviya Arasu

0

一些调试建议:

  • var_dump($_POST); // 在mysqli_real_escape_string之前
  • var_dump($comment); // 在mysqli_real_escape_string之后

mysqli api可能无法正常工作!像这样修复查询:

  • $sql = "INSERT INTO parentComment(ID, Comment) VALUES('','".$comment."')";
  • echo $sql; // 检查SQL语法
  • echo mysqli_error($link); // 是否有错误
  • 查看您的.htaccess文件,看看是否有<Limit POST>标记
  • 删除此行include ('index.php');。假设这两个文件在一个文件夹中。所以只需运行index.php. 我尝试了没有那行代码,我成功运行了你的代码。

所以我尝试了所有这些建议。从中得出的结论是我的POST为空,我的评论也为空。同时,使用echo sql打印出来的一切都是空的。基本上,一切都是空的,我不知道为什么。你提到的@bill更正是什么? - Oviya Arasu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接