2014-12-03 74 views
2

因此,我试图将评论推送到我的数据库(icposts,下面),我没有得到任何结果。我可以拉和显示我直接插入到数据库表中的评论,但是当我尝试从html表单发送评论时,它似乎根本不起作用。将评论发送给我在本地主机上的数据库

<?php 

$connect=mysqli_connect("localhost","root",""); 
$database=mysqli_select_db("icposts"); 


$username=$_POST['poster']; 
$title=$_POST['postTitle']; 
$body=$_POST['postText']; 
$date=$_POST['currentDate']; 
$submit=$_POST['submit']; 

if($submit) 
{ 
     $query=mysql_query("INSERT INTO 'posts'('id', 'username', 'title', 'body', 'date') VALUES ('','$username','$title','$body','$date')"); 

} 
?> 

这里的表单的HTML,供参考:

<form name="input" action="comments.php" method="POST"> 
 
\t Username: <input id = "poster" type="text" name="poster"value="Guest" /><br> 
 
\t Tite: <input id = "postTitle" type="text" name="postTitle" /><br> 
 
\t Comment: <br> <textarea id = "postText" name = "postText"rows="4" cols="50"></textarea> 
 
\t <input id = "submit" name = "submit" type="submit" value="Submit" /> 
 
\t <input id = "currentDate" name = "currentDate" type = "hidden" value = "" /> 
 
</form>
我一直在寻找不同的例子,我看不出什么毛病,我得到了什么那里,当我比较它与其他人在网上发布了什么。

回答

2

首先,您需要将连接传递给$database=mysqli_select_db("icposts");

然后你开始混合MySQL API与mysql_query。他们只是不混合。

$database=mysqli_select_db($connect,"icposts"); 

那么你的表和列使用了错误的identifiers引号。

要么使用蜱,或将其删除(引号),并通过连接到查询:

$query=mysqli_query($connect,"INSERT INTO `posts` (`id`, `username`, `title`, `body`, `date`) 

VALUES ('','$username','$title','$body','$date')"); 

同时添加or die(mysqli_error($connection))mysqli_query()check for DB errors,这就是为什么你没有得到错误的原因;你没有检查他们。 Error reporting是另一个你应该看看。

例子:

if (!mysqli_query($connection,"INSERT INTO `posts` (`id`, `username`, `title`, `body`, `date`) 
    VALUES ('','$username','$title','$body','$date')"); 
) 
    { 
    echo("Error description: " . mysqli_error($connection)); 
    } 
else{ 
    echo "Success!"; 
} 

您也可以使用所有4个参数来代替:

$connect=mysqli_connect("localhost", "root", "", "icposts"); 

您可能还需要与

if(isset($_POST['submit'])) 

更换if($submit)那么你可以得到摆脱$submit=$_POST['submit'];。最好使用isset()

注意:您将需要确保您的currentDate列允许空白数据,否则您将需要给它一些形式的价值。

关于“id”列的另一个说明。如果它是一个auto_increment,你可以从查询中忽略它。

数据库将自行增加。


旁注:

你现在的代码是开放的SQL injection。使用prepared statementsPDO with prepared statements,他们更安全

在此期间,直到你进入使用准备好的语句,使用更改代码:

$username = stripslashes($_POST['poster']); 
$username = mysqli_real_escape_string($connection, $_POST['poster']); 

,并为所有的变量这样做。


这里是一个准备好的语句底漆:

<?php 
$link = new mysqli('localhost', 'root', '', 'database'); 
if ($link->connect_errno) { 
    throw new Exception($link->connect_error, $link->connect_errno); 
} 

// Check that the expected value has been provided via a POST request 
if (!isset($_POST['input1'])) { 
    throw new Exception('Missing POST request parameter [input1]'); 
} 

// now prepare an INSERT statement 
if (!$stmt = $link->prepare('INSERT INTO `your_table` (`name`) VALUES (?)')) { 
    throw new Exception($link->error, $link->errno); 
} 

// bind parameters 
$stmt->bind_param('s', $_POST['input1']); 

if (!$stmt->execute()) { 
    throw new Exception($stmt->error, $stmt->errno); 
} 
+0

感谢您的帮助!我正在做这项工作,并不要求页面安全,但我会在未来的项目中记住SQL注入。 – 2014-12-03 21:09:46

+0

不客气谢恩,很高兴得到了帮助。请记住标记为已解决,*欢呼声* – 2014-12-03 21:11:29

0
$connect=mysqli_connect("localhost","root",""); 

应(选择DB可以简单地删除)

$connect=mysqli_connect("localhost","root","", "icposts"); 

而且

$query=mysql_query("INSERT INTO 'posts'('id', 'username', 'title', 'body', 'date') VALUES ('','$username','$title','$body','$date')"); 

应该

$query=mysqli_query("INSERT INTO 'posts'('id', 'username', 'title', 'body', 'date') VALUES ('','$username','$title','$body','$date')", $database); 

不守请记住,这是一个非常糟糕的回答,也看着你的查询好像id是一个自动递增的列。如果是这种情况,您甚至不必将其写入查询本身。

您可能想要进一步研究参数化查询。 这是一个不错的职位。 How can I prevent SQL injection in PHP?

+0

感谢您的帮助,我会继续安全考虑我的未来计划,但现在,这并不一定是安全的我目前。 – 2014-12-03 21:11:42

相关问题