English 中文(简体)
表格不张贴到数据库——PHP
原标题:Form not posting to database - PHP
  • 时间:2012-01-12 16:09:00
  •  标签:
  • php
  • mysqli

I m 更新到PHP和设计小型博客。 我试图提出一个表格,以设立一个新的员额,但那是没有工作的,我可以说明原因。 我把它与我的登记表工作法典相比较,似乎也一样。 我不会发现任何错误,而是仅仅重载网页,而不是张贴到数据库。

View - v_newpost.php

    <article>
    <?php
    if (!isset ($_SESSION[ username ]))
    {
    ?>
    <span class="alert">Please login to create a post.</span>
    <?php
    }

    else 
    {
    ?>
    <form class="newpost" action="" method="post">
    <fieldset>
    <legend>Submit a new post</legend>
    <?php if ($error[ alert ] !=   ) { echo "<span class= alert >".$error[ alert ]."</span>";} ?>
    <ul>
    <li>
    <label for="title">Title:</label>
    <input type="text" name="title" value="<?php echo $input[ title ]; ?>" required autofocus>
    </li>
    <li>
    <label for="content">Content:</label>
    <textarea id="content" name="content" rows=6 value="<?php echo $input[ content ]; ?>"></textarea>
    </li>
    </ul>
    </fieldset>
    <fieldset>
    <button type="submit" class=postbutton>Publish</button>
    </fieldset>
    </form>
    </div>
    <?php
    }
    ?>
</article>

newpost.php

    <?php
    require_once  includes/connection.php ;

$error[ alert ] =   ;
$input[ title ] =   ;
$input[ content ] =   ;

if (isset($_POST[ submit ]))
{
    if ($_POST[ title ] ==    || $_POST[ content ] ==   )
    {
        $error[ alert ] =  Please give your post a title and content. ;

        $input[ title ] = $_POST[ title ];
        $input[ content ] = $_POST[ content ];

        include_once( v_newpost.php );
    }
    else
    {
        $input[ title ] = htmlentities($_POST[ title ], ENT_QUOTES);
        $input[ content ] = htmlentities($_POST[ content ], ENT_QUOTES);

        if ($stmt = $mysqli->prepare("INSERT INTO posts (title, content) VALUES (?,?)"))
        {
            $stmt->bind_param("ss", $input[ title ], $input[ content ]);
            $stmt->execute();
            $stmt->close();

            $error[ alert ] =   ;
            $input[ title ] =   ;
            $input[ content ] =   ;

            header( Location: index.php );
        }
        else
        {
            $error[ alert ] =  Failed to create post ;
        }
    }

}
else
{
    include_once( v_newpost.php );
}


?>

我确信,这很可能是一件 st事,但我已经多次审视了这个问题,并能够理解为什么它不工作。

最佳回答

以下内容永远不会出现,因为你没有<代码>名称=“提交”的表格。

if (isset($_POST[ submit ]))
问题回答

你们不得不确定形式的行动,以指明新的职位。 php

你需要以你的形式采取行动。 Try:

<form class="newpost" action="newpost.php" method="post">

In newpost.php you have if (isset($_POST[ submit ])) It ll not work because you do not send anything with name submit.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签