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

标签 php mysql sql mamp

所以目前,这是我的代码

索引.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);

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

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

最佳答案

检查一次你在数据库中评论的数据类型是什么。(我更喜欢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);

关于PHP $_POST 为空——不适用于使用 MAMP 的本地服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36003469/

相关文章:

mysql - Node Express mysql 连接错误处理优化

http连接中的Android身份验证错误android.os.networkonmainthreadException

sql - PostgreSQL - 基于列数据的减法简化查询

python - 如何以最简单的方式连接两个没有公共(public)键的表?

php - 使用数据库查询插入记录 (Laravel)

php - Memcached vs APC 我应该选择哪一个?

php - Laravel:试图获取非对象的属性

mysql - 在 cakephp 路由中使用 - 删除 url 的空白?

php - 正则表达式选择的内容超出了我的预期(PHP)

同一个表上的查询中的 MySQL 查询