不将消息插入表中

时间:2014-04-18 05:25:06

标签: php mysql

在我的下面的代码中,它应该清除它所做的表格Banner,然后在其中插入一条消息,它不会 这是我的代码,谢谢

<?php
error_reporting(-1);
mysql_connect('localhost', 'username', 'password');
mysql_select_db("induadmi_db");
if($_POST['submit']){
    $bannermsg = $_POST['newBanner'];
mysql_query("TRUNCATE `Banner`");
mysql_query("INSERT INTO `Banner` SET Message='$bannermsg'");
}
?>
<html>
     <body>
        <form method="post">
            <input type="text" id="newBanner" name="newBanner' placeholder="Enter Message"/>
             <input type="submit" id="submit" name="submit" value="Submit">
        </form>
    </body>
</html>

这是我顺便说一句的错误:

  

注意:未定义的索引:newBanner in   第5行/home/induadmi/public_html/por/newbanner.php

5 个答案:

答案 0 :(得分:4)

这是您在HTML中的错误

<html>
         <body>
            <form method="post">
                <input type="text" id="newBanner" name="newBanner" placeholder="Enter Message"/>
                 <input type="submit" id="submit" name="submit" value="Submit">
            </form>
        </body>
    </html>

name="newBanner'是错误name="newBanner"

答案 1 :(得分:1)

您的错误意味着$_POST['newBanner']尚未定义。所以将$bannermsg = $_POST['newBanner'];放在if($_POST['submit']){条件

通过检查$_POST exixts

来尝试这样做
<?php
error_reporting(-1);
mysql_connect('localhost', 'username', 'password');
mysql_select_db("induadmi_db");
if(isset($_POST['submit']))
{
     $bannermsg = $_POST['newBanner'];
     mysql_query("TRUNCATE `Banner`");
     mysql_query("INSERT INTO `Banner` SET Message='$bannermsg'");
}
?>

答案 2 :(得分:1)

试试这个

<?php
        error_reporting(-1);
        mysql_connect('localhost', 'username', 'password');
        mysql_select_db("induadmi_db");
        if (isset($_POST['submit'])) {
            $bannermsg = $_POST['newBanner'];
             mysql_query("TRUNCATE `Banner`");
             mysql_query("INSERT INTO `Banner` SET Message='$bannermsg'");
        }
        ?>



    <html>
     <body>
        <form method="post">
            <input type="text" id="newBanner" name="newBanner" placeholder="Enter Message"/>
             <input type="submit" id="submit" name="submit" value="Submit">
        </form>
    </body>
</html>

答案 3 :(得分:0)

您没有在<form> ..

上正确关闭引号
 name="newBanner' place
                ^---------- 

这就是您收到Undefined:index通知的原因..始终使用isset构造。

另外......您的TRUNCATE语法错误..

应该是

mysql_query("TRUNCATE TABLE `Banner`");

固定代码..

<?php
if(isset($_POST['submit'])){
error_reporting(-1);
mysql_connect('localhost', 'induadmi_main', '$admiNiNd/U');
mysql_select_db("induadmi_db");
$bannermsg = $_POST['newBanner'];

    mysql_query("TRUNCATE TABLE `Banner`");
    mysql_query("INSERT INTO `Banner` SET Message='$bannermsg'");
}
?>
<html>
<body>
<form method="post" action="">
    <input type="text" id="newBanner" name="newBanner" placeholder="Enter Message"/>
    <input type="submit" id="submit" name="submit" value="Submit">
</form>
</body>
</html>

mysql_*)扩展程序自PHP 5.5.0起已弃用,将来会被删除。相反,应使用MySQLiPDO_MySQL扩展名的准备好的语句来抵御SQL注入攻击!

答案 4 :(得分:0)

你需要检查mysql_query的结果,看看调用是否成功,如果没有,检查mysql_error以查看错误是什么。

http://us2.php.net/manual/en/function.mysql-query.php

http://us2.php.net/manual/en/function.mysql-error.php