2015-02-07 59 views
-1

我想在PHP中创建一个评论框来将其值存储在数据库中。我的评论框没有在数据库中存储任何东西

<?php 
    include('connect.php'); 
    $name= isset($_POST['name']); 
    $comment= isset($_POST['comment']); 
    $submit=isset($_POST['submit']); 
    if($submit) 
    { 
    if($name && $comment) 
    { 
    $insert= "INSERT INTO comment(name,comment) VALUES ('$name','$comment')"; 

    } 
    else 
    { 
    echo "please fill out the field"; 
    } 


    } 
    ?> 
    <html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Comment box</title> 
    </head> 

    <body> 
    <form action="comment-box.php"> 
    <table> 
    <tr><td>NAME:</td> <td><input type="text" name="name"/></td></tr> 
    <tr><td colspan="2">comment:</td></tr> 
    <tr><td colspan="2"> <textarea name="comment"></textarea> </td></tr> 
    <tr><td colspan="2"> <input type="submit" name="submit" value="comment"></textarea> </td></tr> 

    </table> 


    </form> 
    </body> 
    </html> 

我的连接文件connect.php不会在我的数据库中存储任何东西。 请帮我一把。感谢提前:)笑我恨这个红盒子错误:d

<?php 

    mysql_connect('localhost','root','') or die(mysql_error()); 
    mysql_select_db("comment") or die ("connect select DB"); 

    ?> 
+0

你在哪里执行'sql'语句?我只能看到你在哪里定义它。你也不应该使用不推荐的'mysql_ *'函数。 – Jens 2015-02-07 10:46:28

+0

您有SQL注入的风险,您需要在发送之前“准备”您的语句。 – cybermonkey 2015-02-07 11:13:35

+0

另外,你试图插入的值是'boolean' - isset'的返回类型。 – msfoster 2015-02-08 01:02:04

回答

3

起码你查询你不执行:

$insert= "INSERT INTO comment(name,comment) VALUES ('$name','$comment')"; 

你必须运行mysql_query,但因为它已经过时,你应考虑将 切换为mysqli_queryPDO::query

$res = mysqli_query($con, $insert, ...) 

其中$con是你的连接。