2011-04-22 67 views
0
$body = $_POST['post']; 
$submit = $_POST['submit']; 
$date = date("Y-m-d"); 
require('php/connect.php'); 

if($submit) 
{ 

$query = mysql_query("INSERT INTO news (`id`, `body`, `date`) VALUES (NULL, '".$body."', '".$date."')"); 
header("Location: index.php"); 
} 

我不明白爲什麼這不起作用,我直接從PHP寫了一個查詢,我的管理員在寫了一行之前手動模擬自己,它仍然不工作,有人可以嗎?MYSQL INSERT問題

+0

什麼是您的架構是什麼樣子? – 2011-04-22 15:18:27

+0

id | body |日期 – 2011-04-22 15:20:11

+1

註釋掉你的'header(「Location:index.php」); '和'追加或死亡(mysql_error());'在您的查詢代碼末尾,會告訴你什麼地方出了錯 – afarazit 2011-04-22 15:21:45

回答

2

你一定要使用mysql_real_escape_string

隨着mysql_error可以打印出一條錯誤消息,逃避你的輸入值,但你需要的連接標識符作爲參數。

1

我只是建議處理MySQL錯誤

$query = mysql_query("INSERT INTO news (`id`, `body`, `date`) VALUES (NULL, '".$body."', '".$date."')") 
or trigger_error(mysql_error()); 

如果id是主鍵不能爲空

你應該使用mysql_real_escape_string功能逃跑用戶輸入。

,會發生什麼,如果我在你的$body輸入放"that's it "價值,你的查詢將失敗。

+0

它仍然不會發布。 – 2011-04-22 15:23:51

+0

添加'mysql_error'後收到了什麼錯誤? – 2011-04-22 15:28:44

+0

在看到任何錯誤之前,他需要註釋掉header() – afarazit 2011-04-22 15:39:24

0

註釋掉你的標題(「Location:index.php」);並追加或死亡(mysql_error());在查詢代碼的最後,它會告訴你出了什麼問題。在將其插入數據庫之前,您還應該使用「mysql_real_escape_string」用戶輸入。

$body = mysql_real_escape_string($_POST['post']); 
$submit = $_POST['submit']; 
$date = date("Y-m-d"); 
require('php/connect.php'); 

if($submit) 
{ 

$query = mysql_query("INSERT INTO news (`id`, `body`, `date`) VALUES (NULL, '$body', '$date')") or die(mysql_error()); 
//header("Location: index.php"); 
}