我需要获取用户的注释并存储在数据库表第1列中,并在不同的表中显示输入的注释。代码工作正常,没有错误,但注释不会存储在数据库中。
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method='post' action=''>
<input type="text" name='Comment'/>
<input type="Submit" value="Submit" name="Submit" />
</form>
<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "escalation";
$conn = new mysqli($server, $username, $password, $database);
if ($conn->connect_error) {
die("connection failed:" . $conn->connect_error);
}
if (isset($_POST['Submit'])) $Comment = isset($_POST['Comment']) ? $_POST['Comment'] : '';
$sql = "INSERT INTO css(Dis_Cmt)VALUES('$Comment')";
$res = $conn->query($sql);
if ($res) {
echo "Successful";
echo "<br />";
echo "<a href='Uploadphp.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
?>
</body>
</html>
答案 0 :(得分:3)
试试这个。注意开合括号。
<head>
<title></title>
</head>
<body>
<form method='post' action=''>
<input type="text" name='Comment'/>
<input type="Submit" value="Submit" name="Submit" />
</form>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "escalation";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['Submit'])){
$Comment=isset($_POST['Comment']) ?$_POST['Comment']:'';
$sql="INSERT INTO css(Dis_Cmt)VALUES('$Comment')";
$res=$conn->query($sql);
if($res){
echo "Successful";
echo "<BR>";
echo "<a href='Uploadphp.php'>Back to main page</a>";
}else {
echo "ERROR";
}
}
?>
</body>
</html>
另一个信息和一些建议虽然与您的问题无关,但请使用有助于防止SQL注入的预准备语句。
你可以阅读关于mysqli准备好的陈述here的php手册
您可能还想查看PDO准备好的声明click here
您可能还想检查此this完整帮助程序类,以了解我个人创建的crud操作。还使用了PDO预处理语句。
希望能帮助某人。
答案 1 :(得分:0)
试试这个:
RedirectMatch 302 ^/archives/(.*)$ http://example.org/?=$1
答案 2 :(得分:0)
您是否检查过$ _POST是否提供了您想要的输出。如果是,那么在命令行上尝试mysql命令,看看它是否显示任何错误......你的命令应该有效,我在我的命令行中尝试过,它有效。 您没有在数据库表中设置正确的数据类型和值的长度,或者尝试这样做:
一直有效的安全命令是
Select Coalesce((SELECT meta_value AS value FROM tr_postmeta WHERE meta_key='pa_meta_debt' AND post_id=<%fieldname5%> Limit 1),0) As meta_value
答案 3 :(得分:0)
我注意到的第一件事是:
if ($conn->connect_error) {
die("connection failed:" . $conn->connect_error);
}
应该是
(!$conn)
答案 4 :(得分:0)
祝你好运!$Comment = $_POST['Comment']; $sql = "INSERT INTO css (Dis_Cmt) VALUES (?)"; $statement = $conn->prepare($sql); $statement->bind_param('s', $Comment); if($statement){ echo "Successful"; echo "<BR>"; echo "<a href='Uploadphp.php'>Back to main page</a>"; } else { echo "ERROR"; } $statement->close();