目前,这是我所拥有的代码
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是自动递增的。评论是从用户那里得到的。
var_dump()
打印的内容 - Sagar Guhecomment
是 MySQL 保留字(https://dev.mysql.com/doc/refman/5.7/en/keywords.html)。在这种情况下,请尝试使用(ID,'Comment')或更改列的名称。 - P_95