PHP插入表问题

时间:2014-02-22 17:24:45

标签: php mysql

我再次。虽然我真的很努力练习,但我对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');
    }
}
?>

我对此非常陌生,所以毫无疑问这是一个非常简单的解决方案。也许只是错过了一些东西,但我真的无法理解。添加问题。 :(

2 个答案:

答案 0 :(得分: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());

答案 1 :(得分:0)

您需要在函数中传递变量,以便为函数添加参数,否则它将不起作用,您的变量也应在查询中以$为前缀,因此更改函数如下

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

作为旁注,我会说你的代码极易受到mysql injections的攻击。我宁愿切换到PDOmysqli并使用准备好的陈述来避免任何风险。

相关问题