PHP $ _POST为空 - 使用MAMP不适用于本地服务器

时间:2016-03-15 05:43:27

标签: php mysql sql mamp

目前,这是我的代码

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

当我回复$ comment时,没有任何内容被打印出来。但是,如果我做一些像回声&#34; hi&#34;有用。我认为由于某种原因,$ _POST没有得到认可。任何建议使这项工作或我做错了。

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

4 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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);

答案 2 :(得分: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。尝试没有那条线的代码,它对我有用。

答案 3 :(得分: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');
}

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