【问题标题】:PHP $_POST empty -- not working for local server using MAMPPHP $_POST 为空——不适用于使用 MAMP 的本地服务器
【发布时间】:2016-06-30 10:21:40
【问题描述】:

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

index.php:

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

插入.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);

当我回显 $comment 时,没有任何内容被打印出来。但是,如果我执行类似 echo "hi" 的操作,它会起作用。我认为由于某种原因 $_POST 未被识别。任何使这项工作的建议,或者如果我做错了。

我的目标是获取用户输入并插入到 phpmyadmin 上的数据库中。目前,它能够插入,但是它插入一个空值。我的数据库中只有两列。一个 ID 和一个 Comment 列。 ID 会自动递增。评论是我从用户那里得到的。

【问题讨论】:

  • 为什么又要包含index.php?
  • 看看var_dump() 打印了什么
  • 没有打印出来。它是空的。
  • @sudhakar,我不应该包括 index.php 吗?
  • 是的尝试一下,我可以知道包含该页面的原因是什么? @oviya

标签: php mysql sql mamp


【解决方案1】:

检查一下您在数据库中的评论数据类型是什么。(我更喜欢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的代码没有区别。
  • 嘿,感谢您的建议,但不幸的是它仍然无法正常工作。没有值被插入。注释的数据类型是varchar
【解决方案2】:

试试这个

<?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);

【讨论】:

    【解决方案3】:

    一些调试建议:

    • 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 文件,看看您是否有 &lt;Limit POST&gt; 标签
    • 删除此行include ('index.php');。假设这两个文件在一个文件夹中。所以只需运行 index.php 。尝试了没有该行的代码,它对我有用。

    【讨论】:

    • 所以我尝试了所有这些建议。我从中得到的是我的 POST 是空的,我的评论也是空的。还回显 sql 打印出带有空注释变量的所有内容。基本上,一切都是空的,我不知道为什么。你指的@bill 更正是什么?
    【解决方案4】:

    使用下面的代码:-

    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 语句并打印出 'comment is not set or not contain valid value' 感谢您的帮助,但仍然不确定是什么问题是。
    • 您是否在评论字段中传递了有效值?
    • 是的,我只是想在我的表单中添加“hi”这个词。那应该是有效的。我已将数据库中的评论设置为 varchar
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    相关资源
    最近更新 更多