2014-12-03 11 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

相關問題