我只是倾向于php。 我今天开始编写评论工具。我不知道我哪里出错了。
我为它创建了一个php表单,数据库。 以及成功创建连接,我也能够检索数据库中已有的数据。
但是当单击“提交”按钮时,表单中的数据不会插入到数据库表中。请帮忙
PHP
<?php
include './misc.php';
$cxn= mysqli_connect($host,$user,$passwd,$db) or die("unable to connect to server");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Home</title>
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<form role="form" class="form form-default" action="./commentbox.php?value=1" method="POST">
<div class="container">
<div class="row">
<h2>Cutest Pic ever!<small>Comment your views below about this pic</small></h2>
</div><br><br>
<div class="row">
<div class="col-xs-3"></div>
<div class="col-xs-6 panel panel-success">
<div class="panel-body">
<img src="./pics/kitten.jpg" class="img-rounded">
</div>
</div>
<div class="col-xs-3"></div>
</div>
<div class="row">
<div class="col-xs-1"></div>
<div class="col-xs-10 panel panel-success">
<div class-"panel panel-heading"><br><br>
<label for="fullname">Your NAME:</label>
<?php echo("<input type='text' class='form-
control' placeholder='enter your full name.'
name='fullname' maxlength='20'>");?>
</div>
<div class="panel panel-body">
<div class="col-xs-1"></div>
<div class="col-xs-8">
<?php
//comment box
echo("<textarea rows='4' cols='50' name='commentbox'
placeholder='enter your comment' maxlength='50'></textarea>");
?>
</div>
<div class="col-xs-2">
<?php
//comment submit button
echo("<button type='submit' name='submit' class='post btn btn-lg btn-success'>POST</button>");
?>
</div>
<div class="col-xs-1"></div>
</div>
</div>
<div class="col-xs-1"></div>
</div>
<div class="comdisplay row">
<?php
$sql="SELECT * from comment";
if ($_GET['value']==1)
{
$query="INSERT into comment('comname','comdesc') values('{$_POST['fullname']}','{$_POST['commentbox']}')";
if (mysqli_query($cxn,$query))
{
$result= mysqli_query($cxn,$sql) or die("couldnt ececute query");
while ($rows=mysqli_fetch_assoc($result))
{
extract($rows);
echo("<div class='comment panel panel-body panel-success'>");
echo("<p><b>".$comname."</b></p>
<p>".$comdesc."</p></div>");
}
}
else
{
echo("Failed to add comment");
}
}
$result= mysqli_query($cxn,$sql) or die("couldnt ececute query");
$num= mysqli_num_rows($result);
if ($num==0)
{
echo("No comments have been posted yet!");
}
else
{
while ($rows=mysqli_fetch_assoc($result))
{
extract($rows);
echo("<div class='comment panel panel-body panel-success'>");
echo("<p><b>".$comname."</b></p>
<p>".$comdesc."</p></div>");
}
}
?>
</div>
</div>
</form>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="comment.js"></script>
</body>
</html>
表单将值插入数据库的部分
if ($_GET['value']==1)
{
$query="INSERT into comment('comname','comdesc') values('{$_POST['fullname']}','{$_POST['commentbox']}')";
if (mysqli_query($cxn,$query))
{
$result= mysqli_query($cxn,$sql) or die("couldnt ececute query");
while ($rows=mysqli_fetch_assoc($result))
{
extract($rows);
echo("<div class='comment panel panel-body panel-success'>");
echo("<p><b>".$comname."</b></p>
<p>".$comdesc."</p></div>");
}
}
else
{
echo("Failed to add comment");
}
}
$result= mysqli_query($cxn,$sql) or die("couldnt ececute query");
$num= mysqli_num_rows($result);
if ($num==0)
{
echo("No comments have been posted yet!");
}
else
{
while ($rows=mysqli_fetch_assoc($result))
{
extract($rows);
echo("<div class='comment panel panel-body panel-success'>");
echo("<p><b>".$comname."</b></p>
<p>".$comdesc."</p></div>");
}
}
请帮助
下面给出的是输出链接
答案 0 :(得分:1)
很高兴知道你正在学习。
到目前为止,您的代码很好,只需更改SQL
即可这
$query="INSERT into comment('comname','comdesc') values('{$_POST['fullname']}','{$_POST['commentbox']}')";
要
$fullname = mysqli_real_escape_string($cxn , $_POST['fullname']);
$commentbox = mysqli_real_escape_string($cxn , $_POST['commentbox']);
$query="INSERT into comment(comname,comdesc) values('{$fullname}','{$commentbox}')";
MySQL数据库表字段名称(在您的情况下为comname,comdesc)可以用反引号(`)括起来,以避免使用保留关键字崩溃。
单个qoutes用于在字段中插入值。
另外,请注意我的mysql_real_escape_string()
代码。
永远不要相信用户输入。
使用前过滤它们。