2014-02-22 277 views
1

我再次。雖然我真的很努力地嘗試練習,但我對PHP真的很陌生,所以現在的條款有點兒麻煩。PHP插入表問題

我現在的問題是我的CMS似乎無法將數據提交到MySQL表。這裏是我的功能:

function newEntry() { 
    $query = mysql_query("INSERT INTO entries VALUES(null,'name','description','content')") or die(mysql_error()); 
} 

這裏是我的提交內容形式:

<form action="doNewEntry.php" method="post"> 
    <textarea name="entTitle" id="entTitle">Entry Title</textarea><br> 
    <textarea name="entDesc" id="entDesc">Input Description Here</textarea><br> 
    <textarea name="entCont" id="entCont">Type and format content here! What you see, is what you get.</textarea><br> 
    <script> 
    CKEDITOR.replace('entCont'); 
    </script> 
    <table><td colspan="2"><input type="submit" name="submit" /></td></table> 
</form> 

,這裏是文件之間的事實,使崗位:

<?php 
include('includes/functions.php'); 
if(isset($_POST['submit'])) { 
      if(isset($_POST['entTitle'])) { 
       newEntry($_POST['entTitle'],$_POST['entDesc'],$_POST['entCont']); 
       header("Location: entries.php"); 
     } else { 
      echo "Please fill out all fields!"; 
      include('newEntry.php'); 
    } 
} 
?> 

我難以置信新的,所以這無疑是一個非常簡單的修復。也許只是錯過了一些東西,但我真的無法弄清楚。添加問題。 :(

+0

你通過parame通過newEntry調用,但在newEntry函數中,您沒有得到任何參數! –

回答

1
function newEntry() 
You have passed the parameters to this function but dint received in definition. 

function newEntry($title, $description ,$content){ 
     //your code here 
} 

Need to reform this query 

$query = mysql_query("INSERT INTO entries VALUES(null,'name','description','content')") or die(mysql_error()); 
+0

這些值是靜態的,他需要使用傳遞給函數的參數(例如$ title,$ desc,$ content)。 – Dexa

0

你需要通過你的變量在你的功能,所以參數添加到您的功能,否則將無法正常工作,您也應變量通過$在查詢預先考慮,因此改變功能如下

function newEntry($name, $description, $content) 
{ 
    $query = mysql_query("INSERT INTO entries VALUES(null,'$name','$description','$content')") or die(mysql_error()); 
} 

至於側面說明我要說的是,你的代碼是higly容易mysql injections。我寧願切換到任何PDOmysqli和使用準備好的statments以避免任何風險。

+0

此功能位於管理面板中,因此沒有密碼的人不會面臨風險,我認爲不正確?如果這個系統有用,我想我會在後面學習PDO。 我現在還得到一個錯誤:列計數不匹配第1行的值計數 –

+0

@jack我wouls而是在查詢中指示所有的莖,以便INSERT INTO表(列1,列2等...)VALUES( 'value1','value2'等等) – Fabio