2014-02-22 278 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